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>
73 #include <sys/systm.h>
75 #include <machine/bus.h>
76 #include <machine/stdarg.h>
79 #include <isa/isavar.h>
80 #include <pc98/cbus/fdcreg.h>
81 #include <pc98/cbus/fdcvar.h>
82 #include <pc98/pc98/pc98_machdep.h>
84 #include <isa/isavar.h>
85 #include <isa/isareg.h>
86 #include <dev/fdc/fdcreg.h>
87 #include <dev/fdc/fdcvar.h>
91 #define FDBIO_FORMAT BIO_CMD2
93 /* configuration flags for fdc */
94 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */
97 * Stop retrying after this many DMA overruns. Since each retry takes
98 * one revolution, with 300 rpm., 25 retries take approximately 5
99 * seconds which the read attempt will block in case the DMA overrun
102 #define FDC_DMAOV_MAX 25
105 * Timeout value for the PIO loops to wait until the FDC main status
106 * register matches our expectations (request for master, direction
107 * bit). This is supposed to be a number of microseconds, although
108 * timing might actually not be very accurate.
110 * Timeouts of 100 msec are believed to be required for some broken
113 #define FDSTS_TIMEOUT 100000
116 * Number of subdevices that can be used for different density types.
124 #define FDBIO_RDSECTID BIO_CMD1
127 * List of native drive densities. Order must match enum fd_drivetype
128 * in <sys/fdcio.h>. Upon attaching the drive, each of the
129 * programmable subdevices is initialized with the native density
133 static struct fd_type fd_native_types[] =
135 { 0 }, /* FDT_NONE */
136 { 0 }, /* FDT_360K */
137 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* FDT_12M */
138 { 0 }, /* FDT_720K */
139 { 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,FL_MFM }, /* FDT_144M */
140 { 0 }, /* FDT_288M */
143 static struct fd_type fd_searchlist_12m[] = {
144 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* 1.2M */
146 { 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,FL_MFM }, /* 820K */
147 { 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,FL_MFM }, /* 800K */
149 { 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,FL_MFM }, /* 720K */
150 { 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_MFM|FL_2STEP },/* 360K */
151 { 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,FL_MFM }, /* 640K */
152 { 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,FL_MFM }, /* 1.23M 1024/sec */
154 { 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,FL_MFM }, /* 1.28M 1024/sec */
157 static struct fd_type fd_searchlist_144m[] = {
159 { 21,2,0xFF,0x04,82,3444,2,2,0x0C,2,0,FL_MFM }, /* 1.72M in 3mode */
160 { 18,2,0xFF,0x1B,82,2952,2,2,0x54,1,0,FL_MFM }, /* 1.48M in 3mode */
162 { 18,2,0xFF,0x1B,80,2880,2,2,0x54,1,0,FL_MFM }, /* 1.44M in 3mode */
163 { 15,2,0xFF,0x1B,80,2400,0,2,0x54,1,0,FL_MFM }, /* 1.2M */
165 { 10,2,0xFF,0x10,82,1640,1,2,0x30,1,0,FL_MFM }, /* 820K */
166 { 10,2,0xFF,0x10,80,1600,1,2,0x30,1,0,FL_MFM }, /* 800K */
168 { 9,2,0xFF,0x20,80,1440,1,2,0x50,1,0,FL_MFM }, /* 720K */
169 { 9,2,0xFF,0x20,40, 720,1,2,0x50,1,0,FL_MFM|FL_2STEP },/* 360K */
170 { 8,2,0xFF,0x2A,80,1280,1,2,0x50,1,0,FL_MFM }, /* 640K */
171 { 8,3,0xFF,0x35,77,1232,0,2,0x74,1,0,FL_MFM }, /* 1.23M 1024/sec */
173 { 8,3,0xFF,0x35,80,1280,0,2,0x74,1,0,FL_MFM }, /* 1.28M 1024/sec */
174 { 9,3,0xFF,0x35,82,1476,0,2,0x47,1,0,FL_MFM }, /* 1.48M 1024/sec 9sec */
175 { 10,3,0xFF,0x1B,82,1640,2,2,0x54,1,0,FL_MFM }, /* 1.64M in 3mode - Reserve */
179 static struct fd_type fd_native_types[] =
181 { 0 }, /* FDT_NONE */
182 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */
183 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */
184 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */
185 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
186 #if 0 /* we currently don't handle 2.88 MB */
187 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/
189 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */
194 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density
195 * selection, they just start out with their native density (or lose).
196 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their
197 * respective lists of densities to search for.
199 static struct fd_type fd_searchlist_12m[] = {
200 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */
201 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */
202 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
205 static struct fd_type fd_searchlist_144m[] = {
206 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
207 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
210 /* We search for 1.44M first since this is the most common case. */
211 static struct fd_type fd_searchlist_288m[] = {
212 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */
214 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */
216 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */
220 #define MAX_SEC_SIZE (128 << 3)
221 #define MAX_CYLINDER 85 /* some people really stress their drives
225 devclass_t fdc_devclass;
228 * Per drive structure (softc).
231 struct fdc_data *fdc; /* pointer to controller structure */
232 int fdsu; /* this units number on this controller */
233 enum fd_drivetype type; /* drive type */
234 struct fd_type *ft; /* pointer to current type descriptor */
235 struct fd_type fts[NUMDENS]; /* type descriptors */
237 #define FD_OPEN 0x01 /* it's open */
238 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */
239 #define FD_ACTIVE 0x04 /* it's active */
240 #define FD_MOTOR 0x08 /* motor should be on */
241 #define FD_MOTOR_WAIT 0x10 /* motor coming up */
242 #define FD_UA 0x20 /* force unit attention */
245 #define FD_NO_TRACK -2
246 int track; /* where we think the head is */
247 int options; /* user configurable options, see fdcio.h */
248 struct callout_handle toffhandle;
249 struct callout_handle tohandle;
250 struct devstat *device_stats;
251 struct cdev *masterdev;
264 static devclass_t fd_devclass;
266 /* configuration flags for fd */
267 #define FD_TYPEMASK 0x0f /* drive type, matches enum
268 * fd_drivetype; on i386 machines, if
269 * given as 0, use RTC type for fd0
271 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK)
272 #define FD_NO_CHLINE 0x10 /* drive does not support changeline
273 * aka. unit attention */
274 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
275 * assume it is there */
278 * Throughout this file the following conventions will be used:
280 * fd is a pointer to the fd_data struct for the drive in question
281 * fdc is a pointer to the fdc_data struct for the controller
282 * fdu is the floppy drive unit number
283 * fdcu is the floppy controller unit number
284 * fdsu is the floppy drive unit number on that controller. (sub-unit)
288 * Function declarations, same (chaotic) order as they appear in the
289 * file. Re-ordering is too late now, it would only obfuscate the
290 * diffs against old and offspring versions (like the PC98 one).
292 * Anyone adding functions here, please keep this sequence the same
293 * as below -- makes locating a particular function in the body much
296 static u_int8_t fdsts_rd(fdc_p);
297 static void fddata_wr(fdc_p, u_int8_t);
298 static u_int8_t fddata_rd(fdc_p);
299 static int fdc_err(struct fdc_data *, const char *);
300 static int enable_fifo(fdc_p fdc);
301 static int fd_sense_drive_status(fdc_p, int *);
302 static int fd_sense_int(fdc_p, int *, int *);
303 static int fd_read_status(fdc_p);
304 static int fd_probe(device_t);
305 static int fd_attach(device_t);
306 static int fd_detach(device_t);
307 static void set_motor(struct fdc_data *, int, int);
310 static timeout_t fd_turnoff;
311 static timeout_t fd_motor_on;
312 static void fd_turnon(struct fd_data *);
313 static void fdc_reset(fdc_p);
314 static int fd_in(struct fdc_data *, int *);
315 static int out_fdc(struct fdc_data *, int);
316 static d_open_t fdopen;
317 static d_close_t fdclose;
318 static d_strategy_t fdstrategy;
319 static void fdstart(struct fdc_data *);
320 static timeout_t fd_iotimeout;
321 static timeout_t fd_pseudointr;
322 static driver_intr_t fdc_intr;
323 static int fdcpio(fdc_p, long, caddr_t, u_int);
324 static int fdautoselect(struct cdev *);
325 static int fdstate(struct fdc_data *);
326 static int retrier(struct fdc_data *);
327 static void fdbiodone(struct bio *);
328 static int fdmisccmd(struct cdev *, u_int, void *);
329 static d_ioctl_t fdioctl;
331 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */
334 /* CAUTION: fd_debug causes huge amounts of logging output */
335 static int volatile fd_debug = 0;
336 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0)
337 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0)
338 #else /* FDC_DEBUG */
339 #define TRACE0(arg) do { } while (0)
340 #define TRACE1(arg1, arg2) do { } while (0)
341 #endif /* FDC_DEBUG */
344 * Bus space handling (access to low-level IO).
348 fdout_wr(fdc_p fdc, u_int8_t v)
350 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
357 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
361 fddata_wr(fdc_p fdc, u_int8_t v)
363 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
369 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
374 fdctl_wr(fdc_p fdc, u_int8_t v)
376 bus_space_write_1(fdc->portt, fdc->porth, FDCTL, v);
384 return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
388 static struct cdevsw fd_cdevsw = {
389 .d_version = D_VERSION,
393 .d_write = physwrite,
395 .d_strategy = fdstrategy,
397 .d_flags = D_DISK | D_NEEDGIANT,
401 * Auxiliary functions. Well, some only. Others are scattered
402 * throughout the entire file.
405 fdc_err(struct fdc_data *fdc, const char *s)
409 if (fdc->fdc_errs < FDC_ERRMAX)
410 device_printf(fdc->fdc_dev, "%s", s);
411 else if (fdc->fdc_errs == FDC_ERRMAX)
412 device_printf(fdc->fdc_dev, "too many errors, not "
413 "logging any more\n");
420 * fd_cmd: Send a command to the chip. Takes a varargs with this structure:
422 * # of output bytes, output bytes as ints ...,
423 * # of input bytes, input bytes as ints ...
426 fd_cmd(struct fdc_data *fdc, int n_out, ...)
434 cmd = (u_char)(va_arg(ap, int));
437 for (n = 0; n < n_out; n++)
439 if (out_fdc(fdc, va_arg(ap, int)) < 0)
442 snprintf(msg, sizeof(msg),
443 "cmd %x failed at out byte %d of %d\n",
445 return fdc_err(fdc, msg);
448 n_in = va_arg(ap, int);
449 for (n = 0; n < n_in; n++)
451 int *ptr = va_arg(ap, int *);
452 if (fd_in(fdc, ptr) < 0)
455 snprintf(msg, sizeof(msg),
456 "cmd %02x failed at in byte %d of %d\n",
458 return fdc_err(fdc, msg);
466 enable_fifo(fdc_p fdc)
470 if ((fdc->flags & FDC_HAS_FIFO) == 0) {
473 * Cannot use fd_cmd the normal way here, since
474 * this might be an invalid command. Thus we send the
475 * first byte, and check for an early turn of data directon.
478 if (out_fdc(fdc, I8207X_CONFIG) < 0)
479 return fdc_err(fdc, "Enable FIFO failed\n");
481 /* If command is invalid, return */
483 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
484 != NE7_RQM && j-- > 0) {
485 if (i == (NE7_DIO | NE7_RQM)) {
493 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
495 return fdc_err(fdc, "Enable FIFO failed\n");
497 fdc->flags |= FDC_HAS_FIFO;
501 I8207X_CONFIG, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
502 return fdc_err(fdc, "Re-enable FIFO failed\n");
507 fd_sense_drive_status(fdc_p fdc, int *st3p)
511 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
513 return fdc_err(fdc, "Sense Drive Status failed\n");
522 fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
526 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
529 "sense intr err reading stat reg 0\n");
536 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
538 * There doesn't seem to have been an interrupt.
543 if (fd_in(fdc, &cyl) < 0) {
544 return fdc_err(fdc, "can't get cyl num\n");
555 fd_read_status(fdc_p fdc)
559 for (i = ret = 0; i < 7; i++) {
561 * XXX types are poorly chosen. Only bytes can be read
562 * from the hardware, but fdc->status[] wants u_ints and
563 * fd_in() gives ints.
567 ret = fd_in(fdc, &status);
568 fdc->status[i] = status;
574 fdc->flags |= FDC_STAT_VALID;
576 fdc->flags &= ~FDC_STAT_VALID;
582 static int pc98_trans = 0; /* 0 : HD , 1 : DD , 2 : 1.44 */
583 static int pc98_trans_prev = -1;
585 static void set_density(fdc_p fdc)
587 /* always motor on */
588 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
589 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
591 fdctl_wr(fdc, FDC_RST | FDC_DMAE);
592 /* in the case of note W, always inhibit 100ms timer */
595 static int pc98_fd_check_ready(fdu_t fdu)
597 fd_p fd = devclass_get_softc(fd_devclass, fdu);
598 struct fdc_data *fdc = fd->fdc;
599 int retry = 0, status;
601 while (retry++ < 30000) {
602 set_motor(fdc, fd->fdsu, TURNON);
603 out_fdc(fdc, NE7CMD_SENSED); /* Sense Drive Status */
605 out_fdc(fdc, fdu); /* Drive number */
607 if ((fd_in(fdc, &status) == 0) && (status & NE7_ST3_RD)) {
608 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
616 static void pc98_fd_check_type(struct fd_data *fd)
618 struct fdc_data *fdc;
620 if (fd->type != FDT_NONE || fd->fdu < 0 || fd->fdu > 3)
625 /* Look up what the BIOS thinks we have. */
626 if (!((PC98_SYSTEM_PARAMETER(0x55c) >> fd->fdu) & 0x01)) {
630 if ((PC98_SYSTEM_PARAMETER(0x5ae) >> fd->fdu) & 0x01) {
631 /* Check 3mode I/F */
633 bus_space_write_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0,
634 (fd->fdu << 5) | 0x10);
635 if (!(bus_space_read_1(fdc->sc_fdemsiot, fdc->sc_fdemsioh, 0) &
640 device_printf(fd->dev,
641 "Warning: can't control 3mode I/F, fallback to 2mode.\n");
649 fdc_release_resources(struct fdc_data *fdc)
655 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
656 fdc->fdc_intr = NULL;
658 if (fdc->res_irq != 0) {
659 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
661 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
666 if (fdc->res_ctl != 0) {
667 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
669 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
675 if (fdc->res_fdsio != 0) {
676 bus_deactivate_resource(dev, SYS_RES_IOPORT, 3,
678 bus_release_resource(dev, SYS_RES_IOPORT, 3, fdc->res_fdsio);
679 fdc->res_fdsio = NULL;
681 if (fdc->res_fdemsio != 0) {
682 bus_deactivate_resource(dev, SYS_RES_IOPORT, 4,
684 bus_release_resource(dev, SYS_RES_IOPORT, 4, fdc->res_fdemsio);
685 fdc->res_fdemsio = NULL;
688 if (fdc->res_ioport != 0) {
689 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
691 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
693 fdc->res_ioport = NULL;
695 if (fdc->res_drq != 0) {
696 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
698 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
705 * Configuration/initialization stuff, per controller.
709 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
711 struct fdc_ivars *ivars = device_get_ivars(child);
714 case FDC_IVAR_FDUNIT:
715 *result = ivars->fdunit;
717 case FDC_IVAR_FDTYPE:
718 *result = ivars->fdtype;
727 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
729 struct fdc_ivars *ivars = device_get_ivars(child);
732 case FDC_IVAR_FDUNIT:
733 ivars->fdunit = value;
735 case FDC_IVAR_FDTYPE:
736 ivars->fdtype = value;
745 fdc_initial_reset(struct fdc_data *fdc)
748 /* see if it can handle a command */
749 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(4, 240),
750 NE7_SPEC_2(2, 0), 0))
753 /* First, reset the floppy controller. */
756 fdout_wr(fdc, FDO_FRST);
758 /* Then, see if it can handle a command. */
759 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
760 NE7_SPEC_2(2, 0), 0))
767 fdc_detach(device_t dev)
769 struct fdc_data *fdc;
772 fdc = device_get_softc(dev);
774 /* have our children detached first */
775 if ((error = bus_generic_detach(dev)))
779 /* reset controller, turn motor off */
782 /* reset controller, turn motor off */
786 fdc_release_resources(fdc);
791 * Add a child device to the fdc controller. It will then be probed etc.
794 fdc_add_child(device_t dev, const char *name, int unit)
796 struct fdc_ivars *ivar;
799 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
802 child = device_add_child(dev, name, unit);
804 free(ivar, M_DEVBUF);
807 device_set_ivars(child, ivar);
809 ivar->fdtype = FDT_NONE;
810 if (resource_disabled(name, unit))
811 device_disable(child);
816 fdc_attach(device_t dev)
818 struct fdc_data *fdc;
821 fdc = device_get_softc(dev);
823 error = bus_setup_intr(dev, fdc->res_irq,
824 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
827 device_printf(dev, "cannot setup interrupt\n");
830 fdc->fdcu = device_get_unit(dev);
831 fdc->flags |= FDC_NEEDS_RESET;
833 fdc->state = DEVIDLE;
836 /* reset controller, turn motor off, clear fdout mirror reg */
839 /* reset controller, turn motor off, clear fdout mirror reg */
840 fdout_wr(fdc, fdc->fdout = 0);
842 bioq_init(&fdc->head);
848 fdc_hints_probe(device_t dev)
850 const char *name, *dname;
854 * Probe and attach any children. We should probably detect
855 * devices from the BIOS unless overridden.
857 name = device_get_nameunit(dev);
859 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
860 resource_int_value(dname, dunit, "drive", &dunit);
861 fdc_add_child(dev, dname, dunit);
864 if ((error = bus_generic_attach(dev)) != 0)
870 fdc_print_child(device_t me, device_t child)
872 int retval = 0, flags;
874 retval += bus_print_child_header(me, child);
875 retval += printf(" on %s drive %d", device_get_nameunit(me),
876 fdc_get_fdunit(child));
877 if ((flags = device_get_flags(me)) != 0)
878 retval += printf(" flags %#x", flags);
879 retval += printf("\n");
885 * Configuration/initialization, per drive.
888 fd_probe(device_t dev)
895 struct fdc_data *fdc;
899 fdsu = fdc_get_fdunit(dev);
900 fd = device_get_softc(dev);
901 fdc = device_get_softc(device_get_parent(dev));
902 flags = device_get_flags(dev);
907 fd->fdu = device_get_unit(dev);
909 /* Auto-probe if fdinfo is present, but always allow override. */
910 type = FD_DTYPE(flags);
911 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
915 /* make sure fdautoselect() will be called */
921 pc98_fd_check_type(fd);
924 * XXX I think using __i386__ is wrong here since we actually want to probe
925 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will
929 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) {
930 /* Look up what the BIOS thinks we have. */
932 if ((fdc->flags & FDC_ISPCMCIA))
934 * Somewhat special. No need to force the
935 * user to set device flags, since the Y-E
936 * Data PCMCIA floppy is always a 1.44 MB
941 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
943 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
945 if (fd->type == FDT_288M_1)
948 #endif /* __i386__ */
951 /* is there a unit? */
952 if (fd->type == FDT_NONE)
957 set_motor(fdc, fdsu, TURNON);
958 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
959 DELAY(1000000); /* 1 sec */
961 if ((flags & FD_NO_PROBE) == 0) {
962 /* If we're at track 0 first seek inwards. */
963 if ((fd_sense_drive_status(fdc, &st3) == 0) &&
964 (st3 & NE7_ST3_T0)) {
965 /* Seek some steps... */
966 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
967 /* ...wait a moment... */
969 /* make ctrlr happy: */
970 fd_sense_int(fdc, 0, 0);
974 for (i = 0; i < 2; i++) {
976 * we must recalibrate twice, just in case the
977 * heads have been beyond cylinder 76, since
978 * most FDCs still barf when attempting to
979 * recalibrate more than 77 steps
982 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
983 /* a second being enough for full stroke seek*/
984 DELAY(i == 0 ? 1000000 : 300000);
986 /* anything responding? */
987 if (fd_sense_int(fdc, &st0, 0) == 0 &&
988 (st0 & NE7_ST0_EC) == 0)
989 break; /* already probed succesfully */
994 set_motor(fdc, fdsu, TURNOFF);
996 if ((flags & FD_NO_PROBE) == 0 &&
997 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1003 /* This doesn't work before the first reset. */
1004 if ((fdc->flags & FDC_HAS_FIFO) == 0 &&
1005 fdc->fdct == FDC_ENHANCED &&
1006 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 &&
1007 enable_fifo(fdc) == 0) {
1008 device_printf(device_get_parent(dev),
1009 "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1016 device_set_desc(dev, "1.44M FDD");
1019 device_set_desc(dev, "1M/640K FDD");
1027 device_set_desc(dev, "1200-KB 5.25\" drive");
1030 device_set_desc(dev, "1440-KB 3.5\" drive");
1033 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1036 device_set_desc(dev, "360-KB 5.25\" drive");
1039 device_set_desc(dev, "720-KB 3.5\" drive");
1045 fd->track = FD_NO_TRACK;
1052 callout_handle_init(&fd->toffhandle);
1053 callout_handle_init(&fd->tohandle);
1055 /* initialize densities for subdevices */
1057 for (i = 0; i < NUMDENS; i++)
1058 memcpy(fd->fts + i, fd_searchlist_144m + i,
1059 sizeof(struct fd_type));
1061 for (i = 0; i < NUMDENS; i++)
1062 memcpy(fd->fts + i, fd_native_types + fd->type,
1063 sizeof(struct fd_type));
1069 fd_attach(device_t dev)
1073 fd = device_get_softc(dev);
1074 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu,
1075 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1076 fd->masterdev->si_drv1 = fd;
1077 fd->device_stats = devstat_new_entry(device_get_name(dev),
1078 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
1079 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
1080 DEVSTAT_PRIORITY_FD);
1085 fd_detach(device_t dev)
1089 fd = device_get_softc(dev);
1090 untimeout(fd_turnoff, fd, fd->toffhandle);
1091 devstat_remove_entry(fd->device_stats);
1092 destroy_dev(fd->masterdev);
1097 static device_method_t fd_methods[] = {
1098 /* Device interface */
1099 DEVMETHOD(device_probe, fd_probe),
1100 DEVMETHOD(device_attach, fd_attach),
1101 DEVMETHOD(device_detach, fd_detach),
1102 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1103 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
1104 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
1109 static driver_t fd_driver = {
1112 sizeof(struct fd_data)
1115 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
1118 * More auxiliary functions.
1121 * Motor control stuff.
1122 * Remember to not deselect the drive we're working on.
1125 set_motor(struct fdc_data *fdc, int fdsu, int turnon)
1128 bus_space_write_1(fdc->sc_fdsiot, fdc->sc_fdsioh, 0,
1129 (pc98_trans != 1 ? FDP_FDDEXC : 0) | FDP_PORTEXC);
1131 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1137 fdout &= ~FDO_FDSEL;
1138 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
1140 fdout &= ~(FDO_MOEN0 << fdsu);
1142 fdout_wr(fdc, fdout);
1143 TRACE1("[0x%x->FDOUT]", fdout);
1148 fd_turnoff(void *xfd)
1153 TRACE1("[fd%d: turnoff]", fd->fdu);
1157 * Don't turn off the motor yet if the drive is active.
1159 * If we got here, this could only mean we missed an interrupt.
1160 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
1161 * after a controller reset. Just schedule a pseudo-interrupt
1162 * so the state machine gets re-entered.
1164 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
1170 fd->flags &= ~FD_MOTOR;
1171 set_motor(fd->fdc, fd->fdsu, TURNOFF);
1176 fd_motor_on(void *xfd)
1182 fd->flags &= ~FD_MOTOR_WAIT;
1183 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
1193 if(!(fd->flags & FD_MOTOR))
1195 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
1196 set_motor(fd->fdc, fd->fdsu, TURNON);
1197 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
1202 fdc_reset(fdc_p fdc)
1204 /* Try a reset, keep motor on */
1207 if (pc98_machine_type & M_EPSON_PC98)
1208 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DD | FDC_MTON);
1210 fdctl_wr(fdc, FDC_RST | FDC_RDY | FDC_DMAE | FDC_MTON);
1212 fdctl_wr(fdc, FDC_DMAE | FDC_MTON);
1215 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1216 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
1218 /* enable FDC, but defer interrupts a moment */
1219 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
1220 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
1222 fdout_wr(fdc, fdc->fdout);
1223 TRACE1("[0x%x->FDOUT]", fdc->fdout);
1226 /* XXX after a reset, silently believe the FDC will accept commands */
1228 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1229 NE7_SPEC_1(4, 240), NE7_SPEC_2(2, 0),
1232 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1233 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1236 if (fdc->flags & FDC_HAS_FIFO)
1237 (void) enable_fifo(fdc);
1241 * FDC IO functions, take care of the main status register, timeout
1242 * in case the desired status bits are never set.
1244 * These PIO loops initially start out with short delays between
1245 * each iteration in the expectation that the required condition
1246 * is usually met quickly, so it can be handled immediately. After
1247 * about 1 ms, stepping is increased to achieve a better timing
1248 * accuracy in the calls to DELAY().
1251 fd_in(struct fdc_data *fdc, int *ptr)
1255 for (j = 0, step = 1;
1256 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) &&
1260 return (fdc_err(fdc, "ready for output in input\n"));
1265 if (j >= FDSTS_TIMEOUT)
1266 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
1269 TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1272 #else /* !FDC_DEBUG */
1277 #endif /* FDC_DEBUG */
1281 out_fdc(struct fdc_data *fdc, int x)
1285 for (j = 0, step = 1;
1286 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM &&
1289 if (i == (NE7_DIO|NE7_RQM))
1290 return (fdc_err(fdc, "ready for input in output\n"));
1295 if (j >= FDSTS_TIMEOUT)
1296 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
1298 /* Send the command and return */
1300 TRACE1("[0x%x->FDDATA]", x);
1305 * Block device driver interface functions (interspersed with even more
1306 * auxiliary functions).
1309 fdopen(struct cdev *dev, int flags, int mode, struct thread *td)
1316 int rv, unitattn, dflags;
1322 if ((fdc == NULL) || (fd->type == FDT_NONE))
1327 dflags = device_get_flags(fd->dev);
1329 * This is a bit bogus. It's still possible that e. g. a
1330 * descriptor gets inherited to a child, but then it's at
1331 * least for the same subdevice. By checking FD_OPEN here, we
1332 * can ensure that a device isn't attempted to be opened with
1333 * different densities at the same time where the second open
1334 * could clobber the settings from the first one.
1336 if (fd->flags & FD_OPEN)
1340 if (pc98_fd_check_ready(fdu) == -1)
1344 if (flags & FNONBLOCK) {
1346 * Unfortunately, physio(9) discards its ioflag
1347 * argument, thus preventing us from seeing the
1348 * O_NONBLOCK bit. So we need to keep track
1351 fd->flags |= FD_NONBLOCK;
1355 * Figure out a unit attention condition.
1357 * If UA has been forced, proceed.
1359 * If the drive has no changeline support,
1360 * or if the drive parameters have been lost
1361 * due to previous non-blocking access,
1362 * assume a forced UA condition.
1364 * If motor is off, turn it on for a moment
1365 * and select our drive, in order to read the
1366 * UA hardware signal.
1368 * If motor is on, and our drive is currently
1369 * selected, just read the hardware bit.
1371 * If motor is on, but active for another
1372 * drive on that controller, we are lost. We
1373 * cannot risk to deselect the other drive, so
1374 * we just assume a forced UA condition to be
1378 if ((dflags & FD_NO_CHLINE) != 0 ||
1379 (fd->flags & FD_UA) != 0 ||
1382 fd->flags &= ~FD_UA;
1384 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 |
1385 FDO_MOEN2 | FDO_MOEN3)) {
1386 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu)
1387 unitattn = fdin_rd(fdc) & FDI_DCHG;
1391 set_motor(fdc, fd->fdsu, TURNON);
1392 unitattn = fdin_rd(fdc) & FDI_DCHG;
1393 set_motor(fdc, fd->fdsu, TURNOFF);
1396 if (unitattn && (rv = fdautoselect(dev)) != 0)
1399 fd->flags |= FD_OPEN;
1401 if ((fdc->flags & FDC_NODMA) == 0) {
1402 if (fdc->dmacnt++ == 0) {
1403 isa_dma_acquire(fdc->dmachan);
1404 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
1409 * Clearing the DMA overrun counter at open time is a bit messy.
1410 * Since we're only managing one counter per controller, opening
1411 * the second drive could mess it up. Anyway, if the DMA overrun
1412 * condition is really persistent, it will eventually time out
1413 * still. OTOH, clearing it here will ensure we'll at least start
1414 * trying again after a previous (maybe even long ago) failure.
1415 * Also, this is merely a stop-gap measure only that should not
1416 * happen during normal operation, so we can tolerate it to be a
1417 * bit sloppy about this.
1419 fdc->dma_overruns = 0;
1425 fdclose(struct cdev *dev, int flags, int mode, struct thread *td)
1432 fd->flags &= ~(FD_OPEN | FD_NONBLOCK);
1433 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1435 if ((fdc->flags & FDC_NODMA) == 0)
1436 if (--fdc->dmacnt == 0)
1437 isa_dma_release(fdc->dmachan);
1443 fdstrategy(struct bio *bp)
1445 long blknum, nblocks;
1452 fd = bp->bio_dev->si_drv1;
1455 bp->bio_resid = bp->bio_bcount;
1456 if (fd->type == FDT_NONE || fd->ft == 0) {
1457 if (fd->type != FDT_NONE && (fd->flags & FD_NONBLOCK))
1458 bp->bio_error = EAGAIN;
1460 bp->bio_error = ENXIO;
1461 bp->bio_flags |= BIO_ERROR;
1464 fdblk = 128 << (fd->ft->secsize);
1465 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) {
1466 if (fd->flags & FD_NONBLOCK) {
1467 bp->bio_error = EAGAIN;
1468 bp->bio_flags |= BIO_ERROR;
1471 if (bp->bio_offset < 0) {
1473 "fd%d: fdstrat: bad request offset = %ju, bcount = %ld\n",
1474 fdu, (intmax_t)bp->bio_offset, bp->bio_bcount);
1475 bp->bio_error = EINVAL;
1476 bp->bio_flags |= BIO_ERROR;
1479 if ((bp->bio_bcount % fdblk) != 0) {
1480 bp->bio_error = EINVAL;
1481 bp->bio_flags |= BIO_ERROR;
1487 * Set up block calculations.
1490 if (bp->bio_offset >= ((off_t)128 << fd->ft->secsize) * fd->ft->size) {
1491 bp->bio_error = EINVAL;
1492 bp->bio_flags |= BIO_ERROR;
1496 blknum = bp->bio_offset / fdblk;
1497 nblocks = fd->ft->size;
1498 if (blknum + bp->bio_bcount / fdblk > nblocks) {
1499 if (blknum >= nblocks) {
1500 if (bp->bio_cmd != BIO_READ) {
1501 bp->bio_error = ENOSPC;
1502 bp->bio_flags |= BIO_ERROR;
1504 goto bad; /* not always bad, but EOF */
1506 bp->bio_bcount = (nblocks - blknum) * fdblk;
1508 bp->bio_pblkno = blknum;
1510 bioq_disksort(&fdc->head, bp);
1511 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1512 devstat_start_transaction_bio(fd->device_stats, bp);
1513 device_busy(fd->dev);
1525 * We have just queued something. If the controller is not busy
1526 * then simulate the case where it has just finished a command
1527 * So that it (the interrupt routine) looks on the queue for more
1528 * work to do and picks up what we just added.
1530 * If the controller is already busy, we need do nothing, as it
1531 * will pick up our work when the present work completes.
1534 fdstart(struct fdc_data *fdc)
1539 if(fdc->state == DEVIDLE)
1547 fd_iotimeout(void *xfdc)
1553 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
1556 * Due to IBM's brain-dead design, the FDC has a faked ready
1557 * signal, hardwired to ready == true. Thus, any command
1558 * issued if there's no diskette in the drive will _never_
1559 * complete, and must be aborted by resetting the FDC.
1560 * Many thanks, Big Blue!
1561 * The FDC must not be reset directly, since that would
1562 * interfere with the state machine. Instead, pretend that
1563 * the command completed but was invalid. The state machine
1564 * will reset the FDC and retry once.
1567 fdc->status[0] = NE7_ST0_IC_IV;
1568 fdc->flags &= ~FDC_STAT_VALID;
1569 fdc->state = IOTIMEDOUT;
1574 /* Just ensure it has the right spl. */
1576 fd_pseudointr(void *xfdc)
1588 * Keep calling the state machine until it returns a 0.
1589 * Always called at splbio.
1592 fdc_intr(void *xfdc)
1600 * Magic pseudo-DMA initialization for YE FDC. Sets count and
1603 #define SET_BCDR(fdc,wr,cnt,port) \
1604 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \
1605 ((cnt)-1) & 0xff); \
1606 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
1607 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
1610 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy.
1613 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
1615 u_char *cptr = (u_char *)addr;
1617 if (flags == BIO_READ) {
1618 if (fdc->state != PIOREAD) {
1619 fdc->state = PIOREAD;
1622 SET_BCDR(fdc, 0, count, 0);
1623 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1624 FDC_YE_DATAPORT, cptr, count);
1626 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
1627 FDC_YE_DATAPORT, cptr, count);
1628 SET_BCDR(fdc, 0, count, 0);
1634 * Try figuring out the density of the media present in our device.
1637 fdautoselect(struct cdev *dev)
1640 struct fd_type *fdtp;
1641 struct fdc_readid id;
1642 int i, n, oopts, rv;
1653 /* no autoselection on those drives */
1654 fd->ft = fd_native_types + fd->type;
1659 fdtp = fd_searchlist_12m;
1660 n = sizeof fd_searchlist_12m / sizeof(struct fd_type);
1664 fdtp = fd_searchlist_144m;
1665 n = sizeof fd_searchlist_144m / sizeof(struct fd_type);
1670 fdtp = fd_searchlist_288m;
1671 n = sizeof fd_searchlist_288m / sizeof(struct fd_type);
1677 * Try reading sector ID fields, first at cylinder 0, head 0,
1678 * then at cylinder 2, head N. We don't probe cylinder 1,
1679 * since for 5.25in DD media in a HD drive, there are no data
1680 * to read (2 step pulses per media cylinder required). For
1681 * two-sided media, the second probe always goes to head 1, so
1682 * we can tell them apart from single-sided media. As a
1683 * side-effect this means that single-sided media should be
1684 * mentioned in the search list after two-sided media of an
1685 * otherwise identical density. Media with a different number
1686 * of sectors per track but otherwise identical parameters
1687 * cannot be distinguished at all.
1689 * If we successfully read an ID field on both cylinders where
1690 * the recorded values match our expectation, we are done.
1691 * Otherwise, we try the next density entry from the table.
1693 * Stepping to cylinder 2 has the side-effect of clearing the
1694 * unit attention bit.
1696 oopts = fd->options;
1697 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1698 for (i = 0; i < n; i++, fdtp++) {
1701 id.cyl = id.head = 0;
1702 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
1705 if (id.cyl != 0 || id.head != 0 ||
1706 id.secshift != fdtp->secsize)
1709 id.head = fd->ft->heads - 1;
1710 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id);
1711 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1712 id.secshift != fdtp->secsize)
1718 fd->options = oopts;
1721 device_printf(fd->dev, "autoselection failed\n");
1726 device_printf(fd->dev, "autoselected %d KB medium\n",
1728 (128 << (fd->ft->secsize)) *
1729 fd->ft->size / 1024);
1739 * The controller state machine.
1741 * If it returns a non zero value, it should be called again immediately.
1746 struct fdc_readid *idp;
1747 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
1748 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac;
1749 unsigned long blknum;
1750 fdu_t fdu = fdc->fdu;
1752 register struct bio *bp;
1753 struct fd_formb *finfo = NULL;
1758 bp = bioq_takefirst(&fdc->head);
1764 * Nothing left for this controller to do,
1765 * force into the IDLE state.
1767 fdc->state = DEVIDLE;
1769 device_printf(fdc->fdc_dev,
1770 "unexpected valid fd pointer\n");
1774 TRACE1("[fdc%d IDLE]", fdc->fdcu);
1777 fd = bp->bio_dev->si_drv1;
1779 fdblk = 128 << fd->ft->secsize;
1780 if (fdc->fd && (fd != fdc->fd))
1781 device_printf(fd->dev, "confused fd pointers\n");
1782 read = bp->bio_cmd == BIO_READ;
1783 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
1784 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
1789 format = bp->bio_cmd == FDBIO_FORMAT;
1790 rdsectid = bp->bio_cmd == FDBIO_RDSECTID;
1792 finfo = (struct fd_formb *)bp->bio_data;
1793 TRACE1("fd%d", fdu);
1794 TRACE1("[%s]", fdstates[fdc->state]);
1795 TRACE1("(0x%x)", fd->flags);
1796 untimeout(fd_turnoff, fd, fd->toffhandle);
1797 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
1801 case FINDWORK: /* we have found new work */
1807 pc98_trans = fd->ft->trans;
1808 if (pc98_trans_prev != pc98_trans) {
1811 for (i = 0; i < 10; i++) {
1815 pc98_trans_prev = pc98_trans;
1817 if (pc98_trans != fd->pc98_trans) {
1818 if (fd->type == FDT_144M) {
1819 bus_space_write_1(fdc->sc_fdemsiot,
1827 fd->pc98_trans = pc98_trans;
1830 fdc->fdctl_wr(fdc, fd->ft->trans);
1832 TRACE1("[0x%x->FDCTL]", fd->ft->trans);
1834 * If the next drive has a motor startup pending, then
1835 * it will start up in its own good time.
1837 if(fd->flags & FD_MOTOR_WAIT) {
1838 fdc->state = MOTORWAIT;
1839 return (0); /* will return later */
1842 * Maybe if it's not starting, it SHOULD be starting.
1844 if (!(fd->flags & FD_MOTOR))
1846 fdc->state = MOTORWAIT;
1848 return (0); /* will return later */
1850 else /* at least make sure we are selected */
1852 set_motor(fdc, fd->fdsu, TURNON);
1854 if (fdc->flags & FDC_NEEDS_RESET) {
1855 fdc->state = RESETCTLR;
1856 fdc->flags &= ~FDC_NEEDS_RESET;
1858 fdc->state = DOSEEK;
1859 return (1); /* will return immediately */
1862 blknum = bp->bio_pblkno + fd->skip / fdblk;
1863 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1864 if (cylinder == fd->track)
1866 fdc->state = SEEKCOMPLETE;
1867 return (1); /* will return immediately */
1870 pc98_fd_check_ready(fdu);
1872 if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1873 fd->fdsu, cylinder * steptrac, 0))
1876 * Seek command not accepted, looks like
1877 * the FDC went off to the Saints...
1879 fdc->retry = 6; /* try a reset */
1880 return(retrier(fdc));
1882 fd->track = FD_NO_TRACK;
1883 fdc->state = SEEKWAIT;
1884 return(0); /* will return later */
1887 /* allow heads to settle */
1888 timeout(fd_pseudointr, fdc, hz / 16);
1889 fdc->state = SEEKCOMPLETE;
1890 return(0); /* will return later */
1892 case SEEKCOMPLETE : /* seek done, start DMA */
1893 blknum = bp->bio_pblkno + fd->skip / fdblk;
1894 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1896 /* Make sure seek really happened. */
1897 if(fd->track == FD_NO_TRACK) {
1898 int descyl = cylinder * steptrac;
1901 * This might be a "ready changed" interrupt,
1902 * which cannot really happen since the
1903 * RDY pin is hardwired to + 5 volts. This
1904 * generally indicates a "bouncing" intr
1905 * line, so do one of the following:
1907 * When running on an enhanced FDC that is
1908 * known to not go stuck after responding
1909 * with INVALID, fetch all interrupt states
1910 * until seeing either an INVALID or a
1911 * real interrupt condition.
1913 * When running on a dumb old NE765, give
1914 * up immediately. The controller will
1915 * provide up to four dummy RC interrupt
1916 * conditions right after reset (for the
1917 * corresponding four drives), so this is
1918 * our only chance to get notice that it
1919 * was not the FDC that caused the interrupt.
1921 if (fd_sense_int(fdc, &st0, &cyl)
1923 return (0); /* will return later */
1924 if(fdc->fdct == FDC_NE765
1925 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1926 return (0); /* hope for a real intr */
1927 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1932 * seek to cyl 0 requested; make sure we are
1935 if (fd_sense_drive_status(fdc, &st3))
1937 if ((st3 & NE7_ST3_T0) == 0) {
1939 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
1940 fdu, st3, NE7_ST3BITS);
1947 return (retrier(fdc));
1951 if (cyl != descyl) {
1953 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
1954 fdu, descyl, cyl, st0);
1957 return (retrier(fdc));
1961 fd->track = cylinder;
1963 fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1965 if (!rdsectid && !(fdc->flags & FDC_NODMA))
1966 isa_dmastart(idf, bp->bio_data+fd->skip,
1967 format ? bp->bio_bcount : fdblk, fdc->dmachan);
1968 blknum = bp->bio_pblkno + fd->skip / fdblk;
1969 sectrac = fd->ft->sectrac;
1970 sec = blknum % (sectrac * fd->ft->heads);
1971 head = sec / sectrac;
1972 sec = sec % sectrac + 1;
1973 if (head != 0 && fd->ft->offset_side2 != 0)
1974 sec += fd->ft->offset_side2;
1975 fd->hddrv = ((head&1)<<2)+fdu;
1977 if(format || !(read || rdsectid))
1979 /* make sure the drive is writable */
1980 if(fd_sense_drive_status(fdc, &st3) != 0)
1982 /* stuck controller? */
1983 if (!(fdc->flags & FDC_NODMA))
1985 bp->bio_data + fd->skip,
1986 format ? bp->bio_bcount : fdblk,
1988 fdc->retry = 6; /* reset the beast */
1989 return (retrier(fdc));
1991 if(st3 & NE7_ST3_WP)
1994 * XXX YES! this is ugly.
1995 * in order to force the current operation
1996 * to fail, we will have to fake an FDC
1997 * error - all error handling is done
2000 fdc->status[0] = NE7_ST0_IC_AT;
2001 fdc->status[1] = NE7_ST1_NW;
2003 fdc->status[3] = fd->track;
2004 fdc->status[4] = head;
2005 fdc->status[5] = sec;
2006 fdc->retry = 8; /* break out immediately */
2007 fdc->state = IOTIMEDOUT; /* not really... */
2008 return (1); /* will return immediately */
2013 ne7cmd = NE7CMD_FORMAT | mfm;
2014 if (fdc->flags & FDC_NODMA) {
2016 * This seems to be necessary for
2017 * whatever obscure reason; if we omit
2018 * it, we end up filling the sector ID
2019 * fields of the newly formatted track
2020 * entirely with garbage, causing
2021 * `wrong cylinder' errors all over
2022 * the place when trying to read them
2027 SET_BCDR(fdc, 1, bp->bio_bcount, 0);
2029 (void)fdcpio(fdc,bp->bio_cmd,
2030 bp->bio_data+fd->skip,
2035 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu,
2036 finfo->fd_formb_secshift,
2037 finfo->fd_formb_nsecs,
2038 finfo->fd_formb_gaplen,
2039 finfo->fd_formb_fillbyte, 0)) {
2040 /* controller fell over */
2041 if (!(fdc->flags & FDC_NODMA))
2043 bp->bio_data + fd->skip,
2044 format ? bp->bio_bcount : fdblk,
2047 return (retrier(fdc));
2049 } else if (rdsectid) {
2050 ne7cmd = NE7CMD_READID | mfm;
2051 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) {
2052 /* controller jamming */
2054 return (retrier(fdc));
2057 /* read or write operation */
2058 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm;
2059 if (fdc->flags & FDC_NODMA) {
2061 * This seems to be necessary even when
2064 SET_BCDR(fdc, 1, fdblk, 0);
2067 * Perform the write pseudo-DMA before
2068 * the WRITE command is sent.
2071 (void)fdcpio(fdc,bp->bio_cmd,
2072 bp->bio_data+fd->skip,
2077 head << 2 | fdu, /* head & unit */
2078 fd->track, /* track */
2080 sec, /* sector + 1 */
2081 fd->ft->secsize, /* sector size */
2082 sectrac, /* sectors/track */
2083 fd->ft->gap, /* gap size */
2084 fd->ft->datalen, /* data length */
2086 /* the beast is sleeping again */
2087 if (!(fdc->flags & FDC_NODMA))
2089 bp->bio_data + fd->skip,
2090 format ? bp->bio_bcount : fdblk,
2093 return (retrier(fdc));
2096 if (!rdsectid && (fdc->flags & FDC_NODMA))
2098 * If this is a read, then simply await interrupt
2099 * before performing PIO.
2101 if (read && !fdcpio(fdc,bp->bio_cmd,
2102 bp->bio_data+fd->skip,fdblk)) {
2103 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2104 return(0); /* will return later */
2108 * Write (or format) operation will fall through and
2109 * await completion interrupt.
2111 fdc->state = IOCOMPLETE;
2112 fd->tohandle = timeout(fd_iotimeout, fdc, hz);
2113 return (0); /* will return later */
2117 * Actually perform the PIO read. The IOCOMPLETE case
2118 * removes the timeout for us.
2120 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
2121 fdc->state = IOCOMPLETE;
2123 case IOCOMPLETE: /* IO done, post-analyze */
2124 untimeout(fd_iotimeout, fdc, fd->tohandle);
2126 if (fd_read_status(fdc)) {
2127 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2128 isa_dmadone(idf, bp->bio_data + fd->skip,
2129 format ? bp->bio_bcount : fdblk,
2132 fdc->retry = 6; /* force a reset */
2133 return (retrier(fdc));
2136 fdc->state = IOTIMEDOUT;
2140 if (!rdsectid && !(fdc->flags & FDC_NODMA))
2141 isa_dmadone(idf, bp->bio_data + fd->skip,
2142 format ? bp->bio_bcount : fdblk, fdc->dmachan);
2143 if (fdc->status[0] & NE7_ST0_IC) {
2144 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2145 && fdc->status[1] & NE7_ST1_OR) {
2147 * DMA overrun. Someone hogged the bus and
2148 * didn't release it in time for the next
2151 * We normally restart this without bumping
2152 * the retry counter. However, in case
2153 * something is seriously messed up (like
2154 * broken hardware), we rather limit the
2155 * number of retries so the IO operation
2156 * doesn't block indefinately.
2158 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2159 fdc->state = SEEKCOMPLETE;
2160 return (1);/* will return immediately */
2161 } /* else fall through */
2163 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
2165 fdc->retry = 6; /* force a reset */
2166 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
2167 && fdc->status[2] & NE7_ST2_WC
2169 fdc->retry = 3; /* force recalibrate */
2170 return (retrier(fdc));
2174 /* copy out ID field contents */
2175 idp = (struct fdc_readid *)bp->bio_data;
2176 idp->cyl = fdc->status[3];
2177 idp->head = fdc->status[4];
2178 idp->sec = fdc->status[5];
2179 idp->secshift = fdc->status[6];
2181 /* Operation successful, retry DMA overruns again next time. */
2182 fdc->dma_overruns = 0;
2184 if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
2185 /* set up next transfer */
2186 fdc->state = DOSEEK;
2192 device_unbusy(fd->dev);
2193 biofinish(bp, fd->device_stats, 0);
2196 fdc->state = FINDWORK;
2198 return (1); /* will return immediately */
2203 fdc->state = RESETCOMPLETE;
2204 return (0); /* will return later */
2208 * Discard all the results from the reset so that they
2209 * can't cause an unexpected interrupt later.
2211 for (i = 0; i < 4; i++)
2212 (void)fd_sense_int(fdc, &st0, &cyl);
2213 fdc->state = STARTRECAL;
2217 pc98_fd_check_ready(fdu);
2219 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2222 return (retrier(fdc));
2224 fdc->state = RECALWAIT;
2225 return (0); /* will return later */
2228 /* allow heads to settle */
2229 timeout(fd_pseudointr, fdc, hz / 8);
2230 fdc->state = RECALCOMPLETE;
2231 return (0); /* will return later */
2236 * See SEEKCOMPLETE for a comment on this:
2238 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2239 return (0); /* will return later */
2240 if(fdc->fdct == FDC_NE765
2241 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2242 return (0); /* hope for a real intr */
2243 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
2244 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
2248 * A recalibrate from beyond cylinder 77
2249 * will "fail" due to the FDC limitations;
2250 * since people used to complain much about
2251 * the failure message, try not logging
2252 * this one if it seems to be the first
2255 printf("fd%d: recal failed ST0 %b cyl %d\n",
2256 fdu, st0, NE7_ST0BITS, cyl);
2257 if(fdc->retry < 3) fdc->retry = 3;
2258 return (retrier(fdc));
2261 /* Seek (probably) necessary */
2262 fdc->state = DOSEEK;
2263 return (1); /* will return immediately */
2266 if(fd->flags & FD_MOTOR_WAIT)
2268 return (0); /* time's not up yet */
2270 if (fdc->flags & FDC_NEEDS_RESET) {
2271 fdc->state = RESETCTLR;
2272 fdc->flags &= ~FDC_NEEDS_RESET;
2274 fdc->state = DOSEEK;
2275 return (1); /* will return immediately */
2278 device_printf(fdc->fdc_dev, "unexpected FD int->");
2279 if (fd_read_status(fdc) == 0)
2280 printf("FDC status :%x %x %x %x %x %x %x ",
2289 printf("No status available ");
2290 if (fd_sense_int(fdc, &st0, &cyl) != 0)
2292 printf("[controller is dead now]\n");
2293 return (0); /* will return later */
2295 printf("ST0 = %x, PCN = %x\n", st0, cyl);
2296 return (0); /* will return later */
2298 /* noone should ever get here */
2302 retrier(struct fdc_data *fdc)
2310 /* XXX shouldn't this be cached somewhere? */
2311 fd = bp->bio_dev->si_drv1;
2313 if (fd->options & FDOPT_NORETRY)
2316 switch (fdc->retry) {
2317 case 0: case 1: case 2:
2318 fdc->state = SEEKCOMPLETE;
2320 case 3: case 4: case 5:
2321 fdc->state = STARTRECAL;
2324 fdc->state = RESETCTLR;
2330 if ((fd->options & FDOPT_NOERRLOG) == 0) {
2331 disk_err(bp, "hard error",
2332 fdc->fd->skip / DEV_BSIZE, 0);
2333 if (fdc->flags & FDC_STAT_VALID) {
2335 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2336 fdc->status[0], NE7_ST0BITS,
2337 fdc->status[1], NE7_ST1BITS,
2338 fdc->status[2], NE7_ST2BITS,
2339 fdc->status[3], fdc->status[4],
2343 printf(" (No status)\n");
2345 if ((fd->options & FDOPT_NOERROR) == 0) {
2346 bp->bio_flags |= BIO_ERROR;
2347 bp->bio_error = EIO;
2348 bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2353 device_unbusy(fd->dev);
2354 biofinish(bp, fdc->fd->device_stats, 0);
2355 fdc->state = FINDWORK;
2356 fdc->flags |= FDC_NEEDS_RESET;
2366 fdbiodone(struct bio *bp)
2372 fdmisccmd(struct cdev *dev, u_int cmd, void *data)
2377 struct fd_formb *finfo;
2378 struct fdc_readid *idfield;
2384 fdblk = 128 << fd->ft->secsize;
2385 finfo = (struct fd_formb *)data;
2386 idfield = (struct fdc_readid *)data;
2388 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
2391 * Set up a bio request for fdstrategy(). bio_offset is faked
2392 * so that fdstrategy() will seek to the the requested
2393 * cylinder, and use the desired head.
2396 if (cmd == FDBIO_FORMAT) {
2398 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2399 finfo->head * fd->ft->sectrac) * fdblk;
2400 bp->bio_bcount = sizeof(struct fd_idfield_data) *
2401 finfo->fd_formb_nsecs;
2402 } else if (cmd == FDBIO_RDSECTID) {
2404 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2405 idfield->head * fd->ft->sectrac) * fdblk;
2406 bp->bio_bcount = sizeof(struct fdc_readid);
2408 panic("wrong cmd in fdmisccmd()");
2409 bp->bio_data = data;
2411 bp->bio_done = fdbiodone;
2414 /* Now run the command. */
2416 error = biowait(bp, "fdcmd");
2423 fdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
2427 struct fdc_status *fsp;
2428 struct fdc_readid *rid;
2435 pc98_fd_check_ready(fdu);
2439 * First, handle everything that could be done with
2440 * FD_NONBLOCK still being set.
2444 case DIOCGMEDIASIZE:
2446 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2447 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size;
2450 case DIOCGSECTORSIZE:
2452 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO);
2453 *(u_int *)addr = 128 << (fd->ft->secsize);
2457 if (*(int *)addr != 0)
2458 fd->flags |= FD_NONBLOCK;
2462 * No drive type has been selected yet,
2463 * cannot turn FNONBLOCK off.
2467 fd->flags &= ~FD_NONBLOCK;
2472 /* keep the generic fcntl() code happy */
2475 case FD_GTYPE: /* get drive type */
2477 /* no type known yet, return the native type */
2478 *(struct fd_type *)addr = fd_native_types[fd->type];
2480 *(struct fd_type *)addr = *fd->ft;
2483 case FD_STYPE: /* set drive type */
2485 * Allow setting drive type temporarily iff
2486 * currently unset. Used for fdformat so any
2487 * user can set it, and then start formatting.
2490 return (EINVAL); /* already set */
2491 fd->fts[0] = *(struct fd_type *)addr;
2492 fd->ft = &fd->fts[0];
2496 case FD_GOPTS: /* get drive options */
2497 *(int *)addr = fd->options + FDOPT_AUTOSEL;
2500 case FD_SOPTS: /* set drive options */
2501 fd->options = *(int *)addr & ~FDOPT_AUTOSEL;
2506 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2507 fd_debug = (*(int *)addr != 0);
2508 printf("fd%d: debugging turned %s\n",
2509 fd->fdu, fd_debug ? "on" : "off");
2517 fd->fdc->fdc_errs = 0;
2521 fsp = (struct fdc_status *)addr;
2522 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2524 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2528 *(enum fd_drivetype *)addr = fd->type;
2533 * Now handle everything else. Make sure we have a valid
2536 if (fd->flags & FD_NONBLOCK)
2545 if ((flag & FWRITE) == 0)
2546 return (EBADF); /* must be opened for writing */
2547 if (((struct fd_formb *)addr)->format_version !=
2549 return (EINVAL); /* wrong version of formatting prog */
2550 error = fdmisccmd(dev, FDBIO_FORMAT, addr);
2553 case FD_GTYPE: /* get drive type */
2554 *(struct fd_type *)addr = *fd->ft;
2557 case FD_STYPE: /* set drive type */
2558 /* this is considered harmful; only allow for superuser */
2561 *fd->ft = *(struct fd_type *)addr;
2564 case FD_GOPTS: /* get drive options */
2565 *(int *)addr = fd->options;
2568 case FD_SOPTS: /* set drive options */
2569 fd->options = *(int *)addr;
2574 if ((fd_debug != 0) != (*(int *)addr != 0)) {
2575 fd_debug = (*(int *)addr != 0);
2576 printf("fd%d: debugging turned %s\n",
2577 fd->fdu, fd_debug ? "on" : "off");
2585 fd->fdc->fdc_errs = 0;
2589 fsp = (struct fdc_status *)addr;
2590 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2592 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
2596 rid = (struct fdc_readid *)addr;
2597 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2599 error = fdmisccmd(dev, FDBIO_RDSECTID, addr);