187f6c662SJulian Elischer /* 25b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California. 35b81b6b3SRodney W. Grimes * All rights reserved. 45b81b6b3SRodney W. Grimes * 55b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by 65b81b6b3SRodney W. Grimes * Don Ahn. 75b81b6b3SRodney W. Grimes * 869acd21dSWarner Losh * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 969acd21dSWarner Losh * aided by the Linux floppy driver modifications from David Bateman 1069acd21dSWarner Losh * (dbateman@eng.uts.edu.au). 1169acd21dSWarner Losh * 12dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994 by 133a2f7427SDavid Greenman * jc@irbs.UUCP (John Capo) 143a2f7427SDavid Greenman * vak@zebub.msk.su (Serge Vakulenko) 153a2f7427SDavid Greenman * ache@astral.msk.su (Andrew A. Chernov) 16dc16046fSJoerg Wunsch * 17dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994, 1995 by 183a2f7427SDavid Greenman * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 19dc5df763SJoerg Wunsch * dufault@hda.com (Peter Dufault) 203a2f7427SDavid Greenman * 212995d110SJoerg Wunsch * Copyright (c) 2001 Joerg Wunsch, 2206740f7bSJoerg Wunsch * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) 232995d110SJoerg Wunsch * 245b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 255b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 265b81b6b3SRodney W. Grimes * are met: 275b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 285b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 295b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 305b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 315b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 3264860614SJoerg Wunsch * 3. All advertising materials mentioning features or use of this software 3364860614SJoerg Wunsch * must display the following acknowledgement: 3464860614SJoerg Wunsch * This product includes software developed by the University of 3564860614SJoerg Wunsch * California, Berkeley and its contributors. 3664860614SJoerg Wunsch * 4. Neither the name of the University nor the names of its contributors 3764860614SJoerg Wunsch * may be used to endorse or promote products derived from this software 3864860614SJoerg Wunsch * without specific prior written permission. 395b81b6b3SRodney W. Grimes * 405b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 415b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 425b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 435b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 445b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 455b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 465b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 475b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 485b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 495b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 505b81b6b3SRodney W. Grimes * SUCH DAMAGE. 515b81b6b3SRodney W. Grimes * 52dc4ff321SRodney W. Grimes * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 53c3aac50fSPeter Wemm * $FreeBSD$ 545b81b6b3SRodney W. Grimes */ 555b81b6b3SRodney W. Grimes 56d2fb4892SJoerg Wunsch #include "opt_fdc.h" 575f830ea2SJoerg Wunsch #include "card.h" 585b81b6b3SRodney W. Grimes 59b99f0a4aSAndrew Moore #include <sys/param.h> 60b99f0a4aSAndrew Moore #include <sys/systm.h> 619626b608SPoul-Henning Kamp #include <sys/bio.h> 626182fdbdSPeter Wemm #include <sys/bus.h> 636182fdbdSPeter Wemm #include <sys/conf.h> 64b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h> 65fb919e4dSMark Murray #include <sys/disklabel.h> 666182fdbdSPeter Wemm #include <sys/fcntl.h> 67e774b251SJoerg Wunsch #include <sys/fdcio.h> 681a6bed68SJoerg Wunsch #include <sys/filio.h> 69fb919e4dSMark Murray #include <sys/kernel.h> 70fb919e4dSMark Murray #include <sys/lock.h> 71b99f0a4aSAndrew Moore #include <sys/malloc.h> 726182fdbdSPeter Wemm #include <sys/module.h> 73fb919e4dSMark Murray #include <sys/mutex.h> 743a2f7427SDavid Greenman #include <sys/proc.h> 75b99f0a4aSAndrew Moore #include <sys/syslog.h> 766182fdbdSPeter Wemm 776182fdbdSPeter Wemm #include <machine/bus.h> 786182fdbdSPeter Wemm #include <sys/rman.h> 796182fdbdSPeter Wemm 806182fdbdSPeter Wemm #include <machine/clock.h> 816182fdbdSPeter Wemm #include <machine/resource.h> 82dc5df763SJoerg Wunsch #include <machine/stdarg.h> 836182fdbdSPeter Wemm 846182fdbdSPeter Wemm #include <isa/isavar.h> 85a97c75b7SDoug Rabson #include <isa/isareg.h> 86a97c75b7SDoug Rabson #include <isa/fdreg.h> 87a97c75b7SDoug Rabson #include <isa/rtc.h> 886182fdbdSPeter Wemm 89246ed35dSJoerg Wunsch enum fdc_type 90246ed35dSJoerg Wunsch { 911a6bed68SJoerg Wunsch FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1 92246ed35dSJoerg Wunsch }; 93246ed35dSJoerg Wunsch 94246ed35dSJoerg Wunsch enum fdc_states { 95246ed35dSJoerg Wunsch DEVIDLE, 96246ed35dSJoerg Wunsch FINDWORK, 97246ed35dSJoerg Wunsch DOSEEK, 98246ed35dSJoerg Wunsch SEEKCOMPLETE , 99246ed35dSJoerg Wunsch IOCOMPLETE, 100246ed35dSJoerg Wunsch RECALCOMPLETE, 101246ed35dSJoerg Wunsch STARTRECAL, 102246ed35dSJoerg Wunsch RESETCTLR, 103246ed35dSJoerg Wunsch SEEKWAIT, 104246ed35dSJoerg Wunsch RECALWAIT, 105246ed35dSJoerg Wunsch MOTORWAIT, 106246ed35dSJoerg Wunsch IOTIMEDOUT, 107246ed35dSJoerg Wunsch RESETCOMPLETE, 108246ed35dSJoerg Wunsch PIOREAD 109246ed35dSJoerg Wunsch }; 110246ed35dSJoerg Wunsch 111246ed35dSJoerg Wunsch #ifdef FDC_DEBUG 112246ed35dSJoerg Wunsch static char const * const fdstates[] = { 113246ed35dSJoerg Wunsch "DEVIDLE", 114246ed35dSJoerg Wunsch "FINDWORK", 115246ed35dSJoerg Wunsch "DOSEEK", 116246ed35dSJoerg Wunsch "SEEKCOMPLETE", 117246ed35dSJoerg Wunsch "IOCOMPLETE", 118246ed35dSJoerg Wunsch "RECALCOMPLETE", 119246ed35dSJoerg Wunsch "STARTRECAL", 120246ed35dSJoerg Wunsch "RESETCTLR", 121246ed35dSJoerg Wunsch "SEEKWAIT", 122246ed35dSJoerg Wunsch "RECALWAIT", 123246ed35dSJoerg Wunsch "MOTORWAIT", 124246ed35dSJoerg Wunsch "IOTIMEDOUT", 125246ed35dSJoerg Wunsch "RESETCOMPLETE", 126246ed35dSJoerg Wunsch "PIOREAD" 127246ed35dSJoerg Wunsch }; 128246ed35dSJoerg Wunsch #endif 129246ed35dSJoerg Wunsch 130246ed35dSJoerg Wunsch /* 131246ed35dSJoerg Wunsch * Per controller structure (softc). 132246ed35dSJoerg Wunsch */ 133246ed35dSJoerg Wunsch struct fdc_data 134246ed35dSJoerg Wunsch { 135246ed35dSJoerg Wunsch int fdcu; /* our unit number */ 136246ed35dSJoerg Wunsch int dmachan; 137246ed35dSJoerg Wunsch int flags; 138246ed35dSJoerg Wunsch #define FDC_ATTACHED 0x01 139246ed35dSJoerg Wunsch #define FDC_STAT_VALID 0x08 140246ed35dSJoerg Wunsch #define FDC_HAS_FIFO 0x10 141246ed35dSJoerg Wunsch #define FDC_NEEDS_RESET 0x20 142246ed35dSJoerg Wunsch #define FDC_NODMA 0x40 143246ed35dSJoerg Wunsch #define FDC_ISPNP 0x80 144246ed35dSJoerg Wunsch #define FDC_ISPCMCIA 0x100 145246ed35dSJoerg Wunsch struct fd_data *fd; 146246ed35dSJoerg Wunsch int fdu; /* the active drive */ 147246ed35dSJoerg Wunsch enum fdc_states state; 148246ed35dSJoerg Wunsch int retry; 149246ed35dSJoerg Wunsch int fdout; /* mirror of the w/o digital output reg */ 150246ed35dSJoerg Wunsch u_int status[7]; /* copy of the registers */ 151246ed35dSJoerg Wunsch enum fdc_type fdct; /* chip version of FDC */ 152246ed35dSJoerg Wunsch int fdc_errs; /* number of logged errors */ 153246ed35dSJoerg Wunsch int dma_overruns; /* number of DMA overruns */ 154246ed35dSJoerg Wunsch struct bio_queue_head head; 155246ed35dSJoerg Wunsch struct bio *bp; /* active buffer */ 156246ed35dSJoerg Wunsch struct resource *res_ioport, *res_ctl, *res_irq, *res_drq; 157246ed35dSJoerg Wunsch int rid_ioport, rid_ctl, rid_irq, rid_drq; 158246ed35dSJoerg Wunsch int port_off; 159246ed35dSJoerg Wunsch bus_space_tag_t portt; 160246ed35dSJoerg Wunsch bus_space_handle_t porth; 161246ed35dSJoerg Wunsch bus_space_tag_t ctlt; 162246ed35dSJoerg Wunsch bus_space_handle_t ctlh; 163246ed35dSJoerg Wunsch void *fdc_intr; 164246ed35dSJoerg Wunsch struct device *fdc_dev; 165246ed35dSJoerg Wunsch void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v); 166246ed35dSJoerg Wunsch }; 167246ed35dSJoerg Wunsch 168d306122dSPoul-Henning Kamp #define BIO_FORMAT BIO_CMD2 169d306122dSPoul-Henning Kamp 170246ed35dSJoerg Wunsch typedef int fdu_t; 171246ed35dSJoerg Wunsch typedef int fdcu_t; 172246ed35dSJoerg Wunsch typedef int fdsu_t; 173246ed35dSJoerg Wunsch typedef struct fd_data *fd_p; 174246ed35dSJoerg Wunsch typedef struct fdc_data *fdc_p; 175246ed35dSJoerg Wunsch typedef enum fdc_type fdc_t; 176246ed35dSJoerg Wunsch 177246ed35dSJoerg Wunsch #define FDUNIT(s) (((s) >> 6) & 3) 1781a6bed68SJoerg Wunsch #define FDNUMTOUNIT(n) (((n) & 3) << 6) 179246ed35dSJoerg Wunsch #define FDTYPE(s) ((s) & 0x3f) 180246ed35dSJoerg Wunsch 181246ed35dSJoerg Wunsch /* 182246ed35dSJoerg Wunsch * fdc maintains a set (1!) of ivars per child of each controller. 183246ed35dSJoerg Wunsch */ 184246ed35dSJoerg Wunsch enum fdc_device_ivars { 185246ed35dSJoerg Wunsch FDC_IVAR_FDUNIT, 186246ed35dSJoerg Wunsch }; 187246ed35dSJoerg Wunsch 188246ed35dSJoerg Wunsch /* 189246ed35dSJoerg Wunsch * Simple access macros for the ivars. 190246ed35dSJoerg Wunsch */ 191246ed35dSJoerg Wunsch #define FDC_ACCESSOR(A, B, T) \ 192246ed35dSJoerg Wunsch static __inline T fdc_get_ ## A(device_t dev) \ 193246ed35dSJoerg Wunsch { \ 194246ed35dSJoerg Wunsch uintptr_t v; \ 195246ed35dSJoerg Wunsch BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \ 196246ed35dSJoerg Wunsch return (T) v; \ 197246ed35dSJoerg Wunsch } 198246ed35dSJoerg Wunsch FDC_ACCESSOR(fdunit, FDUNIT, int) 199246ed35dSJoerg Wunsch 2001a6bed68SJoerg Wunsch /* configuration flags for fdc */ 201e34c71eaSJoerg Wunsch #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 2020722d6abSJoerg Wunsch 203dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 204dc5df763SJoerg Wunsch #define FD_FAILED -1 205dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 206dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 2073fef646eSJoerg Wunsch /* 2083fef646eSJoerg Wunsch * Stop retrying after this many DMA overruns. Since each retry takes 20983edbfa5SJoerg Wunsch * one revolution, with 300 rpm., 25 retries take approximately 5 2103fef646eSJoerg Wunsch * seconds which the read attempt will block in case the DMA overrun 2113fef646eSJoerg Wunsch * is persistent. 2123fef646eSJoerg Wunsch */ 2133fef646eSJoerg Wunsch #define FDC_DMAOV_MAX 25 214dc5df763SJoerg Wunsch 2151a6bed68SJoerg Wunsch /* 216cb38bb6cSJoerg Wunsch * Timeout value for the PIO loops to wait until the FDC main status 217cb38bb6cSJoerg Wunsch * register matches our expectations (request for master, direction 218cb38bb6cSJoerg Wunsch * bit). This is supposed to be a number of microseconds, although 219cb38bb6cSJoerg Wunsch * timing might actually not be very accurate. 220cb38bb6cSJoerg Wunsch * 221cb38bb6cSJoerg Wunsch * Timeouts of 100 msec are believed to be required for some broken 222cb38bb6cSJoerg Wunsch * (old) hardware. 223cb38bb6cSJoerg Wunsch */ 224cb38bb6cSJoerg Wunsch #define FDSTS_TIMEOUT 100000 225cb38bb6cSJoerg Wunsch 226cb38bb6cSJoerg Wunsch /* 2271a6bed68SJoerg Wunsch * Number of subdevices that can be used for different density types. 2281a6bed68SJoerg Wunsch * By now, the lower 6 bit of the minor number are reserved for this, 2291a6bed68SJoerg Wunsch * allowing for up to 64 subdevices, but we only use 16 out of this. 2301a6bed68SJoerg Wunsch * Density #0 is used for automatic format detection, the other 2311a6bed68SJoerg Wunsch * densities are available as programmable densities (for assignment 2321a6bed68SJoerg Wunsch * by fdcontrol(8)). 2331a6bed68SJoerg Wunsch * The upper 2 bits of the minor number are reserved for the subunit 2341a6bed68SJoerg Wunsch * (drive #) per controller. 2351a6bed68SJoerg Wunsch */ 2361a6bed68SJoerg Wunsch #define NUMDENS 16 237b99f0a4aSAndrew Moore 238f664aeeeSJoerg Wunsch #define BIO_RDSECTID BIO_CMD1 2397ca0641bSAndrey A. Chernov 2401a6bed68SJoerg Wunsch /* 2411a6bed68SJoerg Wunsch * List of native drive densities. Order must match enum fd_drivetype 2421a6bed68SJoerg Wunsch * in <sys/fdcio.h>. Upon attaching the drive, each of the 2431a6bed68SJoerg Wunsch * programmable subdevices is initialized with the native density 2441a6bed68SJoerg Wunsch * definition. 2451a6bed68SJoerg Wunsch */ 2461a6bed68SJoerg Wunsch static struct fd_type fd_native_types[] = 2475b81b6b3SRodney W. Grimes { 2481a6bed68SJoerg Wunsch { 0 }, /* FDT_NONE */ 2491a6bed68SJoerg Wunsch { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */ 2501a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */ 2511a6bed68SJoerg Wunsch { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */ 2521a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 2531a6bed68SJoerg Wunsch #if 0 /* we currently don't handle 2.88 MB */ 2541a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/ 2551a6bed68SJoerg Wunsch #else 2561a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 2571a6bed68SJoerg Wunsch #endif 2585b81b6b3SRodney W. Grimes }; 2595b81b6b3SRodney W. Grimes 2601a6bed68SJoerg Wunsch /* 2611a6bed68SJoerg Wunsch * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density 2621a6bed68SJoerg Wunsch * selection, they just start out with their native density (or lose). 2631a6bed68SJoerg Wunsch * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their 2641a6bed68SJoerg Wunsch * respective lists of densities to search for. 2651a6bed68SJoerg Wunsch */ 2661a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = { 2671a6bed68SJoerg Wunsch { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */ 2681a6bed68SJoerg Wunsch { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */ 2691a6bed68SJoerg Wunsch { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 2701a6bed68SJoerg Wunsch }; 2711a6bed68SJoerg Wunsch 2721a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = { 2731a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 2741a6bed68SJoerg Wunsch { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 2751a6bed68SJoerg Wunsch }; 2761a6bed68SJoerg Wunsch 2771a6bed68SJoerg Wunsch /* We search for 1.44M first since this is the most common case. */ 2781a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = { 2791a6bed68SJoerg Wunsch { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 2801a6bed68SJoerg Wunsch #if 0 2811a6bed68SJoerg Wunsch { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */ 2821a6bed68SJoerg Wunsch #endif 2831a6bed68SJoerg Wunsch { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 2841a6bed68SJoerg Wunsch }; 285dc16046fSJoerg Wunsch 286f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE (128 << 3) 28764860614SJoerg Wunsch #define MAX_CYLINDER 85 /* some people really stress their drives 28864860614SJoerg Wunsch * up to cyl 82 */ 289250300ebSJoerg Wunsch #define MAX_HEAD 1 290f8ce7dd5SJoerg Wunsch 2916182fdbdSPeter Wemm static devclass_t fdc_devclass; 2925b81b6b3SRodney W. Grimes 293246ed35dSJoerg Wunsch /* 294246ed35dSJoerg Wunsch * Per drive structure (softc). 295246ed35dSJoerg Wunsch */ 2966182fdbdSPeter Wemm struct fd_data { 297b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 2985b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 2991a6bed68SJoerg Wunsch enum fd_drivetype type; /* drive type */ 3001a6bed68SJoerg Wunsch struct fd_type *ft; /* pointer to current type descriptor */ 3011a6bed68SJoerg Wunsch struct fd_type fts[NUMDENS]; /* type descriptors */ 3025b81b6b3SRodney W. Grimes int flags; 3035b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 3041a6bed68SJoerg Wunsch #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */ 3051a6bed68SJoerg Wunsch #define FD_ACTIVE 0x04 /* it's active */ 3061a6bed68SJoerg Wunsch #define FD_MOTOR 0x08 /* motor should be on */ 3071a6bed68SJoerg Wunsch #define FD_MOTOR_WAIT 0x10 /* motor coming up */ 3081a6bed68SJoerg Wunsch #define FD_UA 0x20 /* force unit attention */ 3095b81b6b3SRodney W. Grimes int skip; 3105b81b6b3SRodney W. Grimes int hddrv; 311dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 3125b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 3131a6bed68SJoerg Wunsch int options; /* user configurable options, see fdcio.h */ 31402a19910SJustin T. Gibbs struct callout_handle toffhandle; 31502a19910SJustin T. Gibbs struct callout_handle tohandle; 316b2dfb1f9SJustin T. Gibbs struct devstat device_stats; 317e219897aSJoerg Wunsch eventhandler_tag clonetag; 318e219897aSJoerg Wunsch dev_t masterdev; 3191a6bed68SJoerg Wunsch dev_t clonedevs[NUMDENS - 1]; 3206182fdbdSPeter Wemm device_t dev; 3216182fdbdSPeter Wemm fdu_t fdu; 3226182fdbdSPeter Wemm }; 32337286586SPeter Wemm 32437286586SPeter Wemm struct fdc_ivars { 32537286586SPeter Wemm int fdunit; 32637286586SPeter Wemm }; 3276182fdbdSPeter Wemm static devclass_t fd_devclass; 3285b81b6b3SRodney W. Grimes 3291a6bed68SJoerg Wunsch /* configuration flags for fd */ 3301a6bed68SJoerg Wunsch #define FD_TYPEMASK 0x0f /* drive type, matches enum 3311a6bed68SJoerg Wunsch * fd_drivetype; on i386 machines, if 3321a6bed68SJoerg Wunsch * given as 0, use RTC type for fd0 3331a6bed68SJoerg Wunsch * and fd1 */ 3341a6bed68SJoerg Wunsch #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK) 3351a6bed68SJoerg Wunsch #define FD_NO_CHLINE 0x10 /* drive does not support changeline 3361a6bed68SJoerg Wunsch * aka. unit attention */ 3371a6bed68SJoerg Wunsch #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just 3381a6bed68SJoerg Wunsch * assume it is there */ 3391a6bed68SJoerg Wunsch 340246ed35dSJoerg Wunsch /* 341246ed35dSJoerg Wunsch * Throughout this file the following conventions will be used: 342246ed35dSJoerg Wunsch * 343246ed35dSJoerg Wunsch * fd is a pointer to the fd_data struct for the drive in question 344246ed35dSJoerg Wunsch * fdc is a pointer to the fdc_data struct for the controller 345246ed35dSJoerg Wunsch * fdu is the floppy drive unit number 346246ed35dSJoerg Wunsch * fdcu is the floppy controller unit number 347246ed35dSJoerg Wunsch * fdsu is the floppy drive unit number on that controller. (sub-unit) 348246ed35dSJoerg Wunsch */ 349b99f0a4aSAndrew Moore 350246ed35dSJoerg Wunsch /* 351246ed35dSJoerg Wunsch * Function declarations, same (chaotic) order as they appear in the 352246ed35dSJoerg Wunsch * file. Re-ordering is too late now, it would only obfuscate the 353246ed35dSJoerg Wunsch * diffs against old and offspring versions (like the PC98 one). 354246ed35dSJoerg Wunsch * 355246ed35dSJoerg Wunsch * Anyone adding functions here, please keep this sequence the same 356246ed35dSJoerg Wunsch * as below -- makes locating a particular function in the body much 357246ed35dSJoerg Wunsch * easier. 358246ed35dSJoerg Wunsch */ 359246ed35dSJoerg Wunsch static void fdout_wr(fdc_p, u_int8_t); 360246ed35dSJoerg Wunsch static u_int8_t fdsts_rd(fdc_p); 361246ed35dSJoerg Wunsch static void fddata_wr(fdc_p, u_int8_t); 362246ed35dSJoerg Wunsch static u_int8_t fddata_rd(fdc_p); 363246ed35dSJoerg Wunsch static void fdctl_wr_isa(fdc_p, u_int8_t); 364246ed35dSJoerg Wunsch #if NCARD > 0 365246ed35dSJoerg Wunsch static void fdctl_wr_pcmcia(fdc_p, u_int8_t); 366246ed35dSJoerg Wunsch #endif 367246ed35dSJoerg Wunsch #if 0 368246ed35dSJoerg Wunsch static u_int8_t fdin_rd(fdc_p); 369246ed35dSJoerg Wunsch #endif 370246ed35dSJoerg Wunsch static int fdc_err(struct fdc_data *, const char *); 371246ed35dSJoerg Wunsch static int fd_cmd(struct fdc_data *, int, ...); 372246ed35dSJoerg Wunsch static int enable_fifo(fdc_p fdc); 373246ed35dSJoerg Wunsch static int fd_sense_drive_status(fdc_p, int *); 374246ed35dSJoerg Wunsch static int fd_sense_int(fdc_p, int *, int *); 375246ed35dSJoerg Wunsch static int fd_read_status(fdc_p); 376246ed35dSJoerg Wunsch static int fdc_alloc_resources(struct fdc_data *); 377246ed35dSJoerg Wunsch static void fdc_release_resources(struct fdc_data *); 378246ed35dSJoerg Wunsch static int fdc_read_ivar(device_t, device_t, int, uintptr_t *); 379246ed35dSJoerg Wunsch static int fdc_probe(device_t); 380246ed35dSJoerg Wunsch #if NCARD > 0 381246ed35dSJoerg Wunsch static int fdc_pccard_probe(device_t); 382246ed35dSJoerg Wunsch #endif 383246ed35dSJoerg Wunsch static int fdc_detach(device_t dev); 384246ed35dSJoerg Wunsch static void fdc_add_child(device_t, const char *, int); 385246ed35dSJoerg Wunsch static int fdc_attach(device_t); 386246ed35dSJoerg Wunsch static int fdc_print_child(device_t, device_t); 387246ed35dSJoerg Wunsch static void fd_clone (void *, char *, int, dev_t *); 388246ed35dSJoerg Wunsch static int fd_probe(device_t); 389246ed35dSJoerg Wunsch static int fd_attach(device_t); 390246ed35dSJoerg Wunsch static int fd_detach(device_t); 3916182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 3923a2f7427SDavid Greenman # define TURNON 1 3933a2f7427SDavid Greenman # define TURNOFF 0 3943a2f7427SDavid Greenman static timeout_t fd_turnoff; 3953a2f7427SDavid Greenman static timeout_t fd_motor_on; 3966182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 3973a2f7427SDavid Greenman static void fdc_reset(fdc_p); 3986182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 39980909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int); 400246ed35dSJoerg Wunsch /* 401246ed35dSJoerg Wunsch * The open function is named Fdopen() to avoid confusion with fdopen() 402246ed35dSJoerg Wunsch * in fd(4). The difference is now only meaningful for debuggers. 403246ed35dSJoerg Wunsch */ 404246ed35dSJoerg Wunsch static d_open_t Fdopen; 405246ed35dSJoerg Wunsch static d_close_t fdclose; 406246ed35dSJoerg Wunsch static d_strategy_t fdstrategy; 4076182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 4085c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 4093a2f7427SDavid Greenman static timeout_t fd_pseudointr; 410246ed35dSJoerg Wunsch static driver_intr_t fdc_intr; 411246ed35dSJoerg Wunsch static int fdcpio(fdc_p, long, caddr_t, u_int); 4121a6bed68SJoerg Wunsch static int fdautoselect(dev_t); 4136182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 4146182fdbdSPeter Wemm static int retrier(struct fdc_data *); 415246ed35dSJoerg Wunsch static void fdbiodone(struct bio *); 416f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *); 417246ed35dSJoerg Wunsch static d_ioctl_t fdioctl; 418d66c374fSTor Egge 419d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 420d66c374fSTor Egge 421d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 4223a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 423cba2a7c6SBruce Evans static int volatile fd_debug = 0; 4240e17a5bcSJoerg Wunsch #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0) 4250e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0) 426d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 4270e17a5bcSJoerg Wunsch #define TRACE0(arg) do { } while (0) 4280e17a5bcSJoerg Wunsch #define TRACE1(arg1, arg2) do { } while (0) 429d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 4305b81b6b3SRodney W. Grimes 431246ed35dSJoerg Wunsch /* 432246ed35dSJoerg Wunsch * Bus space handling (access to low-level IO). 433246ed35dSJoerg Wunsch */ 434427ccf00SDoug Rabson static void 435427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 436427ccf00SDoug Rabson { 437427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 438427ccf00SDoug Rabson } 439427ccf00SDoug Rabson 440427ccf00SDoug Rabson static u_int8_t 441427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 442427ccf00SDoug Rabson { 443427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 444427ccf00SDoug Rabson } 445427ccf00SDoug Rabson 446427ccf00SDoug Rabson static void 447427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 448427ccf00SDoug Rabson { 449427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 450427ccf00SDoug Rabson } 451427ccf00SDoug Rabson 452427ccf00SDoug Rabson static u_int8_t 453427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 454427ccf00SDoug Rabson { 455427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 456427ccf00SDoug Rabson } 457427ccf00SDoug Rabson 458427ccf00SDoug Rabson static void 4595f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 460427ccf00SDoug Rabson { 461427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 462427ccf00SDoug Rabson } 463427ccf00SDoug Rabson 46458c9d623SPeter Wemm #if NCARD > 0 4655f830ea2SJoerg Wunsch static void 4665f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 4675f830ea2SJoerg Wunsch { 4685f830ea2SJoerg Wunsch bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 4695f830ea2SJoerg Wunsch } 47058c9d623SPeter Wemm #endif 4715f830ea2SJoerg Wunsch 472427ccf00SDoug Rabson static u_int8_t 473427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 474427ccf00SDoug Rabson { 475427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 476427ccf00SDoug Rabson } 477427ccf00SDoug Rabson 47887f6c662SJulian Elischer #define CDEV_MAJOR 9 4794e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 4804e2f199eSPoul-Henning Kamp /* open */ Fdopen, 4814e2f199eSPoul-Henning Kamp /* close */ fdclose, 4824e2f199eSPoul-Henning Kamp /* read */ physread, 4834e2f199eSPoul-Henning Kamp /* write */ physwrite, 4844e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 4854e2f199eSPoul-Henning Kamp /* poll */ nopoll, 4864e2f199eSPoul-Henning Kamp /* mmap */ nommap, 4874e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 4884e2f199eSPoul-Henning Kamp /* name */ "fd", 4894e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 4904e2f199eSPoul-Henning Kamp /* dump */ nodump, 4914e2f199eSPoul-Henning Kamp /* psize */ nopsize, 4924e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 4934e2f199eSPoul-Henning Kamp }; 4944e2f199eSPoul-Henning Kamp 495246ed35dSJoerg Wunsch /* 496246ed35dSJoerg Wunsch * Auxiliary functions. Well, some only. Others are scattered 497246ed35dSJoerg Wunsch * throughout the entire file. 498246ed35dSJoerg Wunsch */ 499dc5df763SJoerg Wunsch static int 5006182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 501dc5df763SJoerg Wunsch { 5026182fdbdSPeter Wemm fdc->fdc_errs++; 50316b04b6aSJoerg Wunsch if (s) { 504b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 505b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 506b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 507b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 508b6e5f28eSPeter Wemm "logging any more\n"); 50916b04b6aSJoerg Wunsch } 510dc5df763SJoerg Wunsch 511dc5df763SJoerg Wunsch return FD_FAILED; 512dc5df763SJoerg Wunsch } 513dc5df763SJoerg Wunsch 514dc5df763SJoerg Wunsch /* 515dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 516dc5df763SJoerg Wunsch * Unit number, 517dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 518dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 519dc5df763SJoerg Wunsch */ 5206f4e0bebSPoul-Henning Kamp static int 5216182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 522dc5df763SJoerg Wunsch { 523dc5df763SJoerg Wunsch u_char cmd; 524dc5df763SJoerg Wunsch int n_in; 525dc5df763SJoerg Wunsch int n; 526dc5df763SJoerg Wunsch va_list ap; 527dc5df763SJoerg Wunsch 528dc5df763SJoerg Wunsch va_start(ap, n_out); 529dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 530dc5df763SJoerg Wunsch va_end(ap); 531dc5df763SJoerg Wunsch va_start(ap, n_out); 532dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 533dc5df763SJoerg Wunsch { 5346182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 535dc5df763SJoerg Wunsch { 536dc5df763SJoerg Wunsch char msg[50]; 5372127f260SArchie Cobbs snprintf(msg, sizeof(msg), 538dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 539dc5df763SJoerg Wunsch cmd, n + 1, n_out); 5406182fdbdSPeter Wemm return fdc_err(fdc, msg); 541dc5df763SJoerg Wunsch } 542dc5df763SJoerg Wunsch } 543dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 544dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 545dc5df763SJoerg Wunsch { 546dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 5476182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 548dc5df763SJoerg Wunsch { 549dc5df763SJoerg Wunsch char msg[50]; 5502127f260SArchie Cobbs snprintf(msg, sizeof(msg), 551dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 552dc5df763SJoerg Wunsch cmd, n + 1, n_in); 5536182fdbdSPeter Wemm return fdc_err(fdc, msg); 554dc5df763SJoerg Wunsch } 555dc5df763SJoerg Wunsch } 556dc5df763SJoerg Wunsch 557dc5df763SJoerg Wunsch return 0; 558dc5df763SJoerg Wunsch } 559dc5df763SJoerg Wunsch 5606f4e0bebSPoul-Henning Kamp static int 561d66c374fSTor Egge enable_fifo(fdc_p fdc) 562d66c374fSTor Egge { 563d66c374fSTor Egge int i, j; 564d66c374fSTor Egge 565d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 566d66c374fSTor Egge 567d66c374fSTor Egge /* 568d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 569d66c374fSTor Egge * this might be an invalid command. Thus we send the 570d66c374fSTor Egge * first byte, and check for an early turn of data directon. 571d66c374fSTor Egge */ 572d66c374fSTor Egge 5736182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 5746182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 575d66c374fSTor Egge 576d66c374fSTor Egge /* If command is invalid, return */ 577d0900d6bSJoerg Wunsch j = FDSTS_TIMEOUT; 578427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 579d0900d6bSJoerg Wunsch != NE7_RQM && j-- > 0) { 580d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 581d66c374fSTor Egge fdc_reset(fdc); 582d66c374fSTor Egge return FD_FAILED; 583d66c374fSTor Egge } 584d0900d6bSJoerg Wunsch DELAY(1); 585d0900d6bSJoerg Wunsch } 586d66c374fSTor Egge if (j<0 || 5876182fdbdSPeter Wemm fd_cmd(fdc, 3, 588d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 589d66c374fSTor Egge fdc_reset(fdc); 5906182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 591d66c374fSTor Egge } 592d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 593d66c374fSTor Egge return 0; 594d66c374fSTor Egge } 5956182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 596d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 5976182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 598d66c374fSTor Egge return 0; 599d66c374fSTor Egge } 600d66c374fSTor Egge 601d66c374fSTor Egge static int 602dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 603dc5df763SJoerg Wunsch { 604dc5df763SJoerg Wunsch int st3; 605dc5df763SJoerg Wunsch 6066182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 607dc5df763SJoerg Wunsch { 6086182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 609dc5df763SJoerg Wunsch } 610dc5df763SJoerg Wunsch if (st3p) 611dc5df763SJoerg Wunsch *st3p = st3; 612dc5df763SJoerg Wunsch 613dc5df763SJoerg Wunsch return 0; 614dc5df763SJoerg Wunsch } 615dc5df763SJoerg Wunsch 6166f4e0bebSPoul-Henning Kamp static int 617dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 618dc5df763SJoerg Wunsch { 6196182fdbdSPeter Wemm int cyl, st0, ret; 620dc5df763SJoerg Wunsch 6216182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 6226182fdbdSPeter Wemm if (ret) { 6236182fdbdSPeter Wemm (void)fdc_err(fdc, 624dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 625dc5df763SJoerg Wunsch return ret; 626dc5df763SJoerg Wunsch } 627dc5df763SJoerg Wunsch 628dc5df763SJoerg Wunsch if (st0p) 629dc5df763SJoerg Wunsch *st0p = st0; 630dc5df763SJoerg Wunsch 6316182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 632dc5df763SJoerg Wunsch /* 633dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 634dc5df763SJoerg Wunsch */ 635dc5df763SJoerg Wunsch return FD_NOT_VALID; 636dc5df763SJoerg Wunsch } 637dc5df763SJoerg Wunsch 6386182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 6396182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 640dc5df763SJoerg Wunsch } 641dc5df763SJoerg Wunsch 642dc5df763SJoerg Wunsch if (cylp) 643dc5df763SJoerg Wunsch *cylp = cyl; 644dc5df763SJoerg Wunsch 645dc5df763SJoerg Wunsch return 0; 646dc5df763SJoerg Wunsch } 647dc5df763SJoerg Wunsch 648dc5df763SJoerg Wunsch 6496f4e0bebSPoul-Henning Kamp static int 65064860614SJoerg Wunsch fd_read_status(fdc_p fdc) 651dc5df763SJoerg Wunsch { 652dc5df763SJoerg Wunsch int i, ret; 653b5e8ce9fSBruce Evans 654246ed35dSJoerg Wunsch for (i = ret = 0; i < 7; i++) { 655b5e8ce9fSBruce Evans /* 65664860614SJoerg Wunsch * XXX types are poorly chosen. Only bytes can be read 657a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 658b5e8ce9fSBruce Evans * fd_in() gives ints. 659b5e8ce9fSBruce Evans */ 660b5e8ce9fSBruce Evans int status; 661b5e8ce9fSBruce Evans 6626182fdbdSPeter Wemm ret = fd_in(fdc, &status); 663b5e8ce9fSBruce Evans fdc->status[i] = status; 664b5e8ce9fSBruce Evans if (ret != 0) 665dc5df763SJoerg Wunsch break; 666dc5df763SJoerg Wunsch } 667dc5df763SJoerg Wunsch 668dc5df763SJoerg Wunsch if (ret == 0) 669dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 670dc5df763SJoerg Wunsch else 671dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 672dc5df763SJoerg Wunsch 673dc5df763SJoerg Wunsch return ret; 674dc5df763SJoerg Wunsch } 675dc5df763SJoerg Wunsch 6763a2f7427SDavid Greenman static int 67737286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 6785b81b6b3SRodney W. Grimes { 67937286586SPeter Wemm device_t dev; 680e219897aSJoerg Wunsch int ispnp, ispcmcia, nports; 6815b81b6b3SRodney W. Grimes 68237286586SPeter Wemm dev = fdc->fdc_dev; 6835f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 6845f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 6856182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 6866182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 6876182fdbdSPeter Wemm 688b6e5f28eSPeter Wemm /* 6895f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 6905f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 6915f830ea2SJoerg Wunsch * register at 0x3f6. 6925f830ea2SJoerg Wunsch * 693b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 6945f830ea2SJoerg Wunsch * 6955f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 6965f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 6975f830ea2SJoerg Wunsch * one with offset 7 as control register. 698b6e5f28eSPeter Wemm */ 699e219897aSJoerg Wunsch nports = ispcmcia ? 8 : (ispnp ? 1 : 6); 7006182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 7016182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 702e219897aSJoerg Wunsch nports, RF_ACTIVE); 7036182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 704e219897aSJoerg Wunsch device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 705e219897aSJoerg Wunsch nports); 70637286586SPeter Wemm return ENXIO; 7076182fdbdSPeter Wemm } 708427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 709427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 710427ccf00SDoug Rabson 7115f830ea2SJoerg Wunsch if (!ispcmcia) { 712427ccf00SDoug Rabson /* 7135f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 7145f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 7155f830ea2SJoerg Wunsch * by checking the size and adjust the port address 7165f830ea2SJoerg Wunsch * accordingly. 717b6e5f28eSPeter Wemm */ 718b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 719b6e5f28eSPeter Wemm fdc->port_off = -2; 720b6e5f28eSPeter Wemm 721b6e5f28eSPeter Wemm /* 7225f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 7235f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 7245f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 725a2639a18SPeter Wemm * 7265f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 7275f830ea2SJoerg Wunsch * leave out the control register! It seems that some 7285f830ea2SJoerg Wunsch * non-antique controller chips have a different 7295f830ea2SJoerg Wunsch * method of programming the transfer speed which 7305f830ea2SJoerg Wunsch * doesn't require the control register, but it's 7315f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 7325f830ea2SJoerg Wunsch * address for the control register. 733427ccf00SDoug Rabson */ 734b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 735b9da888fSPeter Wemm u_long ctlstart; 736b9da888fSPeter Wemm 737b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 7385f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 7395f830ea2SJoerg Wunsch fdc->port_off + 7; 740b6e5f28eSPeter Wemm 741b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 742b9da888fSPeter Wemm } 743b6e5f28eSPeter Wemm 744b6e5f28eSPeter Wemm /* 745b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 746b6e5f28eSPeter Wemm */ 747427ccf00SDoug Rabson fdc->rid_ctl = 1; 7485f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 7495f830ea2SJoerg Wunsch &fdc->rid_ctl, 750b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 751427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 7525f830ea2SJoerg Wunsch device_printf(dev, 753e219897aSJoerg Wunsch "cannot reserve control I/O port range (control port)\n"); 75437286586SPeter Wemm return ENXIO; 755427ccf00SDoug Rabson } 756427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 757427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 7585f830ea2SJoerg Wunsch } 7596182fdbdSPeter Wemm 7606182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 7616182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 7626182fdbdSPeter Wemm RF_ACTIVE); 7636182fdbdSPeter Wemm if (fdc->res_irq == 0) { 764427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 76537286586SPeter Wemm return ENXIO; 7666182fdbdSPeter Wemm } 7675f830ea2SJoerg Wunsch 7685f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 7696182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 7706182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 7716182fdbdSPeter Wemm RF_ACTIVE); 7726182fdbdSPeter Wemm if (fdc->res_drq == 0) { 773427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 77437286586SPeter Wemm return ENXIO; 7756182fdbdSPeter Wemm } 7766182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 7775f830ea2SJoerg Wunsch } 77837286586SPeter Wemm 77937286586SPeter Wemm return 0; 78037286586SPeter Wemm } 78137286586SPeter Wemm 78237286586SPeter Wemm static void 78337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 78437286586SPeter Wemm { 78537286586SPeter Wemm device_t dev; 78637286586SPeter Wemm 78737286586SPeter Wemm dev = fdc->fdc_dev; 78837286586SPeter Wemm if (fdc->res_irq != 0) { 78937286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 79037286586SPeter Wemm fdc->res_irq); 79137286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 79237286586SPeter Wemm fdc->res_irq); 79337286586SPeter Wemm } 79437286586SPeter Wemm if (fdc->res_ctl != 0) { 79537286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 79637286586SPeter Wemm fdc->res_ctl); 79737286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 79837286586SPeter Wemm fdc->res_ctl); 79937286586SPeter Wemm } 80037286586SPeter Wemm if (fdc->res_ioport != 0) { 80137286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 80237286586SPeter Wemm fdc->res_ioport); 80337286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 80437286586SPeter Wemm fdc->res_ioport); 80537286586SPeter Wemm } 80637286586SPeter Wemm if (fdc->res_drq != 0) { 80737286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 80837286586SPeter Wemm fdc->res_drq); 80937286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 81037286586SPeter Wemm fdc->res_drq); 81137286586SPeter Wemm } 81237286586SPeter Wemm } 81337286586SPeter Wemm 814246ed35dSJoerg Wunsch /* 815246ed35dSJoerg Wunsch * Configuration/initialization stuff, per controller. 816246ed35dSJoerg Wunsch */ 81737286586SPeter Wemm 81837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 81937286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 82037286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 82137286586SPeter Wemm {0} 82237286586SPeter Wemm }; 82337286586SPeter Wemm 82437286586SPeter Wemm static int 8253aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 82637286586SPeter Wemm { 82737286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 82837286586SPeter Wemm 82937286586SPeter Wemm switch (which) { 83037286586SPeter Wemm case FDC_IVAR_FDUNIT: 83137286586SPeter Wemm *result = ivars->fdunit; 83237286586SPeter Wemm break; 83337286586SPeter Wemm default: 83437286586SPeter Wemm return ENOENT; 83537286586SPeter Wemm } 83637286586SPeter Wemm return 0; 83737286586SPeter Wemm } 83837286586SPeter Wemm 83937286586SPeter Wemm static int 84037286586SPeter Wemm fdc_probe(device_t dev) 84137286586SPeter Wemm { 84237286586SPeter Wemm int error, ic_type; 84337286586SPeter Wemm struct fdc_data *fdc; 84437286586SPeter Wemm 84537286586SPeter Wemm fdc = device_get_softc(dev); 84637286586SPeter Wemm bzero(fdc, sizeof *fdc); 84737286586SPeter Wemm fdc->fdc_dev = dev; 8485f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 84937286586SPeter Wemm 85037286586SPeter Wemm /* Check pnp ids */ 85137286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 85237286586SPeter Wemm if (error == ENXIO) 85337286586SPeter Wemm return ENXIO; 8545f830ea2SJoerg Wunsch if (error == 0) 8555f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 85637286586SPeter Wemm 85737286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 85837286586SPeter Wemm error = fdc_alloc_resources(fdc); 85937286586SPeter Wemm if (error) 86037286586SPeter Wemm goto out; 8615b81b6b3SRodney W. Grimes 86216111cedSAndrew Moore /* First - lets reset the floppy controller */ 863427ccf00SDoug Rabson fdout_wr(fdc, 0); 86416111cedSAndrew Moore DELAY(100); 865427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 86616111cedSAndrew Moore 8675b81b6b3SRodney W. Grimes /* see if it can handle a command */ 8686182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 8696182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 8706182fdbdSPeter Wemm error = ENXIO; 8716182fdbdSPeter Wemm goto out; 8725b81b6b3SRodney W. Grimes } 8736182fdbdSPeter Wemm 8746182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 8756182fdbdSPeter Wemm ic_type = (u_char)ic_type; 8766182fdbdSPeter Wemm switch (ic_type) { 8776182fdbdSPeter Wemm case 0x80: 8786182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 8796182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 8806182fdbdSPeter Wemm break; 8811a6bed68SJoerg Wunsch case 0x81: /* not mentioned in any hardware doc */ 8826182fdbdSPeter Wemm case 0x90: 8831a6bed68SJoerg Wunsch device_set_desc(dev, 8841a6bed68SJoerg Wunsch "enhanced floppy controller (i82077, NE72065 or clone)"); 8851a6bed68SJoerg Wunsch fdc->fdct = FDC_ENHANCED; 8866182fdbdSPeter Wemm break; 8876182fdbdSPeter Wemm default: 8886182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 8896182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 8906182fdbdSPeter Wemm break; 8916182fdbdSPeter Wemm } 8926182fdbdSPeter Wemm } 8936182fdbdSPeter Wemm 8946182fdbdSPeter Wemm out: 89537286586SPeter Wemm fdc_release_resources(fdc); 8966182fdbdSPeter Wemm return (error); 8975b81b6b3SRodney W. Grimes } 8985b81b6b3SRodney W. Grimes 8995f830ea2SJoerg Wunsch #if NCARD > 0 9005f830ea2SJoerg Wunsch 9015f830ea2SJoerg Wunsch static int 9025f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 9035f830ea2SJoerg Wunsch { 9045f830ea2SJoerg Wunsch int error; 9055f830ea2SJoerg Wunsch struct fdc_data *fdc; 9065f830ea2SJoerg Wunsch 9075f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 9085f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 9095f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 9105f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 9115f830ea2SJoerg Wunsch 9125f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 9135f830ea2SJoerg Wunsch 9145f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 9155f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 9165f830ea2SJoerg Wunsch if (error) 9175f830ea2SJoerg Wunsch goto out; 9185f830ea2SJoerg Wunsch 9195f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 9205f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 9215f830ea2SJoerg Wunsch DELAY(100); 9225f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 9235f830ea2SJoerg Wunsch 9245f830ea2SJoerg Wunsch /* see if it can handle a command */ 9255f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 9265f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 9275f830ea2SJoerg Wunsch error = ENXIO; 9285f830ea2SJoerg Wunsch goto out; 9295f830ea2SJoerg Wunsch } 9305f830ea2SJoerg Wunsch 9315f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 9325f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 9335f830ea2SJoerg Wunsch 9345f830ea2SJoerg Wunsch out: 9355f830ea2SJoerg Wunsch fdc_release_resources(fdc); 9365f830ea2SJoerg Wunsch return (error); 9375f830ea2SJoerg Wunsch } 9385f830ea2SJoerg Wunsch 939e219897aSJoerg Wunsch #endif /* NCARD > 0 */ 940e219897aSJoerg Wunsch 9415f830ea2SJoerg Wunsch static int 942e219897aSJoerg Wunsch fdc_detach(device_t dev) 9435f830ea2SJoerg Wunsch { 9445f830ea2SJoerg Wunsch struct fdc_data *fdc; 9455f830ea2SJoerg Wunsch int error; 9465f830ea2SJoerg Wunsch 9475f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 9485f830ea2SJoerg Wunsch 9495f830ea2SJoerg Wunsch /* have our children detached first */ 9505f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 9515f830ea2SJoerg Wunsch return (error); 9525f830ea2SJoerg Wunsch 953e219897aSJoerg Wunsch /* reset controller, turn motor off */ 954e219897aSJoerg Wunsch fdout_wr(fdc, 0); 955e219897aSJoerg Wunsch 956e219897aSJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) 957e219897aSJoerg Wunsch isa_dma_release(fdc->dmachan); 958e219897aSJoerg Wunsch 9595f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 9605f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 9615f830ea2SJoerg Wunsch return (0); 9625f830ea2SJoerg Wunsch } 9635f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 9645f830ea2SJoerg Wunsch 9655f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 9665f830ea2SJoerg Wunsch fdc->fdc_intr); 9675f830ea2SJoerg Wunsch fdc_release_resources(fdc); 9685f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 9695f830ea2SJoerg Wunsch return (0); 9705f830ea2SJoerg Wunsch } 9715f830ea2SJoerg Wunsch 9725b81b6b3SRodney W. Grimes /* 97337286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc. 9745b81b6b3SRodney W. Grimes */ 9756182fdbdSPeter Wemm static void 97637286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit) 9775b81b6b3SRodney W. Grimes { 9781a6bed68SJoerg Wunsch int disabled, flags; 97937286586SPeter Wemm struct fdc_ivars *ivar; 9806182fdbdSPeter Wemm device_t child; 98192200632SGarrett Wollman 9827cc0979fSDavid Malone ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 98337286586SPeter Wemm if (ivar == NULL) 9846182fdbdSPeter Wemm return; 98537286586SPeter Wemm if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 98637286586SPeter Wemm ivar->fdunit = 0; 987fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit); 98837286586SPeter Wemm if (child == NULL) 9896182fdbdSPeter Wemm return; 99037286586SPeter Wemm device_set_ivars(child, ivar); 9911a6bed68SJoerg Wunsch if (resource_int_value(name, unit, "flags", &flags) == 0) 9921a6bed68SJoerg Wunsch device_set_flags(child, flags); 993a97c75b7SDoug Rabson if (resource_int_value(name, unit, "disabled", &disabled) == 0 994a97c75b7SDoug Rabson && disabled != 0) 9956182fdbdSPeter Wemm device_disable(child); 9966182fdbdSPeter Wemm } 9976182fdbdSPeter Wemm 9986182fdbdSPeter Wemm static int 9996182fdbdSPeter Wemm fdc_attach(device_t dev) 10006182fdbdSPeter Wemm { 100137286586SPeter Wemm struct fdc_data *fdc; 10022398f0cdSPeter Wemm const char *name, *dname; 10035d54fe91SJoerg Wunsch int i, error, dunit; 1004427ccf00SDoug Rabson 100537286586SPeter Wemm fdc = device_get_softc(dev); 100637286586SPeter Wemm error = fdc_alloc_resources(fdc); 100737286586SPeter Wemm if (error) { 1008f8ce7dd5SJoerg Wunsch device_printf(dev, "cannot re-acquire resources\n"); 100937286586SPeter Wemm return error; 101037286586SPeter Wemm } 101137286586SPeter Wemm error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 10123c36743eSMark Murray INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 10133c36743eSMark Murray &fdc->fdc_intr); 101437286586SPeter Wemm if (error) { 101537286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n"); 101637286586SPeter Wemm return error; 101737286586SPeter Wemm } 101837286586SPeter Wemm fdc->fdcu = device_get_unit(dev); 1019fb35bd37SJoerg Wunsch fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET; 10206182fdbdSPeter Wemm 10215f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 1022f8ce7dd5SJoerg Wunsch /* 1023f8ce7dd5SJoerg Wunsch * Acquire the DMA channel forever, the driver will do 1024f8ce7dd5SJoerg Wunsch * the rest 1025f8ce7dd5SJoerg Wunsch * XXX should integrate with rman 1026f8ce7dd5SJoerg Wunsch */ 1027100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 1028f8ce7dd5SJoerg Wunsch isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 10295f830ea2SJoerg Wunsch } 10305b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 10316182fdbdSPeter Wemm 10323a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 103364860614SJoerg Wunsch fdout_wr(fdc, fdc->fdout = 0); 10348177437dSPoul-Henning Kamp bioq_init(&fdc->head); 10355b81b6b3SRodney W. Grimes 10366182fdbdSPeter Wemm /* 103737286586SPeter Wemm * Probe and attach any children. We should probably detect 103837286586SPeter Wemm * devices from the BIOS unless overridden. 10396182fdbdSPeter Wemm */ 1040ada54f9eSPeter Wemm name = device_get_nameunit(dev); 10412398f0cdSPeter Wemm i = 0; 10422398f0cdSPeter Wemm while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 10432398f0cdSPeter Wemm fdc_add_child(dev, dname, dunit); 104437286586SPeter Wemm 104560444853SJoerg Wunsch if ((error = bus_generic_attach(dev)) != 0) 104660444853SJoerg Wunsch return (error); 104760444853SJoerg Wunsch 104860444853SJoerg Wunsch return (0); 10496182fdbdSPeter Wemm } 10506182fdbdSPeter Wemm 105115317dd8SMatthew N. Dodd static int 10526182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 10536182fdbdSPeter Wemm { 10541a6bed68SJoerg Wunsch int retval = 0, flags; 105515317dd8SMatthew N. Dodd 105615317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 10571a6bed68SJoerg Wunsch retval += printf(" on %s drive %d", device_get_nameunit(me), 105837286586SPeter Wemm fdc_get_fdunit(child)); 10591a6bed68SJoerg Wunsch if ((flags = device_get_flags(me)) != 0) 10601a6bed68SJoerg Wunsch retval += printf(" flags %#x", flags); 10611a6bed68SJoerg Wunsch retval += printf("\n"); 106215317dd8SMatthew N. Dodd 106315317dd8SMatthew N. Dodd return (retval); 10646182fdbdSPeter Wemm } 10656182fdbdSPeter Wemm 106616e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 106716e68fc6SPeter Wemm /* Device interface */ 106816e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 106916e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 1070e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 107116e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 107216e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 107316e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 107416e68fc6SPeter Wemm 107516e68fc6SPeter Wemm /* Bus interface */ 107616e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 107737286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 107816e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 107916e68fc6SPeter Wemm 108016e68fc6SPeter Wemm { 0, 0 } 108116e68fc6SPeter Wemm }; 108216e68fc6SPeter Wemm 108316e68fc6SPeter Wemm static driver_t fdc_driver = { 108416e68fc6SPeter Wemm "fdc", 108516e68fc6SPeter Wemm fdc_methods, 108616e68fc6SPeter Wemm sizeof(struct fdc_data) 108716e68fc6SPeter Wemm }; 108816e68fc6SPeter Wemm 108916e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 10905f063c7bSMike Smith DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0); 109116e68fc6SPeter Wemm 10925f830ea2SJoerg Wunsch #if NCARD > 0 10935f830ea2SJoerg Wunsch 10945f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 10955f830ea2SJoerg Wunsch /* Device interface */ 10965f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 10975f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 1098e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 10995f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 11005f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 11015f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 11025f830ea2SJoerg Wunsch 11035f830ea2SJoerg Wunsch /* Bus interface */ 11045f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 11055f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 11065f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 11075f830ea2SJoerg Wunsch 11085f830ea2SJoerg Wunsch { 0, 0 } 11095f830ea2SJoerg Wunsch }; 11105f830ea2SJoerg Wunsch 11115f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 11125f830ea2SJoerg Wunsch "fdc", 11135f830ea2SJoerg Wunsch fdc_pccard_methods, 11145f830ea2SJoerg Wunsch sizeof(struct fdc_data) 11155f830ea2SJoerg Wunsch }; 11165f830ea2SJoerg Wunsch 11175f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 11185f830ea2SJoerg Wunsch 11195f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 11205f830ea2SJoerg Wunsch 112160444853SJoerg Wunsch /* 11221a6bed68SJoerg Wunsch * Create a clone device upon request by devfs. 112360444853SJoerg Wunsch */ 11243f54a085SPoul-Henning Kamp static void 112580909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev) 11263f54a085SPoul-Henning Kamp { 112760444853SJoerg Wunsch struct fd_data *fd; 11281a6bed68SJoerg Wunsch int i, u; 11293f54a085SPoul-Henning Kamp char *n; 11301a6bed68SJoerg Wunsch size_t l; 11313f54a085SPoul-Henning Kamp 113260444853SJoerg Wunsch fd = (struct fd_data *)arg; 11333f54a085SPoul-Henning Kamp if (*dev != NODEV) 11343f54a085SPoul-Henning Kamp return; 1135db901281SPoul-Henning Kamp if (dev_stdclone(name, &n, "fd", &u) != 2) 11363f54a085SPoul-Henning Kamp return; 11371a6bed68SJoerg Wunsch l = strlen(n); 11381a6bed68SJoerg Wunsch if (l == 1 && *n >= 'a' && *n <= 'h') { 11391a6bed68SJoerg Wunsch /* 11401a6bed68SJoerg Wunsch * Trailing letters a through h denote 11411a6bed68SJoerg Wunsch * pseudo-partitions. We don't support true 11421a6bed68SJoerg Wunsch * (UFS-style) partitions, so we just implement them 11431a6bed68SJoerg Wunsch * as symlinks if someone asks us nicely. 11441a6bed68SJoerg Wunsch */ 114560444853SJoerg Wunsch *dev = make_dev_alias(fd->masterdev, name); 11461a6bed68SJoerg Wunsch return; 11471a6bed68SJoerg Wunsch } 11481a6bed68SJoerg Wunsch if (l >= 2 && l <= 5 && *n == '.') { 11491a6bed68SJoerg Wunsch /* 11501a6bed68SJoerg Wunsch * Trailing numbers, preceded by a dot, denote 11511a6bed68SJoerg Wunsch * subdevices for different densities. Historically, 11521a6bed68SJoerg Wunsch * they have been named by density (like fd0.1440), 11531a6bed68SJoerg Wunsch * but we allow arbitrary numbers between 1 and 4 11541a6bed68SJoerg Wunsch * digits, so fd0.1 through fd0.15 are possible as 11551a6bed68SJoerg Wunsch * well. 11561a6bed68SJoerg Wunsch */ 11571a6bed68SJoerg Wunsch for (i = 1; i < l; i++) 11581a6bed68SJoerg Wunsch if (n[i] < '0' || n[i] > '9') 11591a6bed68SJoerg Wunsch return; 11601a6bed68SJoerg Wunsch for (i = 0; i < NUMDENS - 1; i++) 11611a6bed68SJoerg Wunsch if (fd->clonedevs[i] == NODEV) { 11621a6bed68SJoerg Wunsch *dev = make_dev(&fd_cdevsw, 11631a6bed68SJoerg Wunsch FDNUMTOUNIT(u) + i + 1, 11641a6bed68SJoerg Wunsch UID_ROOT, GID_OPERATOR, 0640, 11651a6bed68SJoerg Wunsch name); 11661a6bed68SJoerg Wunsch fd->clonedevs[i] = *dev; 11671a6bed68SJoerg Wunsch return; 11681a6bed68SJoerg Wunsch } 11693f54a085SPoul-Henning Kamp } 11703f54a085SPoul-Henning Kamp } 11713f54a085SPoul-Henning Kamp 117216e68fc6SPeter Wemm /* 1173246ed35dSJoerg Wunsch * Configuration/initialization, per drive. 117416e68fc6SPeter Wemm */ 11756182fdbdSPeter Wemm static int 11766182fdbdSPeter Wemm fd_probe(device_t dev) 11776182fdbdSPeter Wemm { 11786182fdbdSPeter Wemm int i; 11791a6bed68SJoerg Wunsch u_int st0, st3; 11806182fdbdSPeter Wemm struct fd_data *fd; 11816182fdbdSPeter Wemm struct fdc_data *fdc; 11826182fdbdSPeter Wemm fdsu_t fdsu; 11831a6bed68SJoerg Wunsch int flags; 11846182fdbdSPeter Wemm 11856182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 11866182fdbdSPeter Wemm fd = device_get_softc(dev); 11876182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 11881a6bed68SJoerg Wunsch flags = device_get_flags(dev); 11896182fdbdSPeter Wemm 11906182fdbdSPeter Wemm bzero(fd, sizeof *fd); 11916182fdbdSPeter Wemm fd->dev = dev; 11926182fdbdSPeter Wemm fd->fdc = fdc; 11936182fdbdSPeter Wemm fd->fdsu = fdsu; 11946182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 11951a6bed68SJoerg Wunsch fd->flags = FD_UA; /* make sure fdautoselect() will be called */ 11966182fdbdSPeter Wemm 11971a6bed68SJoerg Wunsch fd->type = FD_DTYPE(flags); 1198038d1bbdSJoerg Wunsch /* 1199038d1bbdSJoerg Wunsch * XXX I think using __i386__ is wrong here since we actually want to probe 1200038d1bbdSJoerg Wunsch * for the machine type, not the CPU type (so non-PC arch's like the PC98 will 1201038d1bbdSJoerg Wunsch * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH 1202038d1bbdSJoerg Wunsch * == i386 breaks the test on FreeBSD/Alpha. 1203038d1bbdSJoerg Wunsch */ 1204038d1bbdSJoerg Wunsch #ifdef __i386__ 12051a6bed68SJoerg Wunsch if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) { 12061a6bed68SJoerg Wunsch /* Look up what the BIOS thinks we have. */ 12071a6bed68SJoerg Wunsch if (fd->fdu == 0) { 12085f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 12091a6bed68SJoerg Wunsch /* 12101a6bed68SJoerg Wunsch * Somewhat special. No need to force the 12111a6bed68SJoerg Wunsch * user to set device flags, since the Y-E 12121a6bed68SJoerg Wunsch * Data PCMCIA floppy is always a 1.44 MB 12131a6bed68SJoerg Wunsch * device. 12141a6bed68SJoerg Wunsch */ 12151a6bed68SJoerg Wunsch fd->type = FDT_144M; 12160722d6abSJoerg Wunsch else 12171a6bed68SJoerg Wunsch fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4; 12181a6bed68SJoerg Wunsch } else { 12191a6bed68SJoerg Wunsch fd->type = rtcin(RTC_FDISKETTE) & 0x0f; 12206b7bd95bSJoerg Wunsch } 12211a6bed68SJoerg Wunsch if (fd->type == FDT_288M_1) 12221a6bed68SJoerg Wunsch fd->type = FDT_288M; 12231a6bed68SJoerg Wunsch } 1224038d1bbdSJoerg Wunsch #endif /* __i386__ */ 12256182fdbdSPeter Wemm /* is there a unit? */ 12261a6bed68SJoerg Wunsch if (fd->type == FDT_NONE) 12276182fdbdSPeter Wemm return (ENXIO); 12286182fdbdSPeter Wemm 12296182fdbdSPeter Wemm /* select it */ 12306182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 1231fb35bd37SJoerg Wunsch fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 12326182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 12336182fdbdSPeter Wemm 12348de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 12351a6bed68SJoerg Wunsch if ((fdc->flags & FDC_HAS_FIFO) == 0 && 12361a6bed68SJoerg Wunsch fdc->fdct == FDC_ENHANCED && 12371a6bed68SJoerg Wunsch (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 && 12381a6bed68SJoerg Wunsch enable_fifo(fdc) == 0) { 1239b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1240b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1241d66c374fSTor Egge } 12426182fdbdSPeter Wemm 12431a6bed68SJoerg Wunsch if ((flags & FD_NO_PROBE) == 0) { 1244dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 12451a6bed68SJoerg Wunsch if ((fd_sense_drive_status(fdc, &st3) == 0) && 12461a6bed68SJoerg Wunsch (st3 & NE7_ST3_T0)) { 1247dc5df763SJoerg Wunsch /* Seek some steps... */ 12486182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1249dc5df763SJoerg Wunsch /* ...wait a moment... */ 1250dc5df763SJoerg Wunsch DELAY(300000); 1251dc5df763SJoerg Wunsch /* make ctrlr happy: */ 12526182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1253dc5df763SJoerg Wunsch } 1254dc5df763SJoerg Wunsch } 1255dc5df763SJoerg Wunsch 12566b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 12576b7bd95bSJoerg Wunsch /* 12586b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 12591a6bed68SJoerg Wunsch * heads have been beyond cylinder 76, since 12601a6bed68SJoerg Wunsch * most FDCs still barf when attempting to 12611a6bed68SJoerg Wunsch * recalibrate more than 77 steps 12626b7bd95bSJoerg Wunsch */ 1263dc5df763SJoerg Wunsch /* go back to 0: */ 12646182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 12656b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 12666b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 12675b81b6b3SRodney W. Grimes 12686b7bd95bSJoerg Wunsch /* anything responding? */ 1269dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1270dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 12716b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 12726b7bd95bSJoerg Wunsch } 1273dc5df763SJoerg Wunsch } 12741a6bed68SJoerg Wunsch } 12756b7bd95bSJoerg Wunsch 12766182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 12773a2f7427SDavid Greenman 12781a6bed68SJoerg Wunsch if ((flags & FD_NO_PROBE) == 0 && 12791a6bed68SJoerg Wunsch (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */ 12806182fdbdSPeter Wemm return (ENXIO); 12815b81b6b3SRodney W. Grimes 12821a6bed68SJoerg Wunsch switch (fd->type) { 12831a6bed68SJoerg Wunsch case FDT_12M: 12841a6bed68SJoerg Wunsch device_set_desc(dev, "1200-KB 5.25\" drive"); 12851a6bed68SJoerg Wunsch fd->type = FDT_12M; 12861a6bed68SJoerg Wunsch break; 12871a6bed68SJoerg Wunsch case FDT_144M: 12881a6bed68SJoerg Wunsch device_set_desc(dev, "1440-KB 3.5\" drive"); 12891a6bed68SJoerg Wunsch fd->type = FDT_144M; 12901a6bed68SJoerg Wunsch break; 12911a6bed68SJoerg Wunsch case FDT_288M: 12921a6bed68SJoerg Wunsch device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 12931a6bed68SJoerg Wunsch fd->type = FDT_288M; 12941a6bed68SJoerg Wunsch break; 12951a6bed68SJoerg Wunsch case FDT_360K: 12961a6bed68SJoerg Wunsch device_set_desc(dev, "360-KB 5.25\" drive"); 12971a6bed68SJoerg Wunsch fd->type = FDT_360K; 12981a6bed68SJoerg Wunsch break; 12991a6bed68SJoerg Wunsch case FDT_720K: 13001a6bed68SJoerg Wunsch device_set_desc(dev, "720-KB 3.5\" drive"); 13011a6bed68SJoerg Wunsch fd->type = FDT_720K; 13021a6bed68SJoerg Wunsch break; 13031a6bed68SJoerg Wunsch default: 13041a6bed68SJoerg Wunsch return (ENXIO); 13051a6bed68SJoerg Wunsch } 1306dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1307b99f0a4aSAndrew Moore fd->fdc = fdc; 1308b99f0a4aSAndrew Moore fd->fdsu = fdsu; 13093a2f7427SDavid Greenman fd->options = 0; 131002a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 131102a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 13125b81b6b3SRodney W. Grimes 13131a6bed68SJoerg Wunsch /* initialize densities for subdevices */ 13141a6bed68SJoerg Wunsch for (i = 0; i < NUMDENS; i++) 13151a6bed68SJoerg Wunsch memcpy(fd->fts + i, fd_native_types + fd->type, 13161a6bed68SJoerg Wunsch sizeof(struct fd_type)); 13176182fdbdSPeter Wemm return (0); 13186182fdbdSPeter Wemm } 13196182fdbdSPeter Wemm 13206182fdbdSPeter Wemm static int 13216182fdbdSPeter Wemm fd_attach(device_t dev) 13226182fdbdSPeter Wemm { 13236182fdbdSPeter Wemm struct fd_data *fd; 132464860614SJoerg Wunsch static int cdevsw_add_done; 132560444853SJoerg Wunsch int i; 13266182fdbdSPeter Wemm 132702211baeSNick Hibma if (!cdevsw_add_done) { 1328475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 132964860614SJoerg Wunsch cdevsw_add_done = 1; 133002211baeSNick Hibma } 133164860614SJoerg Wunsch fd = device_get_softc(dev); 133260444853SJoerg Wunsch fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000); 13335f431174SJoerg Wunsch fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6, 13343f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 13351a6bed68SJoerg Wunsch for (i = 0; i < NUMDENS - 1; i++) 133660444853SJoerg Wunsch fd->clonedevs[i] = NODEV; 13376182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 1338f8ce7dd5SJoerg Wunsch device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 13392a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 13402a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 13416182fdbdSPeter Wemm return (0); 13425b81b6b3SRodney W. Grimes } 13435b81b6b3SRodney W. Grimes 13445f830ea2SJoerg Wunsch static int 13455f830ea2SJoerg Wunsch fd_detach(device_t dev) 13465f830ea2SJoerg Wunsch { 13475f830ea2SJoerg Wunsch struct fd_data *fd; 134860444853SJoerg Wunsch int i; 13495f830ea2SJoerg Wunsch 13505f830ea2SJoerg Wunsch fd = device_get_softc(dev); 135160444853SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 1352e219897aSJoerg Wunsch devstat_remove_entry(&fd->device_stats); 1353e219897aSJoerg Wunsch destroy_dev(fd->masterdev); 13541a6bed68SJoerg Wunsch for (i = 0; i < NUMDENS - 1; i++) 135560444853SJoerg Wunsch if (fd->clonedevs[i] != NODEV) 135660444853SJoerg Wunsch destroy_dev(fd->clonedevs[i]); 1357e219897aSJoerg Wunsch cdevsw_remove(&fd_cdevsw); 1358e219897aSJoerg Wunsch EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag); 13595f830ea2SJoerg Wunsch 13605f830ea2SJoerg Wunsch return (0); 13615f830ea2SJoerg Wunsch } 13625f830ea2SJoerg Wunsch 136316e68fc6SPeter Wemm static device_method_t fd_methods[] = { 136416e68fc6SPeter Wemm /* Device interface */ 136516e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 136616e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 13675f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 136816e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 136916e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 137016e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 137116e68fc6SPeter Wemm 137216e68fc6SPeter Wemm { 0, 0 } 137316e68fc6SPeter Wemm }; 137416e68fc6SPeter Wemm 137516e68fc6SPeter Wemm static driver_t fd_driver = { 137616e68fc6SPeter Wemm "fd", 137716e68fc6SPeter Wemm fd_methods, 137816e68fc6SPeter Wemm sizeof(struct fd_data) 137916e68fc6SPeter Wemm }; 138016e68fc6SPeter Wemm 1381475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 138216e68fc6SPeter Wemm 1383246ed35dSJoerg Wunsch /* 1384246ed35dSJoerg Wunsch * More auxiliary functions. 1385246ed35dSJoerg Wunsch */ 1386246ed35dSJoerg Wunsch /* 1387246ed35dSJoerg Wunsch * Motor control stuff. 1388246ed35dSJoerg Wunsch * Remember to not deselect the drive we're working on. 1389246ed35dSJoerg Wunsch */ 13903a2f7427SDavid Greenman static void 13916182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 13925b81b6b3SRodney W. Grimes { 1393fb35bd37SJoerg Wunsch int fdout; 13943a2f7427SDavid Greenman 1395fb35bd37SJoerg Wunsch fdout = fdc->fdout; 13963a2f7427SDavid Greenman if (turnon) { 13973a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 1398fb35bd37SJoerg Wunsch fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu; 13993a2f7427SDavid Greenman } else 14003a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 14016182fdbdSPeter Wemm fdc->fdout = fdout; 1402fb35bd37SJoerg Wunsch fdout_wr(fdc, fdout); 14033a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 14043a2f7427SDavid Greenman } 14053a2f7427SDavid Greenman 1406381fe1aaSGarrett Wollman static void 14076182fdbdSPeter Wemm fd_turnoff(void *xfd) 14085b81b6b3SRodney W. Grimes { 1409f5f7ba03SJordan K. Hubbard int s; 14106182fdbdSPeter Wemm fd_p fd = xfd; 1411dc16046fSJoerg Wunsch 14126182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 14138335c1b8SBruce Evans 14145f830ea2SJoerg Wunsch s = splbio(); 14158335c1b8SBruce Evans /* 14168335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 14175f830ea2SJoerg Wunsch * 14185f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 14195f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 14205f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 14215f830ea2SJoerg Wunsch * so the state machine gets re-entered. 14228335c1b8SBruce Evans */ 14236182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 14245f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 14255f830ea2SJoerg Wunsch splx(s); 14268335c1b8SBruce Evans return; 14278335c1b8SBruce Evans } 14288335c1b8SBruce Evans 14295b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 14306182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1431f5f7ba03SJordan K. Hubbard splx(s); 14325b81b6b3SRodney W. Grimes } 14335b81b6b3SRodney W. Grimes 14343a2f7427SDavid Greenman static void 14356182fdbdSPeter Wemm fd_motor_on(void *xfd) 14365b81b6b3SRodney W. Grimes { 1437f5f7ba03SJordan K. Hubbard int s; 14386182fdbdSPeter Wemm fd_p fd = xfd; 1439f5f7ba03SJordan K. Hubbard 1440f5f7ba03SJordan K. Hubbard s = splbio(); 14415b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 14425b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 14435b81b6b3SRodney W. Grimes { 14446182fdbdSPeter Wemm fdc_intr(fd->fdc); 14455b81b6b3SRodney W. Grimes } 1446f5f7ba03SJordan K. Hubbard splx(s); 14475b81b6b3SRodney W. Grimes } 14485b81b6b3SRodney W. Grimes 14493a2f7427SDavid Greenman static void 14506182fdbdSPeter Wemm fd_turnon(fd_p fd) 14515b81b6b3SRodney W. Grimes { 14525b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 14535b81b6b3SRodney W. Grimes { 14543a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 14556182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 14566182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 14575b81b6b3SRodney W. Grimes } 14585b81b6b3SRodney W. Grimes } 14595b81b6b3SRodney W. Grimes 1460381fe1aaSGarrett Wollman static void 1461dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 14625b81b6b3SRodney W. Grimes { 14633a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1464427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 14653a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 14663a2f7427SDavid Greenman DELAY(100); 14673a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1468427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 14693a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 14703a2f7427SDavid Greenman DELAY(100); 1471427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 14723a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 14733a2f7427SDavid Greenman 147464860614SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 14756182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1476dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1477dc5df763SJoerg Wunsch 0); 1478d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1479d66c374fSTor Egge (void) enable_fifo(fdc); 14805b81b6b3SRodney W. Grimes } 14815b81b6b3SRodney W. Grimes 1482246ed35dSJoerg Wunsch /* 1483246ed35dSJoerg Wunsch * FDC IO functions, take care of the main status register, timeout 1484246ed35dSJoerg Wunsch * in case the desired status bits are never set. 1485246ed35dSJoerg Wunsch */ 1486b5e8ce9fSBruce Evans static int 14876182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1488dc5df763SJoerg Wunsch { 1489d0900d6bSJoerg Wunsch int i, j = FDSTS_TIMEOUT; 1490427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1491d0900d6bSJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) { 1492dc5df763SJoerg Wunsch if (i == NE7_RQM) 14936182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1494cb38bb6cSJoerg Wunsch /* 1495cb38bb6cSJoerg Wunsch * After (maybe) 1 msec of waiting, back off to larger 1496cb38bb6cSJoerg Wunsch * stepping to get the timing more accurate. 1497cb38bb6cSJoerg Wunsch */ 1498cb38bb6cSJoerg Wunsch if (FDSTS_TIMEOUT - j > 1000) { 1499cb38bb6cSJoerg Wunsch DELAY(1000); 1500cb38bb6cSJoerg Wunsch j -= 999; 1501cb38bb6cSJoerg Wunsch } else 1502d0900d6bSJoerg Wunsch DELAY(1); 1503d0900d6bSJoerg Wunsch } 1504dc5df763SJoerg Wunsch if (j <= 0) 15056182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1506d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1507427ccf00SDoug Rabson i = fddata_rd(fdc); 1508dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1509dc5df763SJoerg Wunsch *ptr = i; 1510dc5df763SJoerg Wunsch return 0; 1511d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1512427ccf00SDoug Rabson i = fddata_rd(fdc); 1513dc5df763SJoerg Wunsch if (ptr) 1514dc5df763SJoerg Wunsch *ptr = i; 1515dc5df763SJoerg Wunsch return 0; 1516d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1517dc5df763SJoerg Wunsch } 1518dc5df763SJoerg Wunsch 1519dc5df763SJoerg Wunsch int 15206182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 15215b81b6b3SRodney W. Grimes { 15223b3837dbSRodney W. Grimes int i; 15235b81b6b3SRodney W. Grimes 15243b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 1525d0900d6bSJoerg Wunsch i = FDSTS_TIMEOUT; 1526d0900d6bSJoerg Wunsch while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0) 1527cb38bb6cSJoerg Wunsch /* 1528cb38bb6cSJoerg Wunsch * After (maybe) 1 msec of waiting, back off to larger 1529cb38bb6cSJoerg Wunsch * stepping to get the timing more accurate. 1530cb38bb6cSJoerg Wunsch */ 1531cb38bb6cSJoerg Wunsch if (FDSTS_TIMEOUT - i > 1000) { 1532cb38bb6cSJoerg Wunsch DELAY(1000); 1533cb38bb6cSJoerg Wunsch i -= 999; 1534cb38bb6cSJoerg Wunsch } else 1535d0900d6bSJoerg Wunsch DELAY(1); 15366182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 15373b3837dbSRodney W. Grimes 15383b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 1539d0900d6bSJoerg Wunsch i = FDSTS_TIMEOUT; 1540d0900d6bSJoerg Wunsch while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0) 1541cb38bb6cSJoerg Wunsch /* 1542cb38bb6cSJoerg Wunsch * After (maybe) 1 msec of waiting, back off to larger 1543cb38bb6cSJoerg Wunsch * stepping to get the timing more accurate. 1544cb38bb6cSJoerg Wunsch */ 1545cb38bb6cSJoerg Wunsch if (FDSTS_TIMEOUT - i > 1000) { 1546cb38bb6cSJoerg Wunsch DELAY(1000); 1547cb38bb6cSJoerg Wunsch i -= 999; 1548cb38bb6cSJoerg Wunsch } else 1549d0900d6bSJoerg Wunsch DELAY(1); 155016b04b6aSJoerg Wunsch if (i <= 0) 15516182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 15523b3837dbSRodney W. Grimes 15533b3837dbSRodney W. Grimes /* Send the command and return */ 1554427ccf00SDoug Rabson fddata_wr(fdc, x); 15553a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 15565b81b6b3SRodney W. Grimes return (0); 15575b81b6b3SRodney W. Grimes } 15585b81b6b3SRodney W. Grimes 1559246ed35dSJoerg Wunsch /* 1560246ed35dSJoerg Wunsch * Block device driver interface functions (interspersed with even more 1561246ed35dSJoerg Wunsch * auxiliary functions). 1562246ed35dSJoerg Wunsch */ 1563381fe1aaSGarrett Wollman int 1564b40ce416SJulian Elischer Fdopen(dev_t dev, int flags, int mode, struct thread *td) 15655b81b6b3SRodney W. Grimes { 15665b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 156720a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 15686182fdbdSPeter Wemm fd_p fd; 1569b99f0a4aSAndrew Moore fdc_p fdc; 15701a6bed68SJoerg Wunsch int rv, unitattn, dflags; 15715b81b6b3SRodney W. Grimes 15726182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1573b99f0a4aSAndrew Moore return (ENXIO); 15746182fdbdSPeter Wemm fdc = fd->fdc; 15751a6bed68SJoerg Wunsch if ((fdc == NULL) || (fd->type == FDT_NONE)) 1576b99f0a4aSAndrew Moore return (ENXIO); 1577b99f0a4aSAndrew Moore if (type > NUMDENS) 1578b99f0a4aSAndrew Moore return (ENXIO); 15791a6bed68SJoerg Wunsch dflags = device_get_flags(fd->dev); 15803e425b96SJulian Elischer /* 15811a6bed68SJoerg Wunsch * This is a bit bogus. It's still possible that e. g. a 15821a6bed68SJoerg Wunsch * descriptor gets inherited to a child, but then it's at 15831a6bed68SJoerg Wunsch * least for the same subdevice. By checking FD_OPEN here, we 15841a6bed68SJoerg Wunsch * can ensure that a device isn't attempted to be opened with 15851a6bed68SJoerg Wunsch * different densities at the same time where the second open 15861a6bed68SJoerg Wunsch * could clobber the settings from the first one. 15873e425b96SJulian Elischer */ 15881a6bed68SJoerg Wunsch if (fd->flags & FD_OPEN) 15891a6bed68SJoerg Wunsch return (EBUSY); 15901a6bed68SJoerg Wunsch 15911a6bed68SJoerg Wunsch if (type == 0) { 15921a6bed68SJoerg Wunsch if (flags & FNONBLOCK) { 15931a6bed68SJoerg Wunsch /* 15941a6bed68SJoerg Wunsch * Unfortunately, physio(9) discards its ioflag 15951a6bed68SJoerg Wunsch * argument, thus preventing us from seeing the 15961a6bed68SJoerg Wunsch * IO_NDELAY bit. So we need to keep track 15971a6bed68SJoerg Wunsch * ourselves. 15981a6bed68SJoerg Wunsch */ 15991a6bed68SJoerg Wunsch fd->flags |= FD_NONBLOCK; 16001a6bed68SJoerg Wunsch fd->ft = 0; 16011a6bed68SJoerg Wunsch } else { 16021a6bed68SJoerg Wunsch /* 16031a6bed68SJoerg Wunsch * Figure out a unit attention condition. 16041a6bed68SJoerg Wunsch * 16051a6bed68SJoerg Wunsch * If UA has been forced, proceed. 16061a6bed68SJoerg Wunsch * 16071a6bed68SJoerg Wunsch * If motor is off, turn it on for a moment 16081a6bed68SJoerg Wunsch * and select our drive, in order to read the 16091a6bed68SJoerg Wunsch * UA hardware signal. 16101a6bed68SJoerg Wunsch * 16111a6bed68SJoerg Wunsch * If motor is on, and our drive is currently 16121a6bed68SJoerg Wunsch * selected, just read the hardware bit. 16131a6bed68SJoerg Wunsch * 16141a6bed68SJoerg Wunsch * If motor is on, but active for another 16151a6bed68SJoerg Wunsch * drive on that controller, we are lost. We 16161a6bed68SJoerg Wunsch * cannot risk to deselect the other drive, so 16171a6bed68SJoerg Wunsch * we just assume a forced UA condition to be 16181a6bed68SJoerg Wunsch * on the safe side. 16191a6bed68SJoerg Wunsch */ 16201a6bed68SJoerg Wunsch unitattn = 0; 16211a6bed68SJoerg Wunsch if ((dflags & FD_NO_CHLINE) != 0 || 16221a6bed68SJoerg Wunsch (fd->flags & FD_UA) != 0) { 16231a6bed68SJoerg Wunsch unitattn = 1; 16241a6bed68SJoerg Wunsch fd->flags &= ~FD_UA; 16251a6bed68SJoerg Wunsch } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 | 16261a6bed68SJoerg Wunsch FDO_MOEN2 | FDO_MOEN3)) { 16271a6bed68SJoerg Wunsch if ((fdc->fdout & FDO_FDSEL) == fd->fdsu) 16281a6bed68SJoerg Wunsch unitattn = fdin_rd(fdc) & FDI_DCHG; 16291a6bed68SJoerg Wunsch else 16301a6bed68SJoerg Wunsch unitattn = 1; 16311a6bed68SJoerg Wunsch } else { 16321a6bed68SJoerg Wunsch set_motor(fdc, fd->fdsu, TURNON); 16331a6bed68SJoerg Wunsch unitattn = fdin_rd(fdc) & FDI_DCHG; 16341a6bed68SJoerg Wunsch set_motor(fdc, fd->fdsu, TURNOFF); 1635b39c878eSAndrey A. Chernov } 16361a6bed68SJoerg Wunsch if (unitattn && (rv = fdautoselect(dev)) != 0) 16371a6bed68SJoerg Wunsch return (rv); 16387ca0641bSAndrey A. Chernov } 16391a6bed68SJoerg Wunsch } else { 16401a6bed68SJoerg Wunsch fd->ft = fd->fts + type; 16417ca0641bSAndrey A. Chernov } 16426182fdbdSPeter Wemm fd->flags |= FD_OPEN; 16433fef646eSJoerg Wunsch /* 16443fef646eSJoerg Wunsch * Clearing the DMA overrun counter at open time is a bit messy. 16453fef646eSJoerg Wunsch * Since we're only managing one counter per controller, opening 16463fef646eSJoerg Wunsch * the second drive could mess it up. Anyway, if the DMA overrun 16473fef646eSJoerg Wunsch * condition is really persistent, it will eventually time out 16483fef646eSJoerg Wunsch * still. OTOH, clearing it here will ensure we'll at least start 16493fef646eSJoerg Wunsch * trying again after a previous (maybe even long ago) failure. 16503fef646eSJoerg Wunsch * Also, this is merely a stop-gap measure only that should not 16513fef646eSJoerg Wunsch * happen during normal operation, so we can tolerate it to be a 16523fef646eSJoerg Wunsch * bit sloppy about this. 16533fef646eSJoerg Wunsch */ 16543fef646eSJoerg Wunsch fdc->dma_overruns = 0; 16555f830ea2SJoerg Wunsch 16565b81b6b3SRodney W. Grimes return 0; 16575b81b6b3SRodney W. Grimes } 16585b81b6b3SRodney W. Grimes 1659381fe1aaSGarrett Wollman int 1660b40ce416SJulian Elischer fdclose(dev_t dev, int flags, int mode, struct thread *td) 16615b81b6b3SRodney W. Grimes { 16625b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 16636182fdbdSPeter Wemm struct fd_data *fd; 1664b99f0a4aSAndrew Moore 16656182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 16661a6bed68SJoerg Wunsch fd->flags &= ~(FD_OPEN | FD_NONBLOCK); 16672995d110SJoerg Wunsch fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1668dc16046fSJoerg Wunsch 16695b81b6b3SRodney W. Grimes return (0); 16705b81b6b3SRodney W. Grimes } 16715b81b6b3SRodney W. Grimes 16723a2f7427SDavid Greenman void 16738177437dSPoul-Henning Kamp fdstrategy(struct bio *bp) 16743a2f7427SDavid Greenman { 1675fb35bd37SJoerg Wunsch long blknum, nblocks; 16763a2f7427SDavid Greenman int s; 16773a2f7427SDavid Greenman fdu_t fdu; 16783a2f7427SDavid Greenman fdc_p fdc; 16793a2f7427SDavid Greenman fd_p fd; 16803a2f7427SDavid Greenman size_t fdblk; 16813a2f7427SDavid Greenman 16828177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 16836182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 16846182fdbdSPeter Wemm if (fd == 0) 16856182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 16868177437dSPoul-Henning Kamp (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 16873a2f7427SDavid Greenman fdc = fd->fdc; 16881a6bed68SJoerg Wunsch if (fd->type == FDT_NONE || fd->ft == 0) { 16898177437dSPoul-Henning Kamp bp->bio_error = ENXIO; 16908177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 16913b178206SWarner Losh goto bad; 169264860614SJoerg Wunsch } 1693d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 1694f664aeeeSJoerg Wunsch if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) { 16951a6bed68SJoerg Wunsch if (fd->flags & FD_NONBLOCK) { 16961a6bed68SJoerg Wunsch bp->bio_error = EAGAIN; 16971a6bed68SJoerg Wunsch bp->bio_flags |= BIO_ERROR; 16981a6bed68SJoerg Wunsch goto bad; 16991a6bed68SJoerg Wunsch } 17008177437dSPoul-Henning Kamp if (bp->bio_blkno < 0) { 1701dc5df763SJoerg Wunsch printf( 17026a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 17038177437dSPoul-Henning Kamp fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 17048177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 17058177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 17063a2f7427SDavid Greenman goto bad; 17073a2f7427SDavid Greenman } 17088177437dSPoul-Henning Kamp if ((bp->bio_bcount % fdblk) != 0) { 17098177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 17108177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 17113a2f7427SDavid Greenman goto bad; 17123a2f7427SDavid Greenman } 17133a2f7427SDavid Greenman } 17143a2f7427SDavid Greenman 17153a2f7427SDavid Greenman /* 17163a2f7427SDavid Greenman * Set up block calculations. 17173a2f7427SDavid Greenman */ 17188177437dSPoul-Henning Kamp if (bp->bio_blkno > 20000000) { 1719bb6382faSJoerg Wunsch /* 1720bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1721bb6382faSJoerg Wunsch * multiplication below from overflowing. 1722bb6382faSJoerg Wunsch */ 17238177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 17248177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 17253a2f7427SDavid Greenman goto bad; 17263a2f7427SDavid Greenman } 1727fb35bd37SJoerg Wunsch blknum = bp->bio_blkno * DEV_BSIZE / fdblk; 1728bb6382faSJoerg Wunsch nblocks = fd->ft->size; 1729fb35bd37SJoerg Wunsch if (blknum + bp->bio_bcount / fdblk > nblocks) { 1730fb35bd37SJoerg Wunsch if (blknum >= nblocks) { 1731fb35bd37SJoerg Wunsch if (bp->bio_cmd == BIO_READ) 1732fb35bd37SJoerg Wunsch bp->bio_resid = bp->bio_bcount; 1733fb35bd37SJoerg Wunsch else { 1734fb35bd37SJoerg Wunsch bp->bio_error = ENOSPC; 17358177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 1736bb6382faSJoerg Wunsch } 1737fb35bd37SJoerg Wunsch goto bad; /* not always bad, but EOF */ 1738fb35bd37SJoerg Wunsch } 1739fb35bd37SJoerg Wunsch bp->bio_bcount = (nblocks - blknum) * fdblk; 1740bb6382faSJoerg Wunsch } 17418177437dSPoul-Henning Kamp bp->bio_pblkno = bp->bio_blkno; 17423a2f7427SDavid Greenman s = splbio(); 17438177437dSPoul-Henning Kamp bioqdisksort(&fdc->head, bp); 17446182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1745b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 17465f830ea2SJoerg Wunsch device_busy(fd->dev); 17476182fdbdSPeter Wemm fdstart(fdc); 17483a2f7427SDavid Greenman splx(s); 17493a2f7427SDavid Greenman return; 17503a2f7427SDavid Greenman 17513a2f7427SDavid Greenman bad: 17523a2f7427SDavid Greenman biodone(bp); 17533a2f7427SDavid Greenman } 17543a2f7427SDavid Greenman 1755246ed35dSJoerg Wunsch /* 1756246ed35dSJoerg Wunsch * fdstart 1757246ed35dSJoerg Wunsch * 1758246ed35dSJoerg Wunsch * We have just queued something. If the controller is not busy 1759246ed35dSJoerg Wunsch * then simulate the case where it has just finished a command 1760246ed35dSJoerg Wunsch * So that it (the interrupt routine) looks on the queue for more 1761246ed35dSJoerg Wunsch * work to do and picks up what we just added. 1762246ed35dSJoerg Wunsch * 1763246ed35dSJoerg Wunsch * If the controller is already busy, we need do nothing, as it 1764246ed35dSJoerg Wunsch * will pick up our work when the present work completes. 1765246ed35dSJoerg Wunsch */ 1766381fe1aaSGarrett Wollman static void 17676182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 17685b81b6b3SRodney W. Grimes { 17695b81b6b3SRodney W. Grimes int s; 17705b81b6b3SRodney W. Grimes 17715b81b6b3SRodney W. Grimes s = splbio(); 17726182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 17735b81b6b3SRodney W. Grimes { 17746182fdbdSPeter Wemm fdc_intr(fdc); 17755b81b6b3SRodney W. Grimes } 17765b81b6b3SRodney W. Grimes splx(s); 17775b81b6b3SRodney W. Grimes } 17785b81b6b3SRodney W. Grimes 1779381fe1aaSGarrett Wollman static void 17806182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 17815b81b6b3SRodney W. Grimes { 17825c1a1eaeSBruce Evans fdc_p fdc; 1783f5f7ba03SJordan K. Hubbard int s; 17845b81b6b3SRodney W. Grimes 17856182fdbdSPeter Wemm fdc = xfdc; 17865c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 17875b81b6b3SRodney W. Grimes 17883a2f7427SDavid Greenman /* 17893a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 17903a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 17913a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 17923a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 17933a2f7427SDavid Greenman * Many thanks, Big Blue! 17945c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 17955c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 17965c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 17975c1a1eaeSBruce Evans * will reset the FDC and retry once. 17983a2f7427SDavid Greenman */ 17993a2f7427SDavid Greenman s = splbio(); 18005c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 18015c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 18025c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 18036182fdbdSPeter Wemm fdc_intr(fdc); 1804f5f7ba03SJordan K. Hubbard splx(s); 18055b81b6b3SRodney W. Grimes } 18065b81b6b3SRodney W. Grimes 1807246ed35dSJoerg Wunsch /* Just ensure it has the right spl. */ 1808381fe1aaSGarrett Wollman static void 18096182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 18105b81b6b3SRodney W. Grimes { 18115b81b6b3SRodney W. Grimes int s; 18123a2f7427SDavid Greenman 18135b81b6b3SRodney W. Grimes s = splbio(); 18146182fdbdSPeter Wemm fdc_intr(xfdc); 18155b81b6b3SRodney W. Grimes splx(s); 18165b81b6b3SRodney W. Grimes } 18175b81b6b3SRodney W. Grimes 1818246ed35dSJoerg Wunsch /* 1819246ed35dSJoerg Wunsch * fdc_intr 1820246ed35dSJoerg Wunsch * 1821246ed35dSJoerg Wunsch * Keep calling the state machine until it returns a 0. 1822246ed35dSJoerg Wunsch * Always called at splbio. 1823246ed35dSJoerg Wunsch */ 1824fe310de8SBruce Evans static void 18256182fdbdSPeter Wemm fdc_intr(void *xfdc) 18265b81b6b3SRodney W. Grimes { 18276182fdbdSPeter Wemm fdc_p fdc = xfdc; 18286182fdbdSPeter Wemm while(fdstate(fdc)) 1829381fe1aaSGarrett Wollman ; 18305b81b6b3SRodney W. Grimes } 18315b81b6b3SRodney W. Grimes 183269acd21dSWarner Losh /* 1833246ed35dSJoerg Wunsch * Magic pseudo-DMA initialization for YE FDC. Sets count and 1834246ed35dSJoerg Wunsch * direction. 183569acd21dSWarner Losh */ 18363b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 18373b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 18383b178206SWarner Losh ((cnt)-1) & 0xff); \ 18393b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 18403b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 184169acd21dSWarner Losh 184269acd21dSWarner Losh /* 1843246ed35dSJoerg Wunsch * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy. 184469acd21dSWarner Losh */ 18454c34deeeSJoerg Wunsch static int 18464c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 184769acd21dSWarner Losh { 184869acd21dSWarner Losh u_char *cptr = (u_char *)addr; 184969acd21dSWarner Losh 185021144e3bSPoul-Henning Kamp if (flags == BIO_READ) { 185169acd21dSWarner Losh if (fdc->state != PIOREAD) { 185269acd21dSWarner Losh fdc->state = PIOREAD; 185369acd21dSWarner Losh return(0); 185464860614SJoerg Wunsch } 18553b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 18563b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 18573b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 185869acd21dSWarner Losh } else { 18593b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 18603b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 18613b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 186264860614SJoerg Wunsch } 186369acd21dSWarner Losh return(1); 186469acd21dSWarner Losh } 186569acd21dSWarner Losh 1866246ed35dSJoerg Wunsch /* 18671a6bed68SJoerg Wunsch * Try figuring out the density of the media present in our device. 18681a6bed68SJoerg Wunsch */ 18691a6bed68SJoerg Wunsch static int 18701a6bed68SJoerg Wunsch fdautoselect(dev_t dev) 18711a6bed68SJoerg Wunsch { 18721a6bed68SJoerg Wunsch fdu_t fdu; 18731a6bed68SJoerg Wunsch fd_p fd; 18741a6bed68SJoerg Wunsch struct fd_type *fdtp; 18751a6bed68SJoerg Wunsch struct fdc_readid id; 18761a6bed68SJoerg Wunsch int i, n, oopts, rv; 18771a6bed68SJoerg Wunsch 18781a6bed68SJoerg Wunsch fdu = FDUNIT(minor(dev)); 18791a6bed68SJoerg Wunsch fd = devclass_get_softc(fd_devclass, fdu); 18801a6bed68SJoerg Wunsch 18811a6bed68SJoerg Wunsch switch (fd->type) { 18821a6bed68SJoerg Wunsch default: 18831a6bed68SJoerg Wunsch return (ENXIO); 18841a6bed68SJoerg Wunsch 18851a6bed68SJoerg Wunsch case FDT_360K: 18861a6bed68SJoerg Wunsch case FDT_720K: 18871a6bed68SJoerg Wunsch /* no autoselection on those drives */ 18881a6bed68SJoerg Wunsch fd->ft = fd_native_types + fd->type; 18891a6bed68SJoerg Wunsch return (0); 18901a6bed68SJoerg Wunsch 18911a6bed68SJoerg Wunsch case FDT_12M: 18921a6bed68SJoerg Wunsch fdtp = fd_searchlist_12m; 18931a6bed68SJoerg Wunsch n = sizeof fd_searchlist_12m / sizeof(struct fd_type); 18941a6bed68SJoerg Wunsch break; 18951a6bed68SJoerg Wunsch 18961a6bed68SJoerg Wunsch case FDT_144M: 18971a6bed68SJoerg Wunsch fdtp = fd_searchlist_144m; 18981a6bed68SJoerg Wunsch n = sizeof fd_searchlist_144m / sizeof(struct fd_type); 18991a6bed68SJoerg Wunsch break; 19001a6bed68SJoerg Wunsch 19011a6bed68SJoerg Wunsch case FDT_288M: 19021a6bed68SJoerg Wunsch fdtp = fd_searchlist_288m; 19031a6bed68SJoerg Wunsch n = sizeof fd_searchlist_288m / sizeof(struct fd_type); 19041a6bed68SJoerg Wunsch break; 19051a6bed68SJoerg Wunsch } 19061a6bed68SJoerg Wunsch 19071a6bed68SJoerg Wunsch /* 19081a6bed68SJoerg Wunsch * Try reading sector ID fields, first at cylinder 0, head 0, 19091a6bed68SJoerg Wunsch * then at cylinder 2, head N. We don't probe cylinder 1, 19101a6bed68SJoerg Wunsch * since for 5.25in DD media in a HD drive, there are no data 19111a6bed68SJoerg Wunsch * to read (2 step pulses per media cylinder required). For 19121a6bed68SJoerg Wunsch * two-sided media, the second probe always goes to head 1, so 19131a6bed68SJoerg Wunsch * we can tell them apart from single-sided media. As a 19141a6bed68SJoerg Wunsch * side-effect this means that single-sided media should be 19151a6bed68SJoerg Wunsch * mentioned in the search list after two-sided media of an 19161a6bed68SJoerg Wunsch * otherwise identical density. Media with a different number 19171a6bed68SJoerg Wunsch * of sectors per track but otherwise identical parameters 19181a6bed68SJoerg Wunsch * cannot be distinguished at all. 19191a6bed68SJoerg Wunsch * 19201a6bed68SJoerg Wunsch * If we successfully read an ID field on both cylinders where 19211a6bed68SJoerg Wunsch * the recorded values match our expectation, we are done. 19221a6bed68SJoerg Wunsch * Otherwise, we try the next density entry from the table. 19231a6bed68SJoerg Wunsch * 19241a6bed68SJoerg Wunsch * Stepping to cylinder 2 has the side-effect of clearing the 19251a6bed68SJoerg Wunsch * unit attention bit. 19261a6bed68SJoerg Wunsch */ 19271a6bed68SJoerg Wunsch oopts = fd->options; 19281a6bed68SJoerg Wunsch fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY; 19291a6bed68SJoerg Wunsch for (i = 0; i < n; i++, fdtp++) { 19301a6bed68SJoerg Wunsch fd->ft = fdtp; 19311a6bed68SJoerg Wunsch 19321a6bed68SJoerg Wunsch id.cyl = id.head = 0; 19331a6bed68SJoerg Wunsch rv = fdmisccmd(dev, BIO_RDSECTID, &id); 19341a6bed68SJoerg Wunsch if (rv != 0) 19351a6bed68SJoerg Wunsch continue; 19361a6bed68SJoerg Wunsch if (id.cyl != 0 || id.head != 0 || 19371a6bed68SJoerg Wunsch id.secshift != fdtp->secsize) 19381a6bed68SJoerg Wunsch continue; 19391a6bed68SJoerg Wunsch id.cyl = 2; 19401a6bed68SJoerg Wunsch id.head = fd->ft->heads - 1; 19411a6bed68SJoerg Wunsch rv = fdmisccmd(dev, BIO_RDSECTID, &id); 19421a6bed68SJoerg Wunsch if (id.cyl != 2 || id.head != fdtp->heads - 1 || 19431a6bed68SJoerg Wunsch id.secshift != fdtp->secsize) 19441a6bed68SJoerg Wunsch continue; 19451a6bed68SJoerg Wunsch if (rv == 0) 19461a6bed68SJoerg Wunsch break; 19471a6bed68SJoerg Wunsch } 19481a6bed68SJoerg Wunsch 19491a6bed68SJoerg Wunsch fd->options = oopts; 19501a6bed68SJoerg Wunsch if (i == n) { 19515613959dSJoerg Wunsch if (bootverbose) 19521a6bed68SJoerg Wunsch device_printf(fd->dev, "autoselection failed\n"); 19531a6bed68SJoerg Wunsch fd->ft = 0; 19541a6bed68SJoerg Wunsch return (EIO); 19551a6bed68SJoerg Wunsch } else { 19565613959dSJoerg Wunsch if (bootverbose) 19571a6bed68SJoerg Wunsch device_printf(fd->dev, "autoselected %d KB medium\n", 19581a6bed68SJoerg Wunsch fd->ft->size / 2); 19591a6bed68SJoerg Wunsch return (0); 19601a6bed68SJoerg Wunsch } 19611a6bed68SJoerg Wunsch } 19621a6bed68SJoerg Wunsch 19631a6bed68SJoerg Wunsch 19641a6bed68SJoerg Wunsch /* 1965246ed35dSJoerg Wunsch * The controller state machine. 1966246ed35dSJoerg Wunsch * 1967246ed35dSJoerg Wunsch * If it returns a non zero value, it should be called again immediately. 1968246ed35dSJoerg Wunsch */ 19693a2f7427SDavid Greenman static int 19706182fdbdSPeter Wemm fdstate(fdc_p fdc) 19715b81b6b3SRodney W. Grimes { 197264860614SJoerg Wunsch struct fdc_readid *idp; 1973fb35bd37SJoerg Wunsch int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac; 19741a6bed68SJoerg Wunsch int st0, cyl, st3, idf, ne7cmd, mfm, steptrac; 1975fb35bd37SJoerg Wunsch unsigned long blknum; 19765b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 19775b81b6b3SRodney W. Grimes fd_p fd; 19788177437dSPoul-Henning Kamp register struct bio *bp; 1979b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 19803a2f7427SDavid Greenman size_t fdblk; 19815b81b6b3SRodney W. Grimes 1982e93e63cbSBruce Evans bp = fdc->bp; 1983e93e63cbSBruce Evans if (bp == NULL) { 19848177437dSPoul-Henning Kamp bp = bioq_first(&fdc->head); 1985e93e63cbSBruce Evans if (bp != NULL) { 19868177437dSPoul-Henning Kamp bioq_remove(&fdc->head, bp); 1987e93e63cbSBruce Evans fdc->bp = bp; 1988e93e63cbSBruce Evans } 1989e93e63cbSBruce Evans } 1990e93e63cbSBruce Evans if (bp == NULL) { 1991246ed35dSJoerg Wunsch /* 1992246ed35dSJoerg Wunsch * Nothing left for this controller to do, 1993246ed35dSJoerg Wunsch * force into the IDLE state. 1994246ed35dSJoerg Wunsch */ 19955b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 19966182fdbdSPeter Wemm if (fdc->fd) { 1997b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1998b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 19995b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20005b81b6b3SRodney W. Grimes fdc->fdu = -1; 20015b81b6b3SRodney W. Grimes } 20026182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 20035b81b6b3SRodney W. Grimes return (0); 20045b81b6b3SRodney W. Grimes } 20058177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 20066182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 20073a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2008b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 2009b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 20108177437dSPoul-Henning Kamp read = bp->bio_cmd == BIO_READ; 20111a6bed68SJoerg Wunsch mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0; 20121a6bed68SJoerg Wunsch steptrac = (fd->ft->flags & FL_2STEP)? 2: 1; 201356a23089SPoul-Henning Kamp if (read) 201456a23089SPoul-Henning Kamp idf = ISADMA_READ; 201556a23089SPoul-Henning Kamp else 201656a23089SPoul-Henning Kamp idf = ISADMA_WRITE; 201764860614SJoerg Wunsch format = bp->bio_cmd == BIO_FORMAT; 2018f664aeeeSJoerg Wunsch rdsectid = bp->bio_cmd == BIO_RDSECTID; 2019fb35bd37SJoerg Wunsch if (format) 20208177437dSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data; 20215b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 20225b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 20235b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 20246182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 20256182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 20265b81b6b3SRodney W. Grimes switch (fdc->state) 20275b81b6b3SRodney W. Grimes { 20285b81b6b3SRodney W. Grimes case DEVIDLE: 20295b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 20305b81b6b3SRodney W. Grimes fdc->retry = 0; 20315b81b6b3SRodney W. Grimes fd->skip = 0; 20325b81b6b3SRodney W. Grimes fdc->fd = fd; 20335b81b6b3SRodney W. Grimes fdc->fdu = fdu; 20345f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 20353a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 2036246ed35dSJoerg Wunsch /* 2037246ed35dSJoerg Wunsch * If the next drive has a motor startup pending, then 2038246ed35dSJoerg Wunsch * it will start up in its own good time. 2039246ed35dSJoerg Wunsch */ 20406182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 20415b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 2042246ed35dSJoerg Wunsch return (0); /* will return later */ 20435b81b6b3SRodney W. Grimes } 2044246ed35dSJoerg Wunsch /* 2045246ed35dSJoerg Wunsch * Maybe if it's not starting, it SHOULD be starting. 2046246ed35dSJoerg Wunsch */ 20475b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 20485b81b6b3SRodney W. Grimes { 20495b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 20506182fdbdSPeter Wemm fd_turnon(fd); 2051246ed35dSJoerg Wunsch return (0); /* will return later */ 20525b81b6b3SRodney W. Grimes } 20535b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 20545b81b6b3SRodney W. Grimes { 20556182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 20565b81b6b3SRodney W. Grimes } 20575c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 20585c1a1eaeSBruce Evans fdc->state = RESETCTLR; 20595c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 20605c1a1eaeSBruce Evans } else 20615e235068SJordan K. Hubbard fdc->state = DOSEEK; 2062246ed35dSJoerg Wunsch return (1); /* will return immediately */ 2063fb35bd37SJoerg Wunsch 20645b81b6b3SRodney W. Grimes case DOSEEK: 2065fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 2066fb35bd37SJoerg Wunsch cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 2067fb35bd37SJoerg Wunsch if (cylinder == fd->track) 20685b81b6b3SRodney W. Grimes { 20695b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 2070246ed35dSJoerg Wunsch return (1); /* will return immediately */ 20715b81b6b3SRodney W. Grimes } 20726182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 20731a6bed68SJoerg Wunsch fd->fdsu, cylinder * steptrac, 0)) 2074dc8603e3SJoerg Wunsch { 2075dc8603e3SJoerg Wunsch /* 2076246ed35dSJoerg Wunsch * Seek command not accepted, looks like 2077dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 2078dc8603e3SJoerg Wunsch */ 2079dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 20806182fdbdSPeter Wemm return(retrier(fdc)); 2081dc8603e3SJoerg Wunsch } 2082dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 20835b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 20845b81b6b3SRodney W. Grimes return(0); /* will return later */ 2085fb35bd37SJoerg Wunsch 20865b81b6b3SRodney W. Grimes case SEEKWAIT: 20875b81b6b3SRodney W. Grimes /* allow heads to settle */ 20886182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 20895b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 20905b81b6b3SRodney W. Grimes return(0); /* will return later */ 2091fb35bd37SJoerg Wunsch 2092246ed35dSJoerg Wunsch case SEEKCOMPLETE : /* seek done, start DMA */ 2093fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 2094fb35bd37SJoerg Wunsch cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 2095fb35bd37SJoerg Wunsch 2096246ed35dSJoerg Wunsch /* Make sure seek really happened. */ 20976182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 20981a6bed68SJoerg Wunsch int descyl = cylinder * steptrac; 20993a2f7427SDavid Greenman do { 21003a2f7427SDavid Greenman /* 2101dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 2102dc5df763SJoerg Wunsch * which cannot really happen since the 2103dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 2104dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 2105dc5df763SJoerg Wunsch * line, so do one of the following: 2106dc5df763SJoerg Wunsch * 2107dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 2108dc5df763SJoerg Wunsch * known to not go stuck after responding 2109dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 2110dc5df763SJoerg Wunsch * until seeing either an INVALID or a 2111dc5df763SJoerg Wunsch * real interrupt condition. 2112dc5df763SJoerg Wunsch * 2113dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 2114dc5df763SJoerg Wunsch * up immediately. The controller will 2115dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 2116dc5df763SJoerg Wunsch * conditions right after reset (for the 2117dc5df763SJoerg Wunsch * corresponding four drives), so this is 2118dc5df763SJoerg Wunsch * our only chance to get notice that it 2119dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 21203a2f7427SDavid Greenman */ 2121dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 2122dc5df763SJoerg Wunsch == FD_NOT_VALID) 2123246ed35dSJoerg Wunsch return (0); /* will return later */ 2124dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2125dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2126246ed35dSJoerg Wunsch return (0); /* hope for a real intr */ 21273a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2128dc5df763SJoerg Wunsch 21296182fdbdSPeter Wemm if (0 == descyl) { 2130dc5df763SJoerg Wunsch int failed = 0; 21313a2f7427SDavid Greenman /* 21323a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 21333a2f7427SDavid Greenman * really there 21343a2f7427SDavid Greenman */ 2135dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 2136dc5df763SJoerg Wunsch failed = 1; 21373a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 21383a2f7427SDavid Greenman printf( 21393a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 21403a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 2141dc5df763SJoerg Wunsch failed = 1; 2142dc5df763SJoerg Wunsch } 2143dc5df763SJoerg Wunsch 21446182fdbdSPeter Wemm if (failed) { 21453a2f7427SDavid Greenman if(fdc->retry < 3) 21463a2f7427SDavid Greenman fdc->retry = 3; 21476182fdbdSPeter Wemm return (retrier(fdc)); 21483a2f7427SDavid Greenman } 21493a2f7427SDavid Greenman } 2150dc5df763SJoerg Wunsch 21516182fdbdSPeter Wemm if (cyl != descyl) { 21523a2f7427SDavid Greenman printf( 21533a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 21542d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 2155e5d7d243SBruce Evans if (fdc->retry < 3) 2156e5d7d243SBruce Evans fdc->retry = 3; 21576182fdbdSPeter Wemm return (retrier(fdc)); 21585b81b6b3SRodney W. Grimes } 21595b81b6b3SRodney W. Grimes } 21605b81b6b3SRodney W. Grimes 2161fb35bd37SJoerg Wunsch fd->track = cylinder; 2162fb35bd37SJoerg Wunsch if (format) 2163fb35bd37SJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 2164fb35bd37SJoerg Wunsch - (char *)finfo; 2165250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 21668177437dSPoul-Henning Kamp isa_dmastart(idf, bp->bio_data+fd->skip, 21678177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 2168fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 21695b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 21705b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 21715b81b6b3SRodney W. Grimes head = sec / sectrac; 21725b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 21731a6bed68SJoerg Wunsch if (head != 0 && fd->ft->offset_side2 != 0) 21741a6bed68SJoerg Wunsch sec += fd->ft->offset_side2; 21753a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 21763a2f7427SDavid Greenman 2177250300ebSJoerg Wunsch if(format || !(read || rdsectid)) 21783a2f7427SDavid Greenman { 21793a2f7427SDavid Greenman /* make sure the drive is writable */ 2180dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 2181dc8603e3SJoerg Wunsch { 2182dc8603e3SJoerg Wunsch /* stuck controller? */ 21835f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 218456a23089SPoul-Henning Kamp isa_dmadone(idf, 21858177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 21868177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 21875c1a1eaeSBruce Evans fdc->dmachan); 2188dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 21896182fdbdSPeter Wemm return (retrier(fdc)); 2190dc8603e3SJoerg Wunsch } 21913a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 21923a2f7427SDavid Greenman { 21933a2f7427SDavid Greenman /* 21943a2f7427SDavid Greenman * XXX YES! this is ugly. 21953a2f7427SDavid Greenman * in order to force the current operation 21963a2f7427SDavid Greenman * to fail, we will have to fake an FDC 21973a2f7427SDavid Greenman * error - all error handling is done 21983a2f7427SDavid Greenman * by the retrier() 21993a2f7427SDavid Greenman */ 22003a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 22013a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 22023a2f7427SDavid Greenman fdc->status[2] = 0; 22033a2f7427SDavid Greenman fdc->status[3] = fd->track; 22043a2f7427SDavid Greenman fdc->status[4] = head; 22053a2f7427SDavid Greenman fdc->status[5] = sec; 22063a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 22073a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 2208246ed35dSJoerg Wunsch return (1); /* will return immediately */ 22093a2f7427SDavid Greenman } 22103a2f7427SDavid Greenman } 22115b81b6b3SRodney W. Grimes 22126182fdbdSPeter Wemm if (format) { 22131a6bed68SJoerg Wunsch ne7cmd = NE7CMD_FORMAT | mfm; 22145f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 22155f830ea2SJoerg Wunsch /* 22165f830ea2SJoerg Wunsch * This seems to be necessary for 22175f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 22185f830ea2SJoerg Wunsch * it, we end up filling the sector ID 22195f830ea2SJoerg Wunsch * fields of the newly formatted track 22205f830ea2SJoerg Wunsch * entirely with garbage, causing 22215f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 22225f830ea2SJoerg Wunsch * the place when trying to read them 22235f830ea2SJoerg Wunsch * back. 22245f830ea2SJoerg Wunsch * 22255f830ea2SJoerg Wunsch * Umpf. 22265f830ea2SJoerg Wunsch */ 22278177437dSPoul-Henning Kamp SET_BCDR(fdc, 1, bp->bio_bcount, 0); 22285f830ea2SJoerg Wunsch 22298177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 22308177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 22318177437dSPoul-Henning Kamp bp->bio_bcount); 22325f830ea2SJoerg Wunsch 22335f830ea2SJoerg Wunsch } 2234b39c878eSAndrey A. Chernov /* formatting */ 22351a6bed68SJoerg Wunsch if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu, 2236dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 2237dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 2238dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 22396182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 2240dc8603e3SJoerg Wunsch /* controller fell over */ 22415f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 224256a23089SPoul-Henning Kamp isa_dmadone(idf, 22438177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 22448177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 22455c1a1eaeSBruce Evans fdc->dmachan); 2246dc8603e3SJoerg Wunsch fdc->retry = 6; 22476182fdbdSPeter Wemm return (retrier(fdc)); 2248dc8603e3SJoerg Wunsch } 2249250300ebSJoerg Wunsch } else if (rdsectid) { 22501a6bed68SJoerg Wunsch ne7cmd = NE7CMD_READID | mfm; 22511a6bed68SJoerg Wunsch if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) { 2252250300ebSJoerg Wunsch /* controller jamming */ 2253250300ebSJoerg Wunsch fdc->retry = 6; 2254250300ebSJoerg Wunsch return (retrier(fdc)); 2255250300ebSJoerg Wunsch } 22566182fdbdSPeter Wemm } else { 2257250300ebSJoerg Wunsch /* read or write operation */ 22581a6bed68SJoerg Wunsch ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm; 22593b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 226069acd21dSWarner Losh /* 2261246ed35dSJoerg Wunsch * This seems to be necessary even when 2262246ed35dSJoerg Wunsch * reading data. 226369acd21dSWarner Losh */ 22643b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 226569acd21dSWarner Losh 226669acd21dSWarner Losh /* 2267246ed35dSJoerg Wunsch * Perform the write pseudo-DMA before 2268246ed35dSJoerg Wunsch * the WRITE command is sent. 226969acd21dSWarner Losh */ 227069acd21dSWarner Losh if (!read) 22718177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 22728177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 227369acd21dSWarner Losh fdblk); 227469acd21dSWarner Losh } 22756182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 22761a6bed68SJoerg Wunsch ne7cmd, 2277dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 2278dc5df763SJoerg Wunsch fd->track, /* track */ 2279dc5df763SJoerg Wunsch head, 2280dc5df763SJoerg Wunsch sec, /* sector + 1 */ 2281dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 2282dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 2283dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 2284dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 22856182fdbdSPeter Wemm 0)) { 2286dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 22875f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 228856a23089SPoul-Henning Kamp isa_dmadone(idf, 22898177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 22908177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 22915c1a1eaeSBruce Evans fdc->dmachan); 2292dc8603e3SJoerg Wunsch fdc->retry = 6; 22936182fdbdSPeter Wemm return (retrier(fdc)); 22945b81b6b3SRodney W. Grimes } 2295b39c878eSAndrey A. Chernov } 2296250300ebSJoerg Wunsch if (!rdsectid && (fdc->flags & FDC_NODMA)) 229769acd21dSWarner Losh /* 2298246ed35dSJoerg Wunsch * If this is a read, then simply await interrupt 2299246ed35dSJoerg Wunsch * before performing PIO. 230069acd21dSWarner Losh */ 23018177437dSPoul-Henning Kamp if (read && !fdcpio(fdc,bp->bio_cmd, 23028177437dSPoul-Henning Kamp bp->bio_data+fd->skip,fdblk)) { 23033b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 230469acd21dSWarner Losh return(0); /* will return later */ 230564860614SJoerg Wunsch } 230669acd21dSWarner Losh 230769acd21dSWarner Losh /* 2308246ed35dSJoerg Wunsch * Write (or format) operation will fall through and 2309246ed35dSJoerg Wunsch * await completion interrupt. 231069acd21dSWarner Losh */ 23115b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 23126182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 23135b81b6b3SRodney W. Grimes return (0); /* will return later */ 2314fb35bd37SJoerg Wunsch 231569acd21dSWarner Losh case PIOREAD: 231669acd21dSWarner Losh /* 2317246ed35dSJoerg Wunsch * Actually perform the PIO read. The IOCOMPLETE case 231869acd21dSWarner Losh * removes the timeout for us. 231969acd21dSWarner Losh */ 23208177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 232169acd21dSWarner Losh fdc->state = IOCOMPLETE; 232269acd21dSWarner Losh /* FALLTHROUGH */ 2323246ed35dSJoerg Wunsch case IOCOMPLETE: /* IO done, post-analyze */ 23246182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 2325dc5df763SJoerg Wunsch 232664860614SJoerg Wunsch if (fd_read_status(fdc)) { 2327250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 23288177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 23298177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 23305c1a1eaeSBruce Evans fdc->dmachan); 2331dc5df763SJoerg Wunsch if (fdc->retry < 6) 2332dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 23336182fdbdSPeter Wemm return (retrier(fdc)); 23345b81b6b3SRodney W. Grimes } 2335dc5df763SJoerg Wunsch 23363a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 2337dc5df763SJoerg Wunsch 23383a2f7427SDavid Greenman /* FALLTHROUGH */ 23393a2f7427SDavid Greenman case IOTIMEDOUT: 2340250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 23418177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 23428177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 23436182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 23443a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 23453a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 2346b39c878eSAndrey A. Chernov /* 23473fef646eSJoerg Wunsch * DMA overrun. Someone hogged the bus and 23483fef646eSJoerg Wunsch * didn't release it in time for the next 23493fef646eSJoerg Wunsch * FDC transfer. 23503fef646eSJoerg Wunsch * 23513fef646eSJoerg Wunsch * We normally restart this without bumping 23523fef646eSJoerg Wunsch * the retry counter. However, in case 23533fef646eSJoerg Wunsch * something is seriously messed up (like 23543fef646eSJoerg Wunsch * broken hardware), we rather limit the 23553fef646eSJoerg Wunsch * number of retries so the IO operation 23563fef646eSJoerg Wunsch * doesn't block indefinately. 2357b39c878eSAndrey A. Chernov */ 23583fef646eSJoerg Wunsch if (fdc->dma_overruns++ < FDC_DMAOV_MAX) { 2359b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2360246ed35dSJoerg Wunsch return (1);/* will return immediately */ 23613fef646eSJoerg Wunsch } /* else fall through */ 2362b39c878eSAndrey A. Chernov } 23633fef646eSJoerg Wunsch if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 23643a2f7427SDavid Greenman && fdc->retry < 6) 23653a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 23663a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 23673a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 23683a2f7427SDavid Greenman && fdc->retry < 3) 23693a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 23706182fdbdSPeter Wemm return (retrier(fdc)); 23715b81b6b3SRodney W. Grimes } 23725b81b6b3SRodney W. Grimes /* All OK */ 2373250300ebSJoerg Wunsch if (rdsectid) { 2374250300ebSJoerg Wunsch /* copy out ID field contents */ 237564860614SJoerg Wunsch idp = (struct fdc_readid *)bp->bio_data; 2376250300ebSJoerg Wunsch idp->cyl = fdc->status[3]; 2377250300ebSJoerg Wunsch idp->head = fdc->status[4]; 2378250300ebSJoerg Wunsch idp->sec = fdc->status[5]; 2379250300ebSJoerg Wunsch idp->secshift = fdc->status[6]; 2380250300ebSJoerg Wunsch } 23813fef646eSJoerg Wunsch /* Operation successful, retry DMA overruns again next time. */ 23823fef646eSJoerg Wunsch fdc->dma_overruns = 0; 23833a2f7427SDavid Greenman fd->skip += fdblk; 2384fb35bd37SJoerg Wunsch if (!rdsectid && !format && fd->skip < bp->bio_bcount) { 23855b81b6b3SRodney W. Grimes /* set up next transfer */ 23865b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 23876182fdbdSPeter Wemm } else { 23885b81b6b3SRodney W. Grimes /* ALL DONE */ 23895b81b6b3SRodney W. Grimes fd->skip = 0; 2390fb35bd37SJoerg Wunsch bp->bio_resid = 0; 2391e93e63cbSBruce Evans fdc->bp = NULL; 23925f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2393a468031cSPoul-Henning Kamp biofinish(bp, &fd->device_stats, 0); 23945b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 23955b81b6b3SRodney W. Grimes fdc->fdu = -1; 23965b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 23975b81b6b3SRodney W. Grimes } 2398246ed35dSJoerg Wunsch return (1); /* will return immediately */ 2399fb35bd37SJoerg Wunsch 24005b81b6b3SRodney W. Grimes case RESETCTLR: 24013a2f7427SDavid Greenman fdc_reset(fdc); 24025b81b6b3SRodney W. Grimes fdc->retry++; 24035c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 2404246ed35dSJoerg Wunsch return (0); /* will return later */ 2405fb35bd37SJoerg Wunsch 24065c1a1eaeSBruce Evans case RESETCOMPLETE: 24075c1a1eaeSBruce Evans /* 24085c1a1eaeSBruce Evans * Discard all the results from the reset so that they 24095c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 24105c1a1eaeSBruce Evans */ 24110e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 24120e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 24135c1a1eaeSBruce Evans fdc->state = STARTRECAL; 2414fb35bd37SJoerg Wunsch /* FALLTHROUGH */ 24155c1a1eaeSBruce Evans case STARTRECAL: 24166182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2417dc8603e3SJoerg Wunsch /* arrgl */ 2418dc8603e3SJoerg Wunsch fdc->retry = 6; 24196182fdbdSPeter Wemm return (retrier(fdc)); 2420dc8603e3SJoerg Wunsch } 24215b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 24225b81b6b3SRodney W. Grimes return (0); /* will return later */ 2423fb35bd37SJoerg Wunsch 24245b81b6b3SRodney W. Grimes case RECALWAIT: 24255b81b6b3SRodney W. Grimes /* allow heads to settle */ 24266182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 24275b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 24285b81b6b3SRodney W. Grimes return (0); /* will return later */ 2429fb35bd37SJoerg Wunsch 24305b81b6b3SRodney W. Grimes case RECALCOMPLETE: 24313a2f7427SDavid Greenman do { 24323a2f7427SDavid Greenman /* 2433dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 24343a2f7427SDavid Greenman */ 2435dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2436246ed35dSJoerg Wunsch return (0); /* will return later */ 2437dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2438dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2439246ed35dSJoerg Wunsch return (0); /* hope for a real intr */ 24403a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 24413a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 24425b81b6b3SRodney W. Grimes { 2443dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2444dc8603e3SJoerg Wunsch /* 2445246ed35dSJoerg Wunsch * A recalibrate from beyond cylinder 77 2446dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2447dc8603e3SJoerg Wunsch * since people used to complain much about 2448dc8603e3SJoerg Wunsch * the failure message, try not logging 2449dc8603e3SJoerg Wunsch * this one if it seems to be the first 2450246ed35dSJoerg Wunsch * time in a line. 2451dc8603e3SJoerg Wunsch */ 2452dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2453dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 24543a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 24556182fdbdSPeter Wemm return (retrier(fdc)); 24565b81b6b3SRodney W. Grimes } 24575b81b6b3SRodney W. Grimes fd->track = 0; 24585b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 24595b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 2460246ed35dSJoerg Wunsch return (1); /* will return immediately */ 2461fb35bd37SJoerg Wunsch 24625b81b6b3SRodney W. Grimes case MOTORWAIT: 24635b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 24645b81b6b3SRodney W. Grimes { 24655b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 24665b81b6b3SRodney W. Grimes } 24675c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 24685c1a1eaeSBruce Evans fdc->state = RESETCTLR; 24695c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 2470fb35bd37SJoerg Wunsch } else 2471fb35bd37SJoerg Wunsch fdc->state = DOSEEK; 2472246ed35dSJoerg Wunsch return (1); /* will return immediately */ 2473fb35bd37SJoerg Wunsch 24745b81b6b3SRodney W. Grimes default: 2475b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 247664860614SJoerg Wunsch if (fd_read_status(fdc) == 0) 2477a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 24785b81b6b3SRodney W. Grimes fdc->status[0], 24795b81b6b3SRodney W. Grimes fdc->status[1], 24805b81b6b3SRodney W. Grimes fdc->status[2], 24815b81b6b3SRodney W. Grimes fdc->status[3], 24825b81b6b3SRodney W. Grimes fdc->status[4], 24835b81b6b3SRodney W. Grimes fdc->status[5], 24845b81b6b3SRodney W. Grimes fdc->status[6] ); 24853a2f7427SDavid Greenman else 2486dac0f2dbSJoerg Wunsch printf("No status available "); 2487dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2488dac0f2dbSJoerg Wunsch { 2489dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 2490246ed35dSJoerg Wunsch return (0); /* will return later */ 24915b81b6b3SRodney W. Grimes } 2492dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2493246ed35dSJoerg Wunsch return (0); /* will return later */ 2494dac0f2dbSJoerg Wunsch } 2495246ed35dSJoerg Wunsch /* noone should ever get here */ 24965b81b6b3SRodney W. Grimes } 24975b81b6b3SRodney W. Grimes 2498aaf08d94SGarrett Wollman static int 24996182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 25005b81b6b3SRodney W. Grimes { 25018177437dSPoul-Henning Kamp struct bio *bp; 25026182fdbdSPeter Wemm struct fd_data *fd; 25036182fdbdSPeter Wemm int fdu; 25045b81b6b3SRodney W. Grimes 2505e93e63cbSBruce Evans bp = fdc->bp; 25065b81b6b3SRodney W. Grimes 25076182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 25088177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 25096182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 25106182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 25113a2f7427SDavid Greenman goto fail; 25126182fdbdSPeter Wemm 25136182fdbdSPeter Wemm switch (fdc->retry) { 25145b81b6b3SRodney W. Grimes case 0: case 1: case 2: 25155b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 25165b81b6b3SRodney W. Grimes break; 25175b81b6b3SRodney W. Grimes case 3: case 4: case 5: 25185b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 25195b81b6b3SRodney W. Grimes break; 25205b81b6b3SRodney W. Grimes case 6: 25215b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 25225b81b6b3SRodney W. Grimes break; 25235b81b6b3SRodney W. Grimes case 7: 25245b81b6b3SRodney W. Grimes break; 25255b81b6b3SRodney W. Grimes default: 25263a2f7427SDavid Greenman fail: 2527fb35bd37SJoerg Wunsch if ((fd->options & FDOPT_NOERRLOG) == 0) { 2528fb35bd37SJoerg Wunsch diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE, 25293a2f7427SDavid Greenman (struct disklabel *)NULL); 2530fb35bd37SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) { 2531dc5df763SJoerg Wunsch printf( 2532a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2533dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2534dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2535dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2536dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2537dc5df763SJoerg Wunsch fdc->status[5]); 2538dc5df763SJoerg Wunsch } 2539dc5df763SJoerg Wunsch else 2540dc5df763SJoerg Wunsch printf(" (No status)\n"); 25415b81b6b3SRodney W. Grimes } 25422995d110SJoerg Wunsch if ((fd->options & FDOPT_NOERROR) == 0) { 25438177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 25448177437dSPoul-Henning Kamp bp->bio_error = EIO; 2545fb35bd37SJoerg Wunsch bp->bio_resid = bp->bio_bcount - fdc->fd->skip; 2546fb35bd37SJoerg Wunsch } else 2547fb35bd37SJoerg Wunsch bp->bio_resid = 0; 2548e93e63cbSBruce Evans fdc->bp = NULL; 25495b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 25505f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2551a468031cSPoul-Henning Kamp biofinish(bp, &fdc->fd->device_stats, 0); 255292ed385aSRodney W. Grimes fdc->state = FINDWORK; 25535c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 25545b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 25555b81b6b3SRodney W. Grimes fdc->fdu = -1; 255692ed385aSRodney W. Grimes return (1); 25575b81b6b3SRodney W. Grimes } 25585b81b6b3SRodney W. Grimes fdc->retry++; 25595b81b6b3SRodney W. Grimes return (1); 25605b81b6b3SRodney W. Grimes } 25615b81b6b3SRodney W. Grimes 25621fdb6e6cSPoul-Henning Kamp static void 25631fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp) 25641fdb6e6cSPoul-Henning Kamp { 25651fdb6e6cSPoul-Henning Kamp wakeup(bp); 25661fdb6e6cSPoul-Henning Kamp } 25671fdb6e6cSPoul-Henning Kamp 2568b39c878eSAndrey A. Chernov static int 2569f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data) 2570b39c878eSAndrey A. Chernov { 2571b39c878eSAndrey A. Chernov fdu_t fdu; 2572b39c878eSAndrey A. Chernov fd_p fd; 25731fdb6e6cSPoul-Henning Kamp struct bio *bp; 2574f664aeeeSJoerg Wunsch struct fd_formb *finfo; 2575f664aeeeSJoerg Wunsch struct fdc_readid *idfield; 25763a2f7427SDavid Greenman size_t fdblk; 2577b39c878eSAndrey A. Chernov 2578b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 25796182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 25803a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2581f664aeeeSJoerg Wunsch finfo = (struct fd_formb *)data; 2582f664aeeeSJoerg Wunsch idfield = (struct fdc_readid *)data; 2583b39c878eSAndrey A. Chernov 2584fb35bd37SJoerg Wunsch bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO); 2585b39c878eSAndrey A. Chernov 2586b39c878eSAndrey A. Chernov /* 2587f664aeeeSJoerg Wunsch * Set up a bio request for fdstrategy(). bio_blkno is faked 2588f664aeeeSJoerg Wunsch * so that fdstrategy() will seek to the the requested 2589817988beSPoul-Henning Kamp * cylinder, and use the desired head. 2590b39c878eSAndrey A. Chernov */ 2591f664aeeeSJoerg Wunsch bp->bio_cmd = cmd; 2592f664aeeeSJoerg Wunsch if (cmd == BIO_FORMAT) { 2593f664aeeeSJoerg Wunsch bp->bio_blkno = 2594f664aeeeSJoerg Wunsch (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2595f664aeeeSJoerg Wunsch finfo->head * fd->ft->sectrac) * 2596f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2597f664aeeeSJoerg Wunsch bp->bio_bcount = sizeof(struct fd_idfield_data) * 2598f664aeeeSJoerg Wunsch finfo->fd_formb_nsecs; 2599f664aeeeSJoerg Wunsch } else if (cmd == BIO_RDSECTID) { 2600f664aeeeSJoerg Wunsch bp->bio_blkno = 2601f664aeeeSJoerg Wunsch (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2602f664aeeeSJoerg Wunsch idfield->head * fd->ft->sectrac) * 2603f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2604250300ebSJoerg Wunsch bp->bio_bcount = sizeof(struct fdc_readid); 2605f664aeeeSJoerg Wunsch } else 2606f664aeeeSJoerg Wunsch panic("wrong cmd in fdmisccmd()"); 2607f664aeeeSJoerg Wunsch bp->bio_data = data; 2608250300ebSJoerg Wunsch bp->bio_dev = dev; 2609250300ebSJoerg Wunsch bp->bio_done = fdbiodone; 2610817988beSPoul-Henning Kamp bp->bio_flags = 0; 2611250300ebSJoerg Wunsch 2612f5f7ba03SJordan K. Hubbard /* 2613fb35bd37SJoerg Wunsch * Now run the command. The wait loop is a version of bufwait() 2614fb35bd37SJoerg Wunsch * adapted for struct bio instead of struct buf and specialized 2615fb35bd37SJoerg Wunsch * for the current context. 2616f5f7ba03SJordan K. Hubbard */ 2617f664aeeeSJoerg Wunsch fdstrategy(bp); 2618f664aeeeSJoerg Wunsch while ((bp->bio_flags & BIO_DONE) == 0) 2619f664aeeeSJoerg Wunsch tsleep(bp, PRIBIO, "fdcmd", 0); 2620f664aeeeSJoerg Wunsch 2621f664aeeeSJoerg Wunsch free(bp, M_TEMP); 2622f664aeeeSJoerg Wunsch return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0); 2623f664aeeeSJoerg Wunsch } 26245b81b6b3SRodney W. Grimes 26253e425b96SJulian Elischer static int 2626b40ce416SJulian Elischer fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 2627f5f7ba03SJordan K. Hubbard { 2628f664aeeeSJoerg Wunsch fdu_t fdu; 2629f664aeeeSJoerg Wunsch fd_p fd; 2630f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2631fb35bd37SJoerg Wunsch struct disklabel *lp; 26322995d110SJoerg Wunsch struct fdc_status *fsp; 2633250300ebSJoerg Wunsch struct fdc_readid *rid; 2634f664aeeeSJoerg Wunsch size_t fdblk; 26351a6bed68SJoerg Wunsch int error, type; 2636f5f7ba03SJordan K. Hubbard 2637f664aeeeSJoerg Wunsch fdu = FDUNIT(minor(dev)); 26381a6bed68SJoerg Wunsch type = FDTYPE(minor(dev)); 2639f664aeeeSJoerg Wunsch fd = devclass_get_softc(fd_devclass, fdu); 2640fb35bd37SJoerg Wunsch 26411a6bed68SJoerg Wunsch /* 26421a6bed68SJoerg Wunsch * First, handle everything that could be done with 26431a6bed68SJoerg Wunsch * FD_NONBLOCK still being set. 26441a6bed68SJoerg Wunsch */ 26451a6bed68SJoerg Wunsch switch (cmd) { 26461a6bed68SJoerg Wunsch case FIONBIO: 26471a6bed68SJoerg Wunsch if (*(int *)addr != 0) 26481a6bed68SJoerg Wunsch fd->flags |= FD_NONBLOCK; 26491a6bed68SJoerg Wunsch else { 26501a6bed68SJoerg Wunsch if (fd->ft == 0) { 26511a6bed68SJoerg Wunsch /* 26521a6bed68SJoerg Wunsch * No drive type has been selected yet, 26531a6bed68SJoerg Wunsch * cannot turn FNONBLOCK off. 26541a6bed68SJoerg Wunsch */ 26551a6bed68SJoerg Wunsch return (EINVAL); 26561a6bed68SJoerg Wunsch } 26571a6bed68SJoerg Wunsch fd->flags &= ~FD_NONBLOCK; 26581a6bed68SJoerg Wunsch } 26591a6bed68SJoerg Wunsch return (0); 2660fb35bd37SJoerg Wunsch 26611a6bed68SJoerg Wunsch case FIOASYNC: 26621a6bed68SJoerg Wunsch /* keep the generic fcntl() code happy */ 26631a6bed68SJoerg Wunsch return (0); 26641a6bed68SJoerg Wunsch 26651a6bed68SJoerg Wunsch case FD_GTYPE: /* get drive type */ 26661a6bed68SJoerg Wunsch if (fd->ft == 0) 26671a6bed68SJoerg Wunsch /* no type known yet, return the native type */ 26681a6bed68SJoerg Wunsch *(struct fd_type *)addr = fd_native_types[fd->type]; 26691a6bed68SJoerg Wunsch else 26701a6bed68SJoerg Wunsch *(struct fd_type *)addr = *fd->ft; 26711a6bed68SJoerg Wunsch return (0); 26721a6bed68SJoerg Wunsch 26731a6bed68SJoerg Wunsch case FD_STYPE: /* set drive type */ 26741a6bed68SJoerg Wunsch if (type == 0) { 26751a6bed68SJoerg Wunsch /* 26761a6bed68SJoerg Wunsch * Allow setting drive type temporarily iff 26771a6bed68SJoerg Wunsch * currently unset. Used for fdformat so any 26781a6bed68SJoerg Wunsch * user can set it, and then start formatting. 26791a6bed68SJoerg Wunsch */ 26801a6bed68SJoerg Wunsch if (fd->ft) 26811a6bed68SJoerg Wunsch return (EINVAL); /* already set */ 26821a6bed68SJoerg Wunsch fd->ft = fd->fts; 26831a6bed68SJoerg Wunsch *fd->ft = *(struct fd_type *)addr; 26841a6bed68SJoerg Wunsch fd->flags |= FD_UA; 26851a6bed68SJoerg Wunsch } else { 26861a6bed68SJoerg Wunsch /* 26871a6bed68SJoerg Wunsch * Set density definition permanently. Only 26881a6bed68SJoerg Wunsch * allow for superuser. 26891a6bed68SJoerg Wunsch */ 269044731cabSJohn Baldwin if (suser(td) != 0) 26911a6bed68SJoerg Wunsch return (EPERM); 26921a6bed68SJoerg Wunsch fd->fts[type] = *(struct fd_type *)addr; 26931a6bed68SJoerg Wunsch } 26941a6bed68SJoerg Wunsch return (0); 26951a6bed68SJoerg Wunsch 26961a6bed68SJoerg Wunsch case FD_GOPTS: /* get drive options */ 26971a6bed68SJoerg Wunsch *(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0); 26981a6bed68SJoerg Wunsch return (0); 26991a6bed68SJoerg Wunsch 27001a6bed68SJoerg Wunsch case FD_SOPTS: /* set drive options */ 27011a6bed68SJoerg Wunsch fd->options = *(int *)addr & ~FDOPT_AUTOSEL; 27021a6bed68SJoerg Wunsch return (0); 27031a6bed68SJoerg Wunsch 27041a6bed68SJoerg Wunsch #ifdef FDC_DEBUG 27051a6bed68SJoerg Wunsch case FD_DEBUG: 27061a6bed68SJoerg Wunsch if ((fd_debug != 0) != (*(int *)addr != 0)) { 27071a6bed68SJoerg Wunsch fd_debug = (*(int *)addr != 0); 27081a6bed68SJoerg Wunsch printf("fd%d: debugging turned %s\n", 27091a6bed68SJoerg Wunsch fd->fdu, fd_debug ? "on" : "off"); 27101a6bed68SJoerg Wunsch } 27111a6bed68SJoerg Wunsch return (0); 27121a6bed68SJoerg Wunsch #endif 27131a6bed68SJoerg Wunsch 27141a6bed68SJoerg Wunsch case FD_CLRERR: 271544731cabSJohn Baldwin if (suser(td) != 0) 27161a6bed68SJoerg Wunsch return (EPERM); 27171a6bed68SJoerg Wunsch fd->fdc->fdc_errs = 0; 27181a6bed68SJoerg Wunsch return (0); 27191a6bed68SJoerg Wunsch 27201a6bed68SJoerg Wunsch case FD_GSTAT: 27211a6bed68SJoerg Wunsch fsp = (struct fdc_status *)addr; 27221a6bed68SJoerg Wunsch if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 27231a6bed68SJoerg Wunsch return (EINVAL); 27241a6bed68SJoerg Wunsch memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 27251a6bed68SJoerg Wunsch return (0); 27261a6bed68SJoerg Wunsch 27271a6bed68SJoerg Wunsch case FD_GDTYPE: 27281a6bed68SJoerg Wunsch *(enum fd_drivetype *)addr = fd->type; 27291a6bed68SJoerg Wunsch return (0); 27301a6bed68SJoerg Wunsch } 27311a6bed68SJoerg Wunsch 27321a6bed68SJoerg Wunsch /* 27331a6bed68SJoerg Wunsch * Now handle everything else. Make sure we have a valid 27341a6bed68SJoerg Wunsch * drive type. 27351a6bed68SJoerg Wunsch */ 27361a6bed68SJoerg Wunsch if (fd->flags & FD_NONBLOCK) 27371a6bed68SJoerg Wunsch return (EAGAIN); 27381a6bed68SJoerg Wunsch if (fd->ft == 0) 27391a6bed68SJoerg Wunsch return (ENXIO); 27403a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2741fb35bd37SJoerg Wunsch error = 0; 2742f5f7ba03SJordan K. Hubbard 27436182fdbdSPeter Wemm switch (cmd) { 2744f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2745fb35bd37SJoerg Wunsch lp = malloc(sizeof(*lp), M_TEMP, M_ZERO); 2746fb35bd37SJoerg Wunsch lp->d_secsize = fdblk; 27476182fdbdSPeter Wemm fdt = fd->ft; 2748fb35bd37SJoerg Wunsch lp->d_secpercyl = fdt->size / fdt->tracks; 2749fb35bd37SJoerg Wunsch lp->d_type = DTYPE_FLOPPY; 275084954bb8SJoerg Wunsch if (readdisklabel(dev, lp) != NULL) 2751f5f7ba03SJordan K. Hubbard error = EINVAL; 2752fb35bd37SJoerg Wunsch else 2753fb35bd37SJoerg Wunsch *(struct disklabel *)addr = *lp; 2754fb35bd37SJoerg Wunsch free(lp, M_TEMP); 2755f5f7ba03SJordan K. Hubbard break; 2756f5f7ba03SJordan K. Hubbard 2757f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2758f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2759fb35bd37SJoerg Wunsch return (EBADF); 2760fb35bd37SJoerg Wunsch /* 2761fb35bd37SJoerg Wunsch * XXX perhaps should call setdisklabel() to do error checking 2762fb35bd37SJoerg Wunsch * although there is nowhere to "set" the result. Perhaps 2763fb35bd37SJoerg Wunsch * should always just fail. 2764fb35bd37SJoerg Wunsch */ 2765f5f7ba03SJordan K. Hubbard break; 2766f5f7ba03SJordan K. Hubbard 2767f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2768f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2769fb35bd37SJoerg Wunsch return (EBADF); 2770f5f7ba03SJordan K. Hubbard break; 2771f5f7ba03SJordan K. Hubbard 2772f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 2773fb35bd37SJoerg Wunsch if ((flag & FWRITE) == 0) 2774fb35bd37SJoerg Wunsch return (EBADF); 2775fb35bd37SJoerg Wunsch lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO); 2776fb35bd37SJoerg Wunsch error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0); 2777fb35bd37SJoerg Wunsch if (error != 0) 2778fb35bd37SJoerg Wunsch error = writedisklabel(dev, lp); 2779fb35bd37SJoerg Wunsch free(lp, M_TEMP); 2780b39c878eSAndrey A. Chernov break; 2781f664aeeeSJoerg Wunsch 2782b39c878eSAndrey A. Chernov case FD_FORM: 2783b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2784fb35bd37SJoerg Wunsch return (EBADF); /* must be opened for writing */ 2785fb35bd37SJoerg Wunsch if (((struct fd_formb *)addr)->format_version != 2786b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2787fb35bd37SJoerg Wunsch return (EINVAL); /* wrong version of formatting prog */ 2788f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_FORMAT, addr); 2789b39c878eSAndrey A. Chernov break; 2790b39c878eSAndrey A. Chernov 2791b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 27923e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2793f5f7ba03SJordan K. Hubbard break; 2794f5f7ba03SJordan K. Hubbard 27953a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 27963a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 279744731cabSJohn Baldwin if (suser(td) != 0) 2798fb35bd37SJoerg Wunsch return (EPERM); 27993e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 28003a2f7427SDavid Greenman break; 28013a2f7427SDavid Greenman 28023a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 28033e425b96SJulian Elischer *(int *)addr = fd->options; 28043a2f7427SDavid Greenman break; 28053a2f7427SDavid Greenman 28063a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 28073e425b96SJulian Elischer fd->options = *(int *)addr; 28083a2f7427SDavid Greenman break; 28093a2f7427SDavid Greenman 2810f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG 2811f664aeeeSJoerg Wunsch case FD_DEBUG: 28120e17a5bcSJoerg Wunsch if ((fd_debug != 0) != (*(int *)addr != 0)) { 28130e17a5bcSJoerg Wunsch fd_debug = (*(int *)addr != 0); 28140e17a5bcSJoerg Wunsch printf("fd%d: debugging turned %s\n", 28150e17a5bcSJoerg Wunsch fd->fdu, fd_debug ? "on" : "off"); 28160e17a5bcSJoerg Wunsch } 2817f664aeeeSJoerg Wunsch break; 2818f664aeeeSJoerg Wunsch #endif 2819f664aeeeSJoerg Wunsch 28202995d110SJoerg Wunsch case FD_CLRERR: 282144731cabSJohn Baldwin if (suser(td) != 0) 2822fb35bd37SJoerg Wunsch return (EPERM); 28232995d110SJoerg Wunsch fd->fdc->fdc_errs = 0; 28242995d110SJoerg Wunsch break; 28252995d110SJoerg Wunsch 28262995d110SJoerg Wunsch case FD_GSTAT: 28272995d110SJoerg Wunsch fsp = (struct fdc_status *)addr; 28282995d110SJoerg Wunsch if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2829fb35bd37SJoerg Wunsch return (EINVAL); 28302995d110SJoerg Wunsch memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 28312995d110SJoerg Wunsch break; 28322995d110SJoerg Wunsch 2833250300ebSJoerg Wunsch case FD_READID: 2834250300ebSJoerg Wunsch rid = (struct fdc_readid *)addr; 2835250300ebSJoerg Wunsch if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2836fb35bd37SJoerg Wunsch return (EINVAL); 2837f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_RDSECTID, addr); 2838250300ebSJoerg Wunsch break; 2839250300ebSJoerg Wunsch 2840f5f7ba03SJordan K. Hubbard default: 28413a2f7427SDavid Greenman error = ENOTTY; 2842f5f7ba03SJordan K. Hubbard break; 2843f5f7ba03SJordan K. Hubbard } 2844f5f7ba03SJordan K. Hubbard return (error); 2845f5f7ba03SJordan K. Hubbard } 2846