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> 68fb919e4dSMark Murray #include <sys/kernel.h> 69fb919e4dSMark Murray #include <sys/lock.h> 70b99f0a4aSAndrew Moore #include <sys/malloc.h> 716182fdbdSPeter Wemm #include <sys/module.h> 72fb919e4dSMark Murray #include <sys/mutex.h> 733a2f7427SDavid Greenman #include <sys/proc.h> 74b99f0a4aSAndrew Moore #include <sys/syslog.h> 756182fdbdSPeter Wemm 766182fdbdSPeter Wemm #include <machine/bus.h> 776182fdbdSPeter Wemm #include <sys/rman.h> 786182fdbdSPeter Wemm 796182fdbdSPeter Wemm #include <machine/clock.h> 806182fdbdSPeter Wemm #include <machine/resource.h> 81dc5df763SJoerg Wunsch #include <machine/stdarg.h> 826182fdbdSPeter Wemm 836182fdbdSPeter Wemm #include <isa/isavar.h> 84a97c75b7SDoug Rabson #include <isa/isareg.h> 85a97c75b7SDoug Rabson #include <isa/fdreg.h> 86a97c75b7SDoug Rabson #include <isa/fdc.h> 87a97c75b7SDoug Rabson #include <isa/rtc.h> 886182fdbdSPeter Wemm 890722d6abSJoerg Wunsch /* configuration flags */ 900722d6abSJoerg Wunsch #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 91e34c71eaSJoerg Wunsch #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 920722d6abSJoerg Wunsch 930722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */ 940722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED 0x1000 950722d6abSJoerg Wunsch 96dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 97dc5df763SJoerg Wunsch #define FD_FAILED -1 98dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 99dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 100dc5df763SJoerg Wunsch 1016fb89845SKATO Takenori #define NUMTYPES 17 1026fb89845SKATO Takenori #define NUMDENS (NUMTYPES - 7) 1037ca0641bSAndrey A. Chernov 104b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 105b99f0a4aSAndrew Moore #define FD_1720 1 106b99f0a4aSAndrew Moore #define FD_1480 2 107b99f0a4aSAndrew Moore #define FD_1440 3 108b99f0a4aSAndrew Moore #define FD_1200 4 109b99f0a4aSAndrew Moore #define FD_820 5 110b99f0a4aSAndrew Moore #define FD_800 6 111b99f0a4aSAndrew Moore #define FD_720 7 112b99f0a4aSAndrew Moore #define FD_360 8 1136fb89845SKATO Takenori #define FD_640 9 1146fb89845SKATO Takenori #define FD_1232 10 115ed2fa05eSAndrey A. Chernov 1166fb89845SKATO Takenori #define FD_1480in5_25 11 1176fb89845SKATO Takenori #define FD_1440in5_25 12 1186fb89845SKATO Takenori #define FD_820in5_25 13 1196fb89845SKATO Takenori #define FD_800in5_25 14 1206fb89845SKATO Takenori #define FD_720in5_25 15 1216fb89845SKATO Takenori #define FD_360in5_25 16 1226fb89845SKATO Takenori #define FD_640in5_25 17 123b99f0a4aSAndrew Moore 124f664aeeeSJoerg Wunsch #define BIO_RDSECTID BIO_CMD1 1257ca0641bSAndrey A. Chernov 1266f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] = 1275b81b6b3SRodney W. Grimes { 128126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 129126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 130126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 131126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 132126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 133126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 134126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 135b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 1366fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */ 1376fb89845SKATO Takenori { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */ 138ed2fa05eSAndrey A. Chernov 139126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 140126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 141126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 142126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 143126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 144126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 1456fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */ 1465b81b6b3SRodney W. Grimes }; 1475b81b6b3SRodney W. Grimes 148b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 149dc16046fSJoerg Wunsch 150f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE (128 << 3) 15164860614SJoerg Wunsch #define MAX_CYLINDER 85 /* some people really stress their drives 15264860614SJoerg Wunsch * up to cyl 82 */ 153250300ebSJoerg Wunsch #define MAX_HEAD 1 154f8ce7dd5SJoerg Wunsch 1555b81b6b3SRodney W. Grimes /***********************************************************************\ 1565b81b6b3SRodney W. Grimes * Per controller structure. * 1575b81b6b3SRodney W. Grimes \***********************************************************************/ 1586182fdbdSPeter Wemm static devclass_t fdc_devclass; 1595b81b6b3SRodney W. Grimes 1605b81b6b3SRodney W. Grimes /***********************************************************************\ 1615b81b6b3SRodney W. Grimes * Per drive structure. * 162b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 1635b81b6b3SRodney W. Grimes \***********************************************************************/ 1646182fdbdSPeter Wemm struct fd_data { 165b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 1665b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 1673a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 1685b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 1695b81b6b3SRodney W. Grimes int flags; 1705b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 1715b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 1725b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 1735b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 1745b81b6b3SRodney W. Grimes int skip; 1755b81b6b3SRodney W. Grimes int hddrv; 176dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 1775b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 1783a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 17902a19910SJustin T. Gibbs struct callout_handle toffhandle; 18002a19910SJustin T. Gibbs struct callout_handle tohandle; 181b2dfb1f9SJustin T. Gibbs struct devstat device_stats; 182e219897aSJoerg Wunsch eventhandler_tag clonetag; 183e219897aSJoerg Wunsch dev_t masterdev; 18460444853SJoerg Wunsch #define NCLONEDEVS 10 /* must match the table below */ 18560444853SJoerg Wunsch dev_t clonedevs[NCLONEDEVS]; 1866182fdbdSPeter Wemm device_t dev; 1876182fdbdSPeter Wemm fdu_t fdu; 1886182fdbdSPeter Wemm }; 18937286586SPeter Wemm 19037286586SPeter Wemm struct fdc_ivars { 19137286586SPeter Wemm int fdunit; 19237286586SPeter Wemm }; 1936182fdbdSPeter Wemm static devclass_t fd_devclass; 1945b81b6b3SRodney W. Grimes 1955b81b6b3SRodney W. Grimes /***********************************************************************\ 1965b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 1975b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 1985b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 1995b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 2005b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 2015b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 2025b81b6b3SRodney W. Grimes \***********************************************************************/ 203b99f0a4aSAndrew Moore 2043a2f7427SDavid Greenman /* internal functions */ 20564860614SJoerg Wunsch static driver_intr_t fdc_intr; 2066182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 2073a2f7427SDavid Greenman # define TURNON 1 2083a2f7427SDavid Greenman # define TURNOFF 0 2093a2f7427SDavid Greenman static timeout_t fd_turnoff; 2103a2f7427SDavid Greenman static timeout_t fd_motor_on; 2116182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 2123a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2136182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 21480909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int); 2156182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 2165c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 2173a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2186182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 2196182fdbdSPeter Wemm static int retrier(struct fdc_data *); 220f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *); 2213a2f7427SDavid Greenman 222d66c374fSTor Egge static int enable_fifo(fdc_p fdc); 22380909a7dSJoerg Wunsch static void fd_clone (void *arg, char *name, int namelen, dev_t *dev); 224d66c374fSTor Egge 225d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 226d66c374fSTor Egge 227d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 228cba2a7c6SBruce Evans static char const * const fdstates[] = 2295b81b6b3SRodney W. Grimes { 2305b81b6b3SRodney W. Grimes "DEVIDLE", 2315b81b6b3SRodney W. Grimes "FINDWORK", 2325b81b6b3SRodney W. Grimes "DOSEEK", 2335b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 2345b81b6b3SRodney W. Grimes "IOCOMPLETE", 2355b81b6b3SRodney W. Grimes "RECALCOMPLETE", 2365b81b6b3SRodney W. Grimes "STARTRECAL", 2375b81b6b3SRodney W. Grimes "RESETCTLR", 2385b81b6b3SRodney W. Grimes "SEEKWAIT", 2395b81b6b3SRodney W. Grimes "RECALWAIT", 2405b81b6b3SRodney W. Grimes "MOTORWAIT", 2415c1a1eaeSBruce Evans "IOTIMEDOUT", 2425c1a1eaeSBruce Evans "RESETCOMPLETE", 243ff9607b0SBruce Evans "PIOREAD", 2445b81b6b3SRodney W. Grimes }; 2455b81b6b3SRodney W. Grimes 2463a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 247cba2a7c6SBruce Evans static int volatile fd_debug = 0; 2485b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 2495b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 250d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 2515b81b6b3SRodney W. Grimes #define TRACE0(arg) 2525b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 253d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 2545b81b6b3SRodney W. Grimes 255427ccf00SDoug Rabson static void 256427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 257427ccf00SDoug Rabson { 258427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 259427ccf00SDoug Rabson } 260427ccf00SDoug Rabson 261427ccf00SDoug Rabson static u_int8_t 262427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 263427ccf00SDoug Rabson { 264427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 265427ccf00SDoug Rabson } 266427ccf00SDoug Rabson 267427ccf00SDoug Rabson static void 268427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 269427ccf00SDoug Rabson { 270427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 271427ccf00SDoug Rabson } 272427ccf00SDoug Rabson 273427ccf00SDoug Rabson static u_int8_t 274427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 275427ccf00SDoug Rabson { 276427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 277427ccf00SDoug Rabson } 278427ccf00SDoug Rabson 279427ccf00SDoug Rabson static void 2805f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 281427ccf00SDoug Rabson { 282427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 283427ccf00SDoug Rabson } 284427ccf00SDoug Rabson 28558c9d623SPeter Wemm #if NCARD > 0 2865f830ea2SJoerg Wunsch static void 2875f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 2885f830ea2SJoerg Wunsch { 2895f830ea2SJoerg Wunsch bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 2905f830ea2SJoerg Wunsch } 29158c9d623SPeter Wemm #endif 2925f830ea2SJoerg Wunsch 293427ccf00SDoug Rabson #if 0 294427ccf00SDoug Rabson 295427ccf00SDoug Rabson static u_int8_t 296427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 297427ccf00SDoug Rabson { 298427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 299427ccf00SDoug Rabson } 300427ccf00SDoug Rabson 301427ccf00SDoug Rabson #endif 302427ccf00SDoug Rabson 303f8ce7dd5SJoerg Wunsch /* 30464860614SJoerg Wunsch * The open function is named Fdopen() to avoid confusion with fdopen() 30564860614SJoerg Wunsch * in fd(4). The difference is now only meaningful for debuggers. 306f8ce7dd5SJoerg Wunsch */ 307f8ce7dd5SJoerg Wunsch static d_open_t Fdopen; 30887f6c662SJulian Elischer static d_close_t fdclose; 30987f6c662SJulian Elischer static d_ioctl_t fdioctl; 31087f6c662SJulian Elischer static d_strategy_t fdstrategy; 31187f6c662SJulian Elischer 31287f6c662SJulian Elischer #define CDEV_MAJOR 9 3134e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 3144e2f199eSPoul-Henning Kamp /* open */ Fdopen, 3154e2f199eSPoul-Henning Kamp /* close */ fdclose, 3164e2f199eSPoul-Henning Kamp /* read */ physread, 3174e2f199eSPoul-Henning Kamp /* write */ physwrite, 3184e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 3194e2f199eSPoul-Henning Kamp /* poll */ nopoll, 3204e2f199eSPoul-Henning Kamp /* mmap */ nommap, 3214e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 3224e2f199eSPoul-Henning Kamp /* name */ "fd", 3234e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 3244e2f199eSPoul-Henning Kamp /* dump */ nodump, 3254e2f199eSPoul-Henning Kamp /* psize */ nopsize, 3264e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 3274e2f199eSPoul-Henning Kamp }; 3284e2f199eSPoul-Henning Kamp 329dc5df763SJoerg Wunsch static int 3306182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 331dc5df763SJoerg Wunsch { 3326182fdbdSPeter Wemm fdc->fdc_errs++; 33316b04b6aSJoerg Wunsch if (s) { 334b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 335b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 336b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 337b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 338b6e5f28eSPeter Wemm "logging any more\n"); 33916b04b6aSJoerg Wunsch } 340dc5df763SJoerg Wunsch 341dc5df763SJoerg Wunsch return FD_FAILED; 342dc5df763SJoerg Wunsch } 343dc5df763SJoerg Wunsch 344dc5df763SJoerg Wunsch /* 345dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 346dc5df763SJoerg Wunsch * Unit number, 347dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 348dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 349dc5df763SJoerg Wunsch */ 3506f4e0bebSPoul-Henning Kamp static int 3516182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 352dc5df763SJoerg Wunsch { 353dc5df763SJoerg Wunsch u_char cmd; 354dc5df763SJoerg Wunsch int n_in; 355dc5df763SJoerg Wunsch int n; 356dc5df763SJoerg Wunsch va_list ap; 357dc5df763SJoerg Wunsch 358dc5df763SJoerg Wunsch va_start(ap, n_out); 359dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 360dc5df763SJoerg Wunsch va_end(ap); 361dc5df763SJoerg Wunsch va_start(ap, n_out); 362dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 363dc5df763SJoerg Wunsch { 3646182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 365dc5df763SJoerg Wunsch { 366dc5df763SJoerg Wunsch char msg[50]; 3672127f260SArchie Cobbs snprintf(msg, sizeof(msg), 368dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 369dc5df763SJoerg Wunsch cmd, n + 1, n_out); 3706182fdbdSPeter Wemm return fdc_err(fdc, msg); 371dc5df763SJoerg Wunsch } 372dc5df763SJoerg Wunsch } 373dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 374dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 375dc5df763SJoerg Wunsch { 376dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 3776182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 378dc5df763SJoerg Wunsch { 379dc5df763SJoerg Wunsch char msg[50]; 3802127f260SArchie Cobbs snprintf(msg, sizeof(msg), 381dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 382dc5df763SJoerg Wunsch cmd, n + 1, n_in); 3836182fdbdSPeter Wemm return fdc_err(fdc, msg); 384dc5df763SJoerg Wunsch } 385dc5df763SJoerg Wunsch } 386dc5df763SJoerg Wunsch 387dc5df763SJoerg Wunsch return 0; 388dc5df763SJoerg Wunsch } 389dc5df763SJoerg Wunsch 3906f4e0bebSPoul-Henning Kamp static int 391d66c374fSTor Egge enable_fifo(fdc_p fdc) 392d66c374fSTor Egge { 393d66c374fSTor Egge int i, j; 394d66c374fSTor Egge 395d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 396d66c374fSTor Egge 397d66c374fSTor Egge /* 398d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 399d66c374fSTor Egge * this might be an invalid command. Thus we send the 400d66c374fSTor Egge * first byte, and check for an early turn of data directon. 401d66c374fSTor Egge */ 402d66c374fSTor Egge 4036182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 4046182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 405d66c374fSTor Egge 406d66c374fSTor Egge /* If command is invalid, return */ 407d66c374fSTor Egge j = 100000; 408427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 409d66c374fSTor Egge != NE7_RQM && j-- > 0) 410d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 411d66c374fSTor Egge fdc_reset(fdc); 412d66c374fSTor Egge return FD_FAILED; 413d66c374fSTor Egge } 414d66c374fSTor Egge if (j<0 || 4156182fdbdSPeter Wemm fd_cmd(fdc, 3, 416d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 417d66c374fSTor Egge fdc_reset(fdc); 4186182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 419d66c374fSTor Egge } 420d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 421d66c374fSTor Egge return 0; 422d66c374fSTor Egge } 4236182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 424d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 4256182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 426d66c374fSTor Egge return 0; 427d66c374fSTor Egge } 428d66c374fSTor Egge 429d66c374fSTor Egge static int 430dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 431dc5df763SJoerg Wunsch { 432dc5df763SJoerg Wunsch int st3; 433dc5df763SJoerg Wunsch 4346182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 435dc5df763SJoerg Wunsch { 4366182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 437dc5df763SJoerg Wunsch } 438dc5df763SJoerg Wunsch if (st3p) 439dc5df763SJoerg Wunsch *st3p = st3; 440dc5df763SJoerg Wunsch 441dc5df763SJoerg Wunsch return 0; 442dc5df763SJoerg Wunsch } 443dc5df763SJoerg Wunsch 4446f4e0bebSPoul-Henning Kamp static int 445dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 446dc5df763SJoerg Wunsch { 4476182fdbdSPeter Wemm int cyl, st0, ret; 448dc5df763SJoerg Wunsch 4496182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 4506182fdbdSPeter Wemm if (ret) { 4516182fdbdSPeter Wemm (void)fdc_err(fdc, 452dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 453dc5df763SJoerg Wunsch return ret; 454dc5df763SJoerg Wunsch } 455dc5df763SJoerg Wunsch 456dc5df763SJoerg Wunsch if (st0p) 457dc5df763SJoerg Wunsch *st0p = st0; 458dc5df763SJoerg Wunsch 4596182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 460dc5df763SJoerg Wunsch /* 461dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 462dc5df763SJoerg Wunsch */ 463dc5df763SJoerg Wunsch return FD_NOT_VALID; 464dc5df763SJoerg Wunsch } 465dc5df763SJoerg Wunsch 4666182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 4676182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 468dc5df763SJoerg Wunsch } 469dc5df763SJoerg Wunsch 470dc5df763SJoerg Wunsch if (cylp) 471dc5df763SJoerg Wunsch *cylp = cyl; 472dc5df763SJoerg Wunsch 473dc5df763SJoerg Wunsch return 0; 474dc5df763SJoerg Wunsch } 475dc5df763SJoerg Wunsch 476dc5df763SJoerg Wunsch 4776f4e0bebSPoul-Henning Kamp static int 47864860614SJoerg Wunsch fd_read_status(fdc_p fdc) 479dc5df763SJoerg Wunsch { 480dc5df763SJoerg Wunsch int i, ret; 481b5e8ce9fSBruce Evans 4826182fdbdSPeter Wemm for (i = 0; i < 7; i++) { 483b5e8ce9fSBruce Evans /* 48464860614SJoerg Wunsch * XXX types are poorly chosen. Only bytes can be read 485a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 486b5e8ce9fSBruce Evans * fd_in() gives ints. 487b5e8ce9fSBruce Evans */ 488b5e8ce9fSBruce Evans int status; 489b5e8ce9fSBruce Evans 4906182fdbdSPeter Wemm ret = fd_in(fdc, &status); 491b5e8ce9fSBruce Evans fdc->status[i] = status; 492b5e8ce9fSBruce Evans if (ret != 0) 493dc5df763SJoerg Wunsch break; 494dc5df763SJoerg Wunsch } 495dc5df763SJoerg Wunsch 496dc5df763SJoerg Wunsch if (ret == 0) 497dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 498dc5df763SJoerg Wunsch else 499dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 500dc5df763SJoerg Wunsch 501dc5df763SJoerg Wunsch return ret; 502dc5df763SJoerg Wunsch } 503dc5df763SJoerg Wunsch 5045b81b6b3SRodney W. Grimes /****************************************************************************/ 5055b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5065b81b6b3SRodney W. Grimes /****************************************************************************/ 507dc5df763SJoerg Wunsch 5083a2f7427SDavid Greenman static int 50937286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 5105b81b6b3SRodney W. Grimes { 51137286586SPeter Wemm device_t dev; 512e219897aSJoerg Wunsch int ispnp, ispcmcia, nports; 5135b81b6b3SRodney W. Grimes 51437286586SPeter Wemm dev = fdc->fdc_dev; 5155f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 5165f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 5176182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 5186182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 5196182fdbdSPeter Wemm 520b6e5f28eSPeter Wemm /* 5215f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 5225f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 5235f830ea2SJoerg Wunsch * register at 0x3f6. 5245f830ea2SJoerg Wunsch * 525b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 5265f830ea2SJoerg Wunsch * 5275f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 5285f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 5295f830ea2SJoerg Wunsch * one with offset 7 as control register. 530b6e5f28eSPeter Wemm */ 531e219897aSJoerg Wunsch nports = ispcmcia ? 8 : (ispnp ? 1 : 6); 5326182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 5336182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 534e219897aSJoerg Wunsch nports, RF_ACTIVE); 5356182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 536e219897aSJoerg Wunsch device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 537e219897aSJoerg Wunsch nports); 53837286586SPeter Wemm return ENXIO; 5396182fdbdSPeter Wemm } 540427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 541427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 542427ccf00SDoug Rabson 5435f830ea2SJoerg Wunsch if (!ispcmcia) { 544427ccf00SDoug Rabson /* 5455f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 5465f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 5475f830ea2SJoerg Wunsch * by checking the size and adjust the port address 5485f830ea2SJoerg Wunsch * accordingly. 549b6e5f28eSPeter Wemm */ 550b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 551b6e5f28eSPeter Wemm fdc->port_off = -2; 552b6e5f28eSPeter Wemm 553b6e5f28eSPeter Wemm /* 5545f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 5555f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 5565f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 557a2639a18SPeter Wemm * 5585f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 5595f830ea2SJoerg Wunsch * leave out the control register! It seems that some 5605f830ea2SJoerg Wunsch * non-antique controller chips have a different 5615f830ea2SJoerg Wunsch * method of programming the transfer speed which 5625f830ea2SJoerg Wunsch * doesn't require the control register, but it's 5635f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 5645f830ea2SJoerg Wunsch * address for the control register. 565427ccf00SDoug Rabson */ 566b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 567b9da888fSPeter Wemm u_long ctlstart; 568b9da888fSPeter Wemm 569b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 5705f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 5715f830ea2SJoerg Wunsch fdc->port_off + 7; 572b6e5f28eSPeter Wemm 573b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 574b9da888fSPeter Wemm } 575b6e5f28eSPeter Wemm 576b6e5f28eSPeter Wemm /* 577b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 578b6e5f28eSPeter Wemm */ 579427ccf00SDoug Rabson fdc->rid_ctl = 1; 5805f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 5815f830ea2SJoerg Wunsch &fdc->rid_ctl, 582b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 583427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 5845f830ea2SJoerg Wunsch device_printf(dev, 585e219897aSJoerg Wunsch "cannot reserve control I/O port range (control port)\n"); 58637286586SPeter Wemm return ENXIO; 587427ccf00SDoug Rabson } 588427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 589427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 5905f830ea2SJoerg Wunsch } 5916182fdbdSPeter Wemm 5926182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 5936182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 5946182fdbdSPeter Wemm RF_ACTIVE); 5956182fdbdSPeter Wemm if (fdc->res_irq == 0) { 596427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 59737286586SPeter Wemm return ENXIO; 5986182fdbdSPeter Wemm } 5995f830ea2SJoerg Wunsch 6005f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 6016182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 6026182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 6036182fdbdSPeter Wemm RF_ACTIVE); 6046182fdbdSPeter Wemm if (fdc->res_drq == 0) { 605427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 60637286586SPeter Wemm return ENXIO; 6076182fdbdSPeter Wemm } 6086182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 6095f830ea2SJoerg Wunsch } 61037286586SPeter Wemm 61137286586SPeter Wemm return 0; 61237286586SPeter Wemm } 61337286586SPeter Wemm 61437286586SPeter Wemm static void 61537286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 61637286586SPeter Wemm { 61737286586SPeter Wemm device_t dev; 61837286586SPeter Wemm 61937286586SPeter Wemm dev = fdc->fdc_dev; 62037286586SPeter Wemm if (fdc->res_irq != 0) { 62137286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62237286586SPeter Wemm fdc->res_irq); 62337286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62437286586SPeter Wemm fdc->res_irq); 62537286586SPeter Wemm } 62637286586SPeter Wemm if (fdc->res_ctl != 0) { 62737286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 62837286586SPeter Wemm fdc->res_ctl); 62937286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63037286586SPeter Wemm fdc->res_ctl); 63137286586SPeter Wemm } 63237286586SPeter Wemm if (fdc->res_ioport != 0) { 63337286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 63437286586SPeter Wemm fdc->res_ioport); 63537286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 63637286586SPeter Wemm fdc->res_ioport); 63737286586SPeter Wemm } 63837286586SPeter Wemm if (fdc->res_drq != 0) { 63937286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64037286586SPeter Wemm fdc->res_drq); 64137286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64237286586SPeter Wemm fdc->res_drq); 64337286586SPeter Wemm } 64437286586SPeter Wemm } 64537286586SPeter Wemm 64637286586SPeter Wemm /****************************************************************************/ 64737286586SPeter Wemm /* autoconfiguration stuff */ 64837286586SPeter Wemm /****************************************************************************/ 64937286586SPeter Wemm 65037286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 65137286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 65237286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 65337286586SPeter Wemm {0} 65437286586SPeter Wemm }; 65537286586SPeter Wemm 65637286586SPeter Wemm static int 6573aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 65837286586SPeter Wemm { 65937286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 66037286586SPeter Wemm 66137286586SPeter Wemm switch (which) { 66237286586SPeter Wemm case FDC_IVAR_FDUNIT: 66337286586SPeter Wemm *result = ivars->fdunit; 66437286586SPeter Wemm break; 66537286586SPeter Wemm default: 66637286586SPeter Wemm return ENOENT; 66737286586SPeter Wemm } 66837286586SPeter Wemm return 0; 66937286586SPeter Wemm } 67037286586SPeter Wemm 67137286586SPeter Wemm /* 67237286586SPeter Wemm * fdc controller section. 67337286586SPeter Wemm */ 67437286586SPeter Wemm static int 67537286586SPeter Wemm fdc_probe(device_t dev) 67637286586SPeter Wemm { 67737286586SPeter Wemm int error, ic_type; 67837286586SPeter Wemm struct fdc_data *fdc; 67937286586SPeter Wemm 68037286586SPeter Wemm fdc = device_get_softc(dev); 68137286586SPeter Wemm bzero(fdc, sizeof *fdc); 68237286586SPeter Wemm fdc->fdc_dev = dev; 6835f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 68437286586SPeter Wemm 68537286586SPeter Wemm /* Check pnp ids */ 68637286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 68737286586SPeter Wemm if (error == ENXIO) 68837286586SPeter Wemm return ENXIO; 6895f830ea2SJoerg Wunsch if (error == 0) 6905f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 69137286586SPeter Wemm 69237286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 69337286586SPeter Wemm error = fdc_alloc_resources(fdc); 69437286586SPeter Wemm if (error) 69537286586SPeter Wemm goto out; 6965b81b6b3SRodney W. Grimes 69716111cedSAndrew Moore /* First - lets reset the floppy controller */ 698427ccf00SDoug Rabson fdout_wr(fdc, 0); 69916111cedSAndrew Moore DELAY(100); 700427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 70116111cedSAndrew Moore 7025b81b6b3SRodney W. Grimes /* see if it can handle a command */ 7036182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7046182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 7056182fdbdSPeter Wemm error = ENXIO; 7066182fdbdSPeter Wemm goto out; 7075b81b6b3SRodney W. Grimes } 7086182fdbdSPeter Wemm 7096182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 7106182fdbdSPeter Wemm ic_type = (u_char)ic_type; 7116182fdbdSPeter Wemm switch (ic_type) { 7126182fdbdSPeter Wemm case 0x80: 7136182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 7146182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 7156182fdbdSPeter Wemm break; 7166182fdbdSPeter Wemm case 0x81: 7176182fdbdSPeter Wemm device_set_desc(dev, "Intel 82077 or clone"); 7186182fdbdSPeter Wemm fdc->fdct = FDC_I82077; 7196182fdbdSPeter Wemm break; 7206182fdbdSPeter Wemm case 0x90: 7216182fdbdSPeter Wemm device_set_desc(dev, "NEC 72065B or clone"); 7226182fdbdSPeter Wemm fdc->fdct = FDC_NE72065; 7236182fdbdSPeter Wemm break; 7246182fdbdSPeter Wemm default: 7256182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 7266182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 7276182fdbdSPeter Wemm break; 7286182fdbdSPeter Wemm } 7296182fdbdSPeter Wemm } 7306182fdbdSPeter Wemm 7316182fdbdSPeter Wemm out: 73237286586SPeter Wemm fdc_release_resources(fdc); 7336182fdbdSPeter Wemm return (error); 7345b81b6b3SRodney W. Grimes } 7355b81b6b3SRodney W. Grimes 7365f830ea2SJoerg Wunsch #if NCARD > 0 7375f830ea2SJoerg Wunsch 7385f830ea2SJoerg Wunsch static int 7395f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 7405f830ea2SJoerg Wunsch { 7415f830ea2SJoerg Wunsch int error; 7425f830ea2SJoerg Wunsch struct fdc_data *fdc; 7435f830ea2SJoerg Wunsch 7445f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7455f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 7465f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 7475f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 7485f830ea2SJoerg Wunsch 7495f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 7505f830ea2SJoerg Wunsch 7515f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 7525f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 7535f830ea2SJoerg Wunsch if (error) 7545f830ea2SJoerg Wunsch goto out; 7555f830ea2SJoerg Wunsch 7565f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 7575f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 7585f830ea2SJoerg Wunsch DELAY(100); 7595f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 7605f830ea2SJoerg Wunsch 7615f830ea2SJoerg Wunsch /* see if it can handle a command */ 7625f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7635f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 7645f830ea2SJoerg Wunsch error = ENXIO; 7655f830ea2SJoerg Wunsch goto out; 7665f830ea2SJoerg Wunsch } 7675f830ea2SJoerg Wunsch 7685f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 7695f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 7705f830ea2SJoerg Wunsch 7715f830ea2SJoerg Wunsch out: 7725f830ea2SJoerg Wunsch fdc_release_resources(fdc); 7735f830ea2SJoerg Wunsch return (error); 7745f830ea2SJoerg Wunsch } 7755f830ea2SJoerg Wunsch 776e219897aSJoerg Wunsch #endif /* NCARD > 0 */ 777e219897aSJoerg Wunsch 7785f830ea2SJoerg Wunsch static int 779e219897aSJoerg Wunsch fdc_detach(device_t dev) 7805f830ea2SJoerg Wunsch { 7815f830ea2SJoerg Wunsch struct fdc_data *fdc; 7825f830ea2SJoerg Wunsch int error; 7835f830ea2SJoerg Wunsch 7845f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7855f830ea2SJoerg Wunsch 7865f830ea2SJoerg Wunsch /* have our children detached first */ 7875f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 7885f830ea2SJoerg Wunsch return (error); 7895f830ea2SJoerg Wunsch 790e219897aSJoerg Wunsch /* reset controller, turn motor off */ 791e219897aSJoerg Wunsch fdout_wr(fdc, 0); 792e219897aSJoerg Wunsch 793e219897aSJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) 794e219897aSJoerg Wunsch isa_dma_release(fdc->dmachan); 795e219897aSJoerg Wunsch 7965f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 7975f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 7985f830ea2SJoerg Wunsch return (0); 7995f830ea2SJoerg Wunsch } 8005f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 8015f830ea2SJoerg Wunsch 8025f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 8035f830ea2SJoerg Wunsch fdc->fdc_intr); 8045f830ea2SJoerg Wunsch fdc_release_resources(fdc); 8055f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 8065f830ea2SJoerg Wunsch return (0); 8075f830ea2SJoerg Wunsch } 8085f830ea2SJoerg Wunsch 8095b81b6b3SRodney W. Grimes /* 81037286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc. 8115b81b6b3SRodney W. Grimes */ 8126182fdbdSPeter Wemm static void 81337286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit) 8145b81b6b3SRodney W. Grimes { 81537286586SPeter Wemm int disabled; 81637286586SPeter Wemm struct fdc_ivars *ivar; 8176182fdbdSPeter Wemm device_t child; 81892200632SGarrett Wollman 8197cc0979fSDavid Malone ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 82037286586SPeter Wemm if (ivar == NULL) 8216182fdbdSPeter Wemm return; 82237286586SPeter Wemm if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 82337286586SPeter Wemm ivar->fdunit = 0; 824fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit); 82537286586SPeter Wemm if (child == NULL) 8266182fdbdSPeter Wemm return; 82737286586SPeter Wemm device_set_ivars(child, ivar); 828a97c75b7SDoug Rabson if (resource_int_value(name, unit, "disabled", &disabled) == 0 829a97c75b7SDoug Rabson && disabled != 0) 8306182fdbdSPeter Wemm device_disable(child); 8316182fdbdSPeter Wemm } 8326182fdbdSPeter Wemm 8336182fdbdSPeter Wemm static int 8346182fdbdSPeter Wemm fdc_attach(device_t dev) 8356182fdbdSPeter Wemm { 83637286586SPeter Wemm struct fdc_data *fdc; 8372398f0cdSPeter Wemm const char *name, *dname; 83860444853SJoerg Wunsch device_t *children; 83960444853SJoerg Wunsch int i, error, dunit, nchildren; 840427ccf00SDoug Rabson 84137286586SPeter Wemm fdc = device_get_softc(dev); 84237286586SPeter Wemm error = fdc_alloc_resources(fdc); 84337286586SPeter Wemm if (error) { 844f8ce7dd5SJoerg Wunsch device_printf(dev, "cannot re-acquire resources\n"); 84537286586SPeter Wemm return error; 84637286586SPeter Wemm } 84737286586SPeter Wemm error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 8483c36743eSMark Murray INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 8493c36743eSMark Murray &fdc->fdc_intr); 85037286586SPeter Wemm if (error) { 85137286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n"); 85237286586SPeter Wemm return error; 85337286586SPeter Wemm } 85437286586SPeter Wemm fdc->fdcu = device_get_unit(dev); 855fb35bd37SJoerg Wunsch fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET; 8566182fdbdSPeter Wemm 8575f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 858f8ce7dd5SJoerg Wunsch /* 859f8ce7dd5SJoerg Wunsch * Acquire the DMA channel forever, the driver will do 860f8ce7dd5SJoerg Wunsch * the rest 861f8ce7dd5SJoerg Wunsch * XXX should integrate with rman 862f8ce7dd5SJoerg Wunsch */ 863100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 864f8ce7dd5SJoerg Wunsch isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 8655f830ea2SJoerg Wunsch } 8665b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 8676182fdbdSPeter Wemm 8683a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 86964860614SJoerg Wunsch fdout_wr(fdc, fdc->fdout = 0); 8708177437dSPoul-Henning Kamp bioq_init(&fdc->head); 8715b81b6b3SRodney W. Grimes 8726182fdbdSPeter Wemm /* 87337286586SPeter Wemm * Probe and attach any children. We should probably detect 87437286586SPeter Wemm * devices from the BIOS unless overridden. 8756182fdbdSPeter Wemm */ 876ada54f9eSPeter Wemm name = device_get_nameunit(dev); 8772398f0cdSPeter Wemm i = 0; 8782398f0cdSPeter Wemm while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 8792398f0cdSPeter Wemm fdc_add_child(dev, dname, dunit); 88037286586SPeter Wemm 88160444853SJoerg Wunsch if ((error = bus_generic_attach(dev)) != 0) 88260444853SJoerg Wunsch return (error); 88360444853SJoerg Wunsch 88460444853SJoerg Wunsch /* Now remove all children not successfully probed (if any). */ 88560444853SJoerg Wunsch if ((error = device_get_children(dev, &children, &nchildren)) != 0) 88660444853SJoerg Wunsch return (error); 88760444853SJoerg Wunsch for (i = 0; i < nchildren; i++) 88860444853SJoerg Wunsch if (!device_is_alive(children[i])) 88960444853SJoerg Wunsch device_delete_child(dev, children[i]); 89060444853SJoerg Wunsch 89160444853SJoerg Wunsch return (0); 8926182fdbdSPeter Wemm } 8936182fdbdSPeter Wemm 89415317dd8SMatthew N. Dodd static int 8956182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 8966182fdbdSPeter Wemm { 89715317dd8SMatthew N. Dodd int retval = 0; 89815317dd8SMatthew N. Dodd 89915317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 90015317dd8SMatthew N. Dodd retval += printf(" on %s drive %d\n", device_get_nameunit(me), 90137286586SPeter Wemm fdc_get_fdunit(child)); 90215317dd8SMatthew N. Dodd 90315317dd8SMatthew N. Dodd return (retval); 9046182fdbdSPeter Wemm } 9056182fdbdSPeter Wemm 90616e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 90716e68fc6SPeter Wemm /* Device interface */ 90816e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 90916e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 910e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 91116e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 91216e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 91316e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 91416e68fc6SPeter Wemm 91516e68fc6SPeter Wemm /* Bus interface */ 91616e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 91737286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 91816e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 91916e68fc6SPeter Wemm 92016e68fc6SPeter Wemm { 0, 0 } 92116e68fc6SPeter Wemm }; 92216e68fc6SPeter Wemm 92316e68fc6SPeter Wemm static driver_t fdc_driver = { 92416e68fc6SPeter Wemm "fdc", 92516e68fc6SPeter Wemm fdc_methods, 92616e68fc6SPeter Wemm sizeof(struct fdc_data) 92716e68fc6SPeter Wemm }; 92816e68fc6SPeter Wemm 92916e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 93016e68fc6SPeter Wemm 9315f830ea2SJoerg Wunsch #if NCARD > 0 9325f830ea2SJoerg Wunsch 9335f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 9345f830ea2SJoerg Wunsch /* Device interface */ 9355f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 9365f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 937e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 9385f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 9395f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 9405f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 9415f830ea2SJoerg Wunsch 9425f830ea2SJoerg Wunsch /* Bus interface */ 9435f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 9445f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 9455f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 9465f830ea2SJoerg Wunsch 9475f830ea2SJoerg Wunsch { 0, 0 } 9485f830ea2SJoerg Wunsch }; 9495f830ea2SJoerg Wunsch 9505f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 9515f830ea2SJoerg Wunsch "fdc", 9525f830ea2SJoerg Wunsch fdc_pccard_methods, 9535f830ea2SJoerg Wunsch sizeof(struct fdc_data) 9545f830ea2SJoerg Wunsch }; 9555f830ea2SJoerg Wunsch 9565f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 9575f830ea2SJoerg Wunsch 9585f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 9595f830ea2SJoerg Wunsch 9603f54a085SPoul-Henning Kamp static struct { 9613f54a085SPoul-Henning Kamp char *match; 9623f54a085SPoul-Henning Kamp int minor; 9633f54a085SPoul-Henning Kamp int link; 9643f54a085SPoul-Henning Kamp } fd_suffix[] = { 96560444853SJoerg Wunsch /* 96660444853SJoerg Wunsch * Genuine clone devices must come first, and their number must 96760444853SJoerg Wunsch * match NCLONEDEVS above. 96860444853SJoerg Wunsch */ 9693f54a085SPoul-Henning Kamp { ".1720", 1, 0 }, 9703f54a085SPoul-Henning Kamp { ".1480", 2, 0 }, 9713f54a085SPoul-Henning Kamp { ".1440", 3, 0 }, 9723f54a085SPoul-Henning Kamp { ".1200", 4, 0 }, 9733f54a085SPoul-Henning Kamp { ".820", 5, 0 }, 9743f54a085SPoul-Henning Kamp { ".800", 6, 0 }, 9753f54a085SPoul-Henning Kamp { ".720", 7, 0 }, 9763f54a085SPoul-Henning Kamp { ".360", 8, 0 }, 9773f54a085SPoul-Henning Kamp { ".640", 9, 0 }, 9783f54a085SPoul-Henning Kamp { ".1232", 10, 0 }, 97960444853SJoerg Wunsch { "a", 0, 1 }, 98060444853SJoerg Wunsch { "b", 0, 1 }, 98160444853SJoerg Wunsch { "c", 0, 1 }, 98260444853SJoerg Wunsch { "d", 0, 1 }, 98360444853SJoerg Wunsch { "e", 0, 1 }, 98460444853SJoerg Wunsch { "f", 0, 1 }, 98560444853SJoerg Wunsch { "g", 0, 1 }, 98660444853SJoerg Wunsch { "h", 0, 1 }, 9873f54a085SPoul-Henning Kamp { 0, 0 } 9883f54a085SPoul-Henning Kamp }; 9893f54a085SPoul-Henning Kamp static void 99080909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev) 9913f54a085SPoul-Henning Kamp { 99260444853SJoerg Wunsch struct fd_data *fd; 9933f54a085SPoul-Henning Kamp int u, d, i; 9943f54a085SPoul-Henning Kamp char *n; 9953f54a085SPoul-Henning Kamp 99660444853SJoerg Wunsch fd = (struct fd_data *)arg; 9973f54a085SPoul-Henning Kamp if (*dev != NODEV) 9983f54a085SPoul-Henning Kamp return; 999db901281SPoul-Henning Kamp if (dev_stdclone(name, &n, "fd", &u) != 2) 10003f54a085SPoul-Henning Kamp return; 10013f54a085SPoul-Henning Kamp for (i = 0; ; i++) { 10023f54a085SPoul-Henning Kamp if (fd_suffix[i].match == NULL) 10033f54a085SPoul-Henning Kamp return; 10043f54a085SPoul-Henning Kamp if (strcmp(n, fd_suffix[i].match)) 10053f54a085SPoul-Henning Kamp continue; 10063f54a085SPoul-Henning Kamp d = fd_suffix[i].minor; 10073f54a085SPoul-Henning Kamp break; 10083f54a085SPoul-Henning Kamp } 10093f54a085SPoul-Henning Kamp if (fd_suffix[i].link == 0) { 10103f54a085SPoul-Henning Kamp *dev = make_dev(&fd_cdevsw, (u << 6) + d, 10113f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, name); 101260444853SJoerg Wunsch fd->clonedevs[i] = *dev; 10133f54a085SPoul-Henning Kamp } else { 101460444853SJoerg Wunsch *dev = make_dev_alias(fd->masterdev, name); 10153f54a085SPoul-Henning Kamp } 10163f54a085SPoul-Henning Kamp } 10173f54a085SPoul-Henning Kamp 101816e68fc6SPeter Wemm /******************************************************************/ 101916e68fc6SPeter Wemm /* 102016e68fc6SPeter Wemm * devices attached to the controller section. 102116e68fc6SPeter Wemm */ 10226182fdbdSPeter Wemm static int 10236182fdbdSPeter Wemm fd_probe(device_t dev) 10246182fdbdSPeter Wemm { 10256182fdbdSPeter Wemm int i; 10266182fdbdSPeter Wemm u_int fdt, st0, st3; 10276182fdbdSPeter Wemm struct fd_data *fd; 10286182fdbdSPeter Wemm struct fdc_data *fdc; 10296182fdbdSPeter Wemm fdsu_t fdsu; 10306182fdbdSPeter Wemm static int fd_fifo = 0; 10316182fdbdSPeter Wemm 10326182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 10336182fdbdSPeter Wemm fd = device_get_softc(dev); 10346182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 10356182fdbdSPeter Wemm 10366182fdbdSPeter Wemm bzero(fd, sizeof *fd); 10376182fdbdSPeter Wemm fd->dev = dev; 10386182fdbdSPeter Wemm fd->fdc = fdc; 10396182fdbdSPeter Wemm fd->fdsu = fdsu; 10406182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 10416182fdbdSPeter Wemm 1042a97c75b7SDoug Rabson #ifdef __i386__ 1043b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 10446182fdbdSPeter Wemm switch (fd->fdu) { 10456182fdbdSPeter Wemm case 0: 10465f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 10475f830ea2SJoerg Wunsch fdt = RTCFDT_144M; 10485f830ea2SJoerg Wunsch else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 10490722d6abSJoerg Wunsch fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 10500722d6abSJoerg Wunsch else 10510722d6abSJoerg Wunsch fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 1052b99f0a4aSAndrew Moore break; 10536182fdbdSPeter Wemm case 1: 10546182fdbdSPeter Wemm fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 1055dc5df763SJoerg Wunsch break; 1056dc5df763SJoerg Wunsch default: 10576182fdbdSPeter Wemm fdt = RTCFDT_NONE; 1058dc5df763SJoerg Wunsch break; 10596b7bd95bSJoerg Wunsch } 1060a97c75b7SDoug Rabson #else 1061a97c75b7SDoug Rabson fdt = RTCFDT_144M; /* XXX probably */ 1062a97c75b7SDoug Rabson #endif 10636182fdbdSPeter Wemm 10646182fdbdSPeter Wemm /* is there a unit? */ 10656182fdbdSPeter Wemm if (fdt == RTCFDT_NONE) 10666182fdbdSPeter Wemm return (ENXIO); 10676182fdbdSPeter Wemm 10686182fdbdSPeter Wemm /* select it */ 10696182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 1070fb35bd37SJoerg Wunsch fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 10716182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 10726182fdbdSPeter Wemm 10738de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 10746182fdbdSPeter Wemm if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 1075e34c71eaSJoerg Wunsch && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 10766182fdbdSPeter Wemm && enable_fifo(fdc) == 0) { 1077b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1078b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1079d66c374fSTor Egge } 10806182fdbdSPeter Wemm fd_fifo = 1; 10816182fdbdSPeter Wemm 10826182fdbdSPeter Wemm if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 10836182fdbdSPeter Wemm && (st3 & NE7_ST3_T0)) { 1084dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 1085dc5df763SJoerg Wunsch /* seek some steps: */ 10866182fdbdSPeter Wemm fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 1087dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 10886182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1089dc5df763SJoerg Wunsch } 1090dc5df763SJoerg Wunsch 1091dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 10926182fdbdSPeter Wemm if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1093dc5df763SJoerg Wunsch /* Seek some steps... */ 10946182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1095dc5df763SJoerg Wunsch /* ...wait a moment... */ 1096dc5df763SJoerg Wunsch DELAY(300000); 1097dc5df763SJoerg Wunsch /* make ctrlr happy: */ 10986182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1099dc5df763SJoerg Wunsch } 1100dc5df763SJoerg Wunsch } 1101dc5df763SJoerg Wunsch 11026b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 11036b7bd95bSJoerg Wunsch /* 11046b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 11056b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 11066b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 11076b7bd95bSJoerg Wunsch * more than 77 steps 11086b7bd95bSJoerg Wunsch */ 1109dc5df763SJoerg Wunsch /* go back to 0: */ 11106182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 11116b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 11126b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 11135b81b6b3SRodney W. Grimes 11146b7bd95bSJoerg Wunsch /* anything responding? */ 1115dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1116dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 11176b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 11186b7bd95bSJoerg Wunsch } 1119dc5df763SJoerg Wunsch } 11206b7bd95bSJoerg Wunsch 11216182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 11223a2f7427SDavid Greenman 11233a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 11246182fdbdSPeter Wemm return (ENXIO); 11255b81b6b3SRodney W. Grimes 1126dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1127b99f0a4aSAndrew Moore fd->fdc = fdc; 1128b99f0a4aSAndrew Moore fd->fdsu = fdsu; 11293a2f7427SDavid Greenman fd->options = 0; 113002a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 113102a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 11325b81b6b3SRodney W. Grimes 1133b99f0a4aSAndrew Moore switch (fdt) { 11347ca0641bSAndrey A. Chernov case RTCFDT_12M: 11356182fdbdSPeter Wemm device_set_desc(dev, "1200-KB 5.25\" drive"); 1136b99f0a4aSAndrew Moore fd->type = FD_1200; 11377ca0641bSAndrey A. Chernov break; 11380722d6abSJoerg Wunsch case RTCFDT_144M | RTCFDT_144M_PRETENDED: 11396182fdbdSPeter Wemm device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 11400722d6abSJoerg Wunsch fdt = RTCFDT_144M; 11416182fdbdSPeter Wemm fd->type = FD_1440; 11427ca0641bSAndrey A. Chernov case RTCFDT_144M: 11436182fdbdSPeter Wemm device_set_desc(dev, "1440-KB 3.5\" drive"); 1144b99f0a4aSAndrew Moore fd->type = FD_1440; 11457ca0641bSAndrey A. Chernov break; 1146290dd077SJoerg Wunsch case RTCFDT_288M: 114786a727d9SJoerg Wunsch case RTCFDT_288M_1: 11486182fdbdSPeter Wemm device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1149290dd077SJoerg Wunsch fd->type = FD_1440; 1150290dd077SJoerg Wunsch break; 11517ca0641bSAndrey A. Chernov case RTCFDT_360K: 11526182fdbdSPeter Wemm device_set_desc(dev, "360-KB 5.25\" drive"); 1153b99f0a4aSAndrew Moore fd->type = FD_360; 11547ca0641bSAndrey A. Chernov break; 1155ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 11566182fdbdSPeter Wemm printf("720-KB 3.5\" drive"); 1157b99f0a4aSAndrew Moore fd->type = FD_720; 1158ed2fa05eSAndrey A. Chernov break; 11597ca0641bSAndrey A. Chernov default: 11606182fdbdSPeter Wemm return (ENXIO); 11615b81b6b3SRodney W. Grimes } 11626182fdbdSPeter Wemm return (0); 11636182fdbdSPeter Wemm } 11646182fdbdSPeter Wemm 11656182fdbdSPeter Wemm static int 11666182fdbdSPeter Wemm fd_attach(device_t dev) 11676182fdbdSPeter Wemm { 11686182fdbdSPeter Wemm struct fd_data *fd; 116964860614SJoerg Wunsch static int cdevsw_add_done; 117060444853SJoerg Wunsch int i; 11716182fdbdSPeter Wemm 117202211baeSNick Hibma if (!cdevsw_add_done) { 1173475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 117464860614SJoerg Wunsch cdevsw_add_done = 1; 117502211baeSNick Hibma } 117664860614SJoerg Wunsch fd = device_get_softc(dev); 117760444853SJoerg Wunsch fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000); 11785f431174SJoerg Wunsch fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6, 11793f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 118060444853SJoerg Wunsch for (i = 0; i < NCLONEDEVS; i++) 118160444853SJoerg Wunsch fd->clonedevs[i] = NODEV; 11826182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 1183f8ce7dd5SJoerg Wunsch device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 11842a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 11852a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 11866182fdbdSPeter Wemm return (0); 11875b81b6b3SRodney W. Grimes } 11885b81b6b3SRodney W. Grimes 11895f830ea2SJoerg Wunsch static int 11905f830ea2SJoerg Wunsch fd_detach(device_t dev) 11915f830ea2SJoerg Wunsch { 11925f830ea2SJoerg Wunsch struct fd_data *fd; 119360444853SJoerg Wunsch int i; 11945f830ea2SJoerg Wunsch 11955f830ea2SJoerg Wunsch fd = device_get_softc(dev); 119660444853SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 1197e219897aSJoerg Wunsch devstat_remove_entry(&fd->device_stats); 1198e219897aSJoerg Wunsch destroy_dev(fd->masterdev); 119960444853SJoerg Wunsch for (i = 0; i < NCLONEDEVS; i++) 120060444853SJoerg Wunsch if (fd->clonedevs[i] != NODEV) 120160444853SJoerg Wunsch destroy_dev(fd->clonedevs[i]); 1202e219897aSJoerg Wunsch cdevsw_remove(&fd_cdevsw); 1203e219897aSJoerg Wunsch EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag); 12045f830ea2SJoerg Wunsch 12055f830ea2SJoerg Wunsch return (0); 12065f830ea2SJoerg Wunsch } 12075f830ea2SJoerg Wunsch 120816e68fc6SPeter Wemm static device_method_t fd_methods[] = { 120916e68fc6SPeter Wemm /* Device interface */ 121016e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 121116e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 12125f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 121316e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 121416e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 121516e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 121616e68fc6SPeter Wemm 121716e68fc6SPeter Wemm { 0, 0 } 121816e68fc6SPeter Wemm }; 121916e68fc6SPeter Wemm 122016e68fc6SPeter Wemm static driver_t fd_driver = { 122116e68fc6SPeter Wemm "fd", 122216e68fc6SPeter Wemm fd_methods, 122316e68fc6SPeter Wemm sizeof(struct fd_data) 122416e68fc6SPeter Wemm }; 122516e68fc6SPeter Wemm 1226475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 122716e68fc6SPeter Wemm 12285b81b6b3SRodney W. Grimes /****************************************************************************/ 12295b81b6b3SRodney W. Grimes /* motor control stuff */ 12305e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 12315b81b6b3SRodney W. Grimes /****************************************************************************/ 12323a2f7427SDavid Greenman static void 12336182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 12345b81b6b3SRodney W. Grimes { 1235fb35bd37SJoerg Wunsch int fdout; 12363a2f7427SDavid Greenman 1237fb35bd37SJoerg Wunsch fdout = fdc->fdout; 12383a2f7427SDavid Greenman if (turnon) { 12393a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 1240fb35bd37SJoerg Wunsch fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu; 12413a2f7427SDavid Greenman } else 12423a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 12436182fdbdSPeter Wemm fdc->fdout = fdout; 1244fb35bd37SJoerg Wunsch fdout_wr(fdc, fdout); 12453a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 12463a2f7427SDavid Greenman } 12473a2f7427SDavid Greenman 1248381fe1aaSGarrett Wollman static void 12496182fdbdSPeter Wemm fd_turnoff(void *xfd) 12505b81b6b3SRodney W. Grimes { 1251f5f7ba03SJordan K. Hubbard int s; 12526182fdbdSPeter Wemm fd_p fd = xfd; 1253dc16046fSJoerg Wunsch 12546182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 12558335c1b8SBruce Evans 12565f830ea2SJoerg Wunsch s = splbio(); 12578335c1b8SBruce Evans /* 12588335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 12595f830ea2SJoerg Wunsch * 12605f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 12615f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 12625f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 12635f830ea2SJoerg Wunsch * so the state machine gets re-entered. 12648335c1b8SBruce Evans */ 12656182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 12665f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 12675f830ea2SJoerg Wunsch splx(s); 12688335c1b8SBruce Evans return; 12698335c1b8SBruce Evans } 12708335c1b8SBruce Evans 12715b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 12726182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1273f5f7ba03SJordan K. Hubbard splx(s); 12745b81b6b3SRodney W. Grimes } 12755b81b6b3SRodney W. Grimes 12763a2f7427SDavid Greenman static void 12776182fdbdSPeter Wemm fd_motor_on(void *xfd) 12785b81b6b3SRodney W. Grimes { 1279f5f7ba03SJordan K. Hubbard int s; 12806182fdbdSPeter Wemm fd_p fd = xfd; 1281f5f7ba03SJordan K. Hubbard 1282f5f7ba03SJordan K. Hubbard s = splbio(); 12835b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 12845b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 12855b81b6b3SRodney W. Grimes { 12866182fdbdSPeter Wemm fdc_intr(fd->fdc); 12875b81b6b3SRodney W. Grimes } 1288f5f7ba03SJordan K. Hubbard splx(s); 12895b81b6b3SRodney W. Grimes } 12905b81b6b3SRodney W. Grimes 12913a2f7427SDavid Greenman static void 12926182fdbdSPeter Wemm fd_turnon(fd_p fd) 12935b81b6b3SRodney W. Grimes { 12945b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 12955b81b6b3SRodney W. Grimes { 12963a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 12976182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 12986182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 12995b81b6b3SRodney W. Grimes } 13005b81b6b3SRodney W. Grimes } 13015b81b6b3SRodney W. Grimes 1302381fe1aaSGarrett Wollman static void 1303dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 13045b81b6b3SRodney W. Grimes { 13053a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1306427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13073a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13083a2f7427SDavid Greenman DELAY(100); 13093a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1310427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 13113a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 13123a2f7427SDavid Greenman DELAY(100); 1313427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 13143a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 13153a2f7427SDavid Greenman 131664860614SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 13176182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1318dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1319dc5df763SJoerg Wunsch 0); 1320d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1321d66c374fSTor Egge (void) enable_fifo(fdc); 13225b81b6b3SRodney W. Grimes } 13235b81b6b3SRodney W. Grimes 13245b81b6b3SRodney W. Grimes /****************************************************************************/ 13255b81b6b3SRodney W. Grimes /* fdc in/out */ 13265b81b6b3SRodney W. Grimes /****************************************************************************/ 1327b5e8ce9fSBruce Evans static int 13286182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1329dc5df763SJoerg Wunsch { 1330dc5df763SJoerg Wunsch int i, j = 100000; 1331427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1332dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 1333dc5df763SJoerg Wunsch if (i == NE7_RQM) 13346182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1335dc5df763SJoerg Wunsch if (j <= 0) 13366182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1337d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1338427ccf00SDoug Rabson i = fddata_rd(fdc); 1339dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1340dc5df763SJoerg Wunsch *ptr = i; 1341dc5df763SJoerg Wunsch return 0; 1342d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1343427ccf00SDoug Rabson i = fddata_rd(fdc); 1344dc5df763SJoerg Wunsch if (ptr) 1345dc5df763SJoerg Wunsch *ptr = i; 1346dc5df763SJoerg Wunsch return 0; 1347d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1348dc5df763SJoerg Wunsch } 1349dc5df763SJoerg Wunsch 1350dc5df763SJoerg Wunsch int 13516182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 13525b81b6b3SRodney W. Grimes { 13533b3837dbSRodney W. Grimes int i; 13545b81b6b3SRodney W. Grimes 13553b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 13563b3837dbSRodney W. Grimes i = 100000; 1357427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0); 13586182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 13593b3837dbSRodney W. Grimes 13603b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 13613b3837dbSRodney W. Grimes i = 100000; 1362427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0); 136316b04b6aSJoerg Wunsch if (i <= 0) 13646182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 13653b3837dbSRodney W. Grimes 13663b3837dbSRodney W. Grimes /* Send the command and return */ 1367427ccf00SDoug Rabson fddata_wr(fdc, x); 13683a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 13695b81b6b3SRodney W. Grimes return (0); 13705b81b6b3SRodney W. Grimes } 13715b81b6b3SRodney W. Grimes 13725b81b6b3SRodney W. Grimes /****************************************************************************/ 13735b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 13745b81b6b3SRodney W. Grimes /****************************************************************************/ 1375381fe1aaSGarrett Wollman int 1376671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 13775b81b6b3SRodney W. Grimes { 13785b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 137920a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 13806182fdbdSPeter Wemm fd_p fd; 1381b99f0a4aSAndrew Moore fdc_p fdc; 13825b81b6b3SRodney W. Grimes 13835b81b6b3SRodney W. Grimes /* check bounds */ 13846182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1385b99f0a4aSAndrew Moore return (ENXIO); 13866182fdbdSPeter Wemm fdc = fd->fdc; 13876182fdbdSPeter Wemm if ((fdc == NULL) || (fd->type == NO_TYPE)) 1388b99f0a4aSAndrew Moore return (ENXIO); 1389b99f0a4aSAndrew Moore if (type > NUMDENS) 1390b99f0a4aSAndrew Moore return (ENXIO); 13917ca0641bSAndrey A. Chernov if (type == 0) 13926182fdbdSPeter Wemm type = fd->type; 13937ca0641bSAndrey A. Chernov else { 13943e425b96SJulian Elischer /* 13953e425b96SJulian Elischer * For each type of basic drive, make sure we are trying 13963e425b96SJulian Elischer * to open a type it can do, 13973e425b96SJulian Elischer */ 13986182fdbdSPeter Wemm if (type != fd->type) { 13996182fdbdSPeter Wemm switch (fd->type) { 14007ca0641bSAndrey A. Chernov case FD_360: 14017ca0641bSAndrey A. Chernov return (ENXIO); 1402ed2fa05eSAndrey A. Chernov case FD_720: 1403b39c878eSAndrey A. Chernov if ( type != FD_820 1404b39c878eSAndrey A. Chernov && type != FD_800 14056fb89845SKATO Takenori && type != FD_640 1406ed2fa05eSAndrey A. Chernov ) 1407ed2fa05eSAndrey A. Chernov return (ENXIO); 1408ed2fa05eSAndrey A. Chernov break; 14097ca0641bSAndrey A. Chernov case FD_1200: 1410b39c878eSAndrey A. Chernov switch (type) { 1411b39c878eSAndrey A. Chernov case FD_1480: 1412b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1413fa4700b4SAndrey A. Chernov break; 14147ca0641bSAndrey A. Chernov case FD_1440: 1415b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1416b39c878eSAndrey A. Chernov break; 14176fb89845SKATO Takenori case FD_1232: 14186fb89845SKATO Takenori break; 1419b39c878eSAndrey A. Chernov case FD_820: 1420b39c878eSAndrey A. Chernov type = FD_820in5_25; 1421b39c878eSAndrey A. Chernov break; 1422b39c878eSAndrey A. Chernov case FD_800: 1423b39c878eSAndrey A. Chernov type = FD_800in5_25; 1424b39c878eSAndrey A. Chernov break; 1425b39c878eSAndrey A. Chernov case FD_720: 1426b39c878eSAndrey A. Chernov type = FD_720in5_25; 1427b39c878eSAndrey A. Chernov break; 14286fb89845SKATO Takenori case FD_640: 14296fb89845SKATO Takenori type = FD_640in5_25; 14306fb89845SKATO Takenori break; 1431b39c878eSAndrey A. Chernov case FD_360: 1432b39c878eSAndrey A. Chernov type = FD_360in5_25; 1433b39c878eSAndrey A. Chernov break; 1434b39c878eSAndrey A. Chernov default: 1435b39c878eSAndrey A. Chernov return(ENXIO); 1436b39c878eSAndrey A. Chernov } 1437b39c878eSAndrey A. Chernov break; 1438b39c878eSAndrey A. Chernov case FD_1440: 1439b39c878eSAndrey A. Chernov if ( type != FD_1720 1440b39c878eSAndrey A. Chernov && type != FD_1480 1441ed2fa05eSAndrey A. Chernov && type != FD_1200 1442b39c878eSAndrey A. Chernov && type != FD_820 1443b39c878eSAndrey A. Chernov && type != FD_800 1444b39c878eSAndrey A. Chernov && type != FD_720 14456fb89845SKATO Takenori && type != FD_640 14467ca0641bSAndrey A. Chernov ) 1447dffff499SAndrey A. Chernov return(ENXIO); 1448fa4700b4SAndrey A. Chernov break; 14497ca0641bSAndrey A. Chernov } 14507ca0641bSAndrey A. Chernov } 1451fa4700b4SAndrey A. Chernov } 14526182fdbdSPeter Wemm fd->ft = fd_types + type - 1; 14536182fdbdSPeter Wemm fd->flags |= FD_OPEN; 14545f830ea2SJoerg Wunsch 14555b81b6b3SRodney W. Grimes return 0; 14565b81b6b3SRodney W. Grimes } 14575b81b6b3SRodney W. Grimes 1458381fe1aaSGarrett Wollman int 1459671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 14605b81b6b3SRodney W. Grimes { 14615b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 14626182fdbdSPeter Wemm struct fd_data *fd; 1463b99f0a4aSAndrew Moore 14646182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14656182fdbdSPeter Wemm fd->flags &= ~FD_OPEN; 14662995d110SJoerg Wunsch fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1467dc16046fSJoerg Wunsch 14685b81b6b3SRodney W. Grimes return (0); 14695b81b6b3SRodney W. Grimes } 14705b81b6b3SRodney W. Grimes 14713a2f7427SDavid Greenman /****************************************************************************/ 14723a2f7427SDavid Greenman /* fdstrategy */ 14733a2f7427SDavid Greenman /****************************************************************************/ 14743a2f7427SDavid Greenman void 14758177437dSPoul-Henning Kamp fdstrategy(struct bio *bp) 14763a2f7427SDavid Greenman { 1477fb35bd37SJoerg Wunsch long blknum, nblocks; 14783a2f7427SDavid Greenman int s; 14793a2f7427SDavid Greenman fdu_t fdu; 14803a2f7427SDavid Greenman fdc_p fdc; 14813a2f7427SDavid Greenman fd_p fd; 14823a2f7427SDavid Greenman size_t fdblk; 14833a2f7427SDavid Greenman 14848177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 14856182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14866182fdbdSPeter Wemm if (fd == 0) 14876182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 14888177437dSPoul-Henning Kamp (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 14893a2f7427SDavid Greenman fdc = fd->fdc; 149069acd21dSWarner Losh if (fd->type == NO_TYPE) { 14918177437dSPoul-Henning Kamp bp->bio_error = ENXIO; 14928177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 14933b178206SWarner Losh goto bad; 149464860614SJoerg Wunsch } 1495d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 1496f664aeeeSJoerg Wunsch if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) { 14978177437dSPoul-Henning Kamp if (bp->bio_blkno < 0) { 1498dc5df763SJoerg Wunsch printf( 14996a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 15008177437dSPoul-Henning Kamp fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 15018177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15028177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15033a2f7427SDavid Greenman goto bad; 15043a2f7427SDavid Greenman } 15058177437dSPoul-Henning Kamp if ((bp->bio_bcount % fdblk) != 0) { 15068177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15078177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15083a2f7427SDavid Greenman goto bad; 15093a2f7427SDavid Greenman } 15103a2f7427SDavid Greenman } 15113a2f7427SDavid Greenman 15123a2f7427SDavid Greenman /* 15133a2f7427SDavid Greenman * Set up block calculations. 15143a2f7427SDavid Greenman */ 15158177437dSPoul-Henning Kamp if (bp->bio_blkno > 20000000) { 1516bb6382faSJoerg Wunsch /* 1517bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1518bb6382faSJoerg Wunsch * multiplication below from overflowing. 1519bb6382faSJoerg Wunsch */ 15208177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15218177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15223a2f7427SDavid Greenman goto bad; 15233a2f7427SDavid Greenman } 1524fb35bd37SJoerg Wunsch blknum = bp->bio_blkno * DEV_BSIZE / fdblk; 1525bb6382faSJoerg Wunsch nblocks = fd->ft->size; 1526fb35bd37SJoerg Wunsch if (blknum + bp->bio_bcount / fdblk > nblocks) { 1527fb35bd37SJoerg Wunsch if (blknum >= nblocks) { 1528fb35bd37SJoerg Wunsch if (bp->bio_cmd == BIO_READ) 1529fb35bd37SJoerg Wunsch bp->bio_resid = bp->bio_bcount; 1530fb35bd37SJoerg Wunsch else { 1531fb35bd37SJoerg Wunsch bp->bio_error = ENOSPC; 15328177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 1533bb6382faSJoerg Wunsch } 1534fb35bd37SJoerg Wunsch goto bad; /* not always bad, but EOF */ 1535fb35bd37SJoerg Wunsch } 1536fb35bd37SJoerg Wunsch bp->bio_bcount = (nblocks - blknum) * fdblk; 1537bb6382faSJoerg Wunsch } 15388177437dSPoul-Henning Kamp bp->bio_pblkno = bp->bio_blkno; 15393a2f7427SDavid Greenman s = splbio(); 15408177437dSPoul-Henning Kamp bioqdisksort(&fdc->head, bp); 15416182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1542b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 15435f830ea2SJoerg Wunsch device_busy(fd->dev); 15446182fdbdSPeter Wemm fdstart(fdc); 15453a2f7427SDavid Greenman splx(s); 15463a2f7427SDavid Greenman return; 15473a2f7427SDavid Greenman 15483a2f7427SDavid Greenman bad: 15493a2f7427SDavid Greenman biodone(bp); 15503a2f7427SDavid Greenman } 15513a2f7427SDavid Greenman 15525b81b6b3SRodney W. Grimes /***************************************************************\ 15535b81b6b3SRodney W. Grimes * fdstart * 15545b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 15555b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 15565b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 15575b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 15585b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 15595b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 15605b81b6b3SRodney W. Grimes \***************************************************************/ 1561381fe1aaSGarrett Wollman static void 15626182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 15635b81b6b3SRodney W. Grimes { 15645b81b6b3SRodney W. Grimes int s; 15655b81b6b3SRodney W. Grimes 15665b81b6b3SRodney W. Grimes s = splbio(); 15676182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 15685b81b6b3SRodney W. Grimes { 15696182fdbdSPeter Wemm fdc_intr(fdc); 15705b81b6b3SRodney W. Grimes } 15715b81b6b3SRodney W. Grimes splx(s); 15725b81b6b3SRodney W. Grimes } 15735b81b6b3SRodney W. Grimes 1574381fe1aaSGarrett Wollman static void 15756182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 15765b81b6b3SRodney W. Grimes { 15775c1a1eaeSBruce Evans fdc_p fdc; 1578f5f7ba03SJordan K. Hubbard int s; 15795b81b6b3SRodney W. Grimes 15806182fdbdSPeter Wemm fdc = xfdc; 15815c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 15825b81b6b3SRodney W. Grimes 15833a2f7427SDavid Greenman /* 15843a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 15853a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 15863a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 15873a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 15883a2f7427SDavid Greenman * Many thanks, Big Blue! 15895c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 15905c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 15915c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 15925c1a1eaeSBruce Evans * will reset the FDC and retry once. 15933a2f7427SDavid Greenman */ 15943a2f7427SDavid Greenman s = splbio(); 15955c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 15965c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 15975c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 15986182fdbdSPeter Wemm fdc_intr(fdc); 1599f5f7ba03SJordan K. Hubbard splx(s); 16005b81b6b3SRodney W. Grimes } 16015b81b6b3SRodney W. Grimes 16025b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1603381fe1aaSGarrett Wollman static void 16046182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 16055b81b6b3SRodney W. Grimes { 16065b81b6b3SRodney W. Grimes int s; 16073a2f7427SDavid Greenman 16085b81b6b3SRodney W. Grimes s = splbio(); 16096182fdbdSPeter Wemm fdc_intr(xfdc); 16105b81b6b3SRodney W. Grimes splx(s); 16115b81b6b3SRodney W. Grimes } 16125b81b6b3SRodney W. Grimes 16135b81b6b3SRodney W. Grimes /***********************************************************************\ 161464860614SJoerg Wunsch * fdc_intr * 16155b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 16165b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 16175b81b6b3SRodney W. Grimes \***********************************************************************/ 1618fe310de8SBruce Evans static void 16196182fdbdSPeter Wemm fdc_intr(void *xfdc) 16205b81b6b3SRodney W. Grimes { 16216182fdbdSPeter Wemm fdc_p fdc = xfdc; 16226182fdbdSPeter Wemm while(fdstate(fdc)) 1623381fe1aaSGarrett Wollman ; 16245b81b6b3SRodney W. Grimes } 16255b81b6b3SRodney W. Grimes 162669acd21dSWarner Losh /* 162769acd21dSWarner Losh * magic pseudo-DMA initialization for YE FDC. Sets count and 162869acd21dSWarner Losh * direction 162969acd21dSWarner Losh */ 16303b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 16313b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 16323b178206SWarner Losh ((cnt)-1) & 0xff); \ 16333b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 16343b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 163569acd21dSWarner Losh 163669acd21dSWarner Losh /* 163769acd21dSWarner Losh * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 163869acd21dSWarner Losh */ 16394c34deeeSJoerg Wunsch static int 16404c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 164169acd21dSWarner Losh { 164269acd21dSWarner Losh u_char *cptr = (u_char *)addr; 164369acd21dSWarner Losh 164421144e3bSPoul-Henning Kamp if (flags == BIO_READ) { 164569acd21dSWarner Losh if (fdc->state != PIOREAD) { 164669acd21dSWarner Losh fdc->state = PIOREAD; 164769acd21dSWarner Losh return(0); 164864860614SJoerg Wunsch } 16493b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 16503b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16513b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 165269acd21dSWarner Losh } else { 16533b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16543b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 16553b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 165664860614SJoerg Wunsch } 165769acd21dSWarner Losh return(1); 165869acd21dSWarner Losh } 165969acd21dSWarner Losh 16605b81b6b3SRodney W. Grimes /***********************************************************************\ 16615b81b6b3SRodney W. Grimes * The controller state machine. * 16625b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 16635b81b6b3SRodney W. Grimes \***********************************************************************/ 16643a2f7427SDavid Greenman static int 16656182fdbdSPeter Wemm fdstate(fdc_p fdc) 16665b81b6b3SRodney W. Grimes { 166764860614SJoerg Wunsch struct fdc_readid *idp; 1668fb35bd37SJoerg Wunsch int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac; 1669fb35bd37SJoerg Wunsch int st0, cyl, st3, idf; 1670fb35bd37SJoerg Wunsch unsigned long blknum; 16715b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 16725b81b6b3SRodney W. Grimes fd_p fd; 16738177437dSPoul-Henning Kamp register struct bio *bp; 1674b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 16753a2f7427SDavid Greenman size_t fdblk; 16765b81b6b3SRodney W. Grimes 1677e93e63cbSBruce Evans bp = fdc->bp; 1678e93e63cbSBruce Evans if (bp == NULL) { 16798177437dSPoul-Henning Kamp bp = bioq_first(&fdc->head); 1680e93e63cbSBruce Evans if (bp != NULL) { 16818177437dSPoul-Henning Kamp bioq_remove(&fdc->head, bp); 1682e93e63cbSBruce Evans fdc->bp = bp; 1683e93e63cbSBruce Evans } 1684e93e63cbSBruce Evans } 1685e93e63cbSBruce Evans if (bp == NULL) { 16865b81b6b3SRodney W. Grimes /***********************************************\ 16875b81b6b3SRodney W. Grimes * nothing left for this controller to do * 16885b81b6b3SRodney W. Grimes * Force into the IDLE state, * 16895b81b6b3SRodney W. Grimes \***********************************************/ 16905b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 16916182fdbdSPeter Wemm if (fdc->fd) { 1692b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1693b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 16945b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 16955b81b6b3SRodney W. Grimes fdc->fdu = -1; 16965b81b6b3SRodney W. Grimes } 16976182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 16985b81b6b3SRodney W. Grimes return (0); 16995b81b6b3SRodney W. Grimes } 17008177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 17016182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 17023a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1703b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 1704b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 17058177437dSPoul-Henning Kamp read = bp->bio_cmd == BIO_READ; 170656a23089SPoul-Henning Kamp if (read) 170756a23089SPoul-Henning Kamp idf = ISADMA_READ; 170856a23089SPoul-Henning Kamp else 170956a23089SPoul-Henning Kamp idf = ISADMA_WRITE; 171064860614SJoerg Wunsch format = bp->bio_cmd == BIO_FORMAT; 1711f664aeeeSJoerg Wunsch rdsectid = bp->bio_cmd == BIO_RDSECTID; 1712fb35bd37SJoerg Wunsch if (format) 17138177437dSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data; 17145b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 17155b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 17165b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 17176182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 17186182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 17195b81b6b3SRodney W. Grimes switch (fdc->state) 17205b81b6b3SRodney W. Grimes { 17215b81b6b3SRodney W. Grimes case DEVIDLE: 17225b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 17235b81b6b3SRodney W. Grimes fdc->retry = 0; 17245b81b6b3SRodney W. Grimes fd->skip = 0; 17255b81b6b3SRodney W. Grimes fdc->fd = fd; 17265b81b6b3SRodney W. Grimes fdc->fdu = fdu; 17275f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 17283a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 17295b81b6b3SRodney W. Grimes /*******************************************************\ 17305b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 1731dc733423SDag-Erling Smørgrav * it will start up in its own good time * 17325b81b6b3SRodney W. Grimes \*******************************************************/ 17336182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 17345b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17355b81b6b3SRodney W. Grimes return (0); /* come back later */ 17365b81b6b3SRodney W. Grimes } 17375b81b6b3SRodney W. Grimes /*******************************************************\ 17385b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 17395b81b6b3SRodney W. Grimes \*******************************************************/ 17405b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 17415b81b6b3SRodney W. Grimes { 17425b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17436182fdbdSPeter Wemm fd_turnon(fd); 17445b81b6b3SRodney W. Grimes return (0); 17455b81b6b3SRodney W. Grimes } 17465b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 17475b81b6b3SRodney W. Grimes { 17486182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 17495b81b6b3SRodney W. Grimes } 17505c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 17515c1a1eaeSBruce Evans fdc->state = RESETCTLR; 17525c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 17535c1a1eaeSBruce Evans } else 17545e235068SJordan K. Hubbard fdc->state = DOSEEK; 1755fb35bd37SJoerg Wunsch return (1); /* come back immediately */ 1756fb35bd37SJoerg Wunsch 17575b81b6b3SRodney W. Grimes case DOSEEK: 1758fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 1759fb35bd37SJoerg Wunsch cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1760fb35bd37SJoerg Wunsch if (cylinder == fd->track) 17615b81b6b3SRodney W. Grimes { 17625b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 1763fb35bd37SJoerg Wunsch return (1); /* come back immediately */ 17645b81b6b3SRodney W. Grimes } 17656182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1766fb35bd37SJoerg Wunsch fd->fdsu, cylinder * fd->ft->steptrac, 1767dc5df763SJoerg Wunsch 0)) 1768dc8603e3SJoerg Wunsch { 1769dc8603e3SJoerg Wunsch /* 1770dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1771dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1772dc8603e3SJoerg Wunsch */ 1773dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 17746182fdbdSPeter Wemm return(retrier(fdc)); 1775dc8603e3SJoerg Wunsch } 1776dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 17775b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 17785b81b6b3SRodney W. Grimes return(0); /* will return later */ 1779fb35bd37SJoerg Wunsch 17805b81b6b3SRodney W. Grimes case SEEKWAIT: 17815b81b6b3SRodney W. Grimes /* allow heads to settle */ 17826182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 17835b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17845b81b6b3SRodney W. Grimes return(0); /* will return later */ 1785fb35bd37SJoerg Wunsch 17865b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 1787fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 1788fb35bd37SJoerg Wunsch cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1789fb35bd37SJoerg Wunsch 17905b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 17916182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 1792fb35bd37SJoerg Wunsch int descyl = cylinder * fd->ft->steptrac; 17933a2f7427SDavid Greenman do { 17943a2f7427SDavid Greenman /* 1795dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1796dc5df763SJoerg Wunsch * which cannot really happen since the 1797dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1798dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1799dc5df763SJoerg Wunsch * line, so do one of the following: 1800dc5df763SJoerg Wunsch * 1801dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1802dc5df763SJoerg Wunsch * known to not go stuck after responding 1803dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1804dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1805dc5df763SJoerg Wunsch * real interrupt condition. 1806dc5df763SJoerg Wunsch * 1807dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1808dc5df763SJoerg Wunsch * up immediately. The controller will 1809dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1810dc5df763SJoerg Wunsch * conditions right after reset (for the 1811dc5df763SJoerg Wunsch * corresponding four drives), so this is 1812dc5df763SJoerg Wunsch * our only chance to get notice that it 1813dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 18143a2f7427SDavid Greenman */ 1815dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1816dc5df763SJoerg Wunsch == FD_NOT_VALID) 1817dc5df763SJoerg Wunsch return 0; 1818dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1819dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1820dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 18213a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1822dc5df763SJoerg Wunsch 18236182fdbdSPeter Wemm if (0 == descyl) { 1824dc5df763SJoerg Wunsch int failed = 0; 18253a2f7427SDavid Greenman /* 18263a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 18273a2f7427SDavid Greenman * really there 18283a2f7427SDavid Greenman */ 1829dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1830dc5df763SJoerg Wunsch failed = 1; 18313a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 18323a2f7427SDavid Greenman printf( 18333a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 18343a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1835dc5df763SJoerg Wunsch failed = 1; 1836dc5df763SJoerg Wunsch } 1837dc5df763SJoerg Wunsch 18386182fdbdSPeter Wemm if (failed) { 18393a2f7427SDavid Greenman if(fdc->retry < 3) 18403a2f7427SDavid Greenman fdc->retry = 3; 18416182fdbdSPeter Wemm return (retrier(fdc)); 18423a2f7427SDavid Greenman } 18433a2f7427SDavid Greenman } 1844dc5df763SJoerg Wunsch 18456182fdbdSPeter Wemm if (cyl != descyl) { 18463a2f7427SDavid Greenman printf( 18473a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 18482d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 1849e5d7d243SBruce Evans if (fdc->retry < 3) 1850e5d7d243SBruce Evans fdc->retry = 3; 18516182fdbdSPeter Wemm return (retrier(fdc)); 18525b81b6b3SRodney W. Grimes } 18535b81b6b3SRodney W. Grimes } 18545b81b6b3SRodney W. Grimes 1855fb35bd37SJoerg Wunsch fd->track = cylinder; 1856fb35bd37SJoerg Wunsch if (format) 1857fb35bd37SJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1858fb35bd37SJoerg Wunsch - (char *)finfo; 1859250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 18608177437dSPoul-Henning Kamp isa_dmastart(idf, bp->bio_data+fd->skip, 18618177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 1862fb35bd37SJoerg Wunsch blknum = bp->bio_pblkno + fd->skip / fdblk; 18635b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 18645b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 18655b81b6b3SRodney W. Grimes head = sec / sectrac; 18665b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 18673a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 18683a2f7427SDavid Greenman 1869250300ebSJoerg Wunsch if(format || !(read || rdsectid)) 18703a2f7427SDavid Greenman { 18713a2f7427SDavid Greenman /* make sure the drive is writable */ 1872dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1873dc8603e3SJoerg Wunsch { 1874dc8603e3SJoerg Wunsch /* stuck controller? */ 18755f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 187656a23089SPoul-Henning Kamp isa_dmadone(idf, 18778177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 18788177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 18795c1a1eaeSBruce Evans fdc->dmachan); 1880dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 18816182fdbdSPeter Wemm return (retrier(fdc)); 1882dc8603e3SJoerg Wunsch } 18833a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 18843a2f7427SDavid Greenman { 18853a2f7427SDavid Greenman /* 18863a2f7427SDavid Greenman * XXX YES! this is ugly. 18873a2f7427SDavid Greenman * in order to force the current operation 18883a2f7427SDavid Greenman * to fail, we will have to fake an FDC 18893a2f7427SDavid Greenman * error - all error handling is done 18903a2f7427SDavid Greenman * by the retrier() 18913a2f7427SDavid Greenman */ 18923a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 18933a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 18943a2f7427SDavid Greenman fdc->status[2] = 0; 18953a2f7427SDavid Greenman fdc->status[3] = fd->track; 18963a2f7427SDavid Greenman fdc->status[4] = head; 18973a2f7427SDavid Greenman fdc->status[5] = sec; 18983a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 18993a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 19003a2f7427SDavid Greenman return (1); 19013a2f7427SDavid Greenman } 19023a2f7427SDavid Greenman } 19035b81b6b3SRodney W. Grimes 19046182fdbdSPeter Wemm if (format) { 19055f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 19065f830ea2SJoerg Wunsch /* 19075f830ea2SJoerg Wunsch * This seems to be necessary for 19085f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 19095f830ea2SJoerg Wunsch * it, we end up filling the sector ID 19105f830ea2SJoerg Wunsch * fields of the newly formatted track 19115f830ea2SJoerg Wunsch * entirely with garbage, causing 19125f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 19135f830ea2SJoerg Wunsch * the place when trying to read them 19145f830ea2SJoerg Wunsch * back. 19155f830ea2SJoerg Wunsch * 19165f830ea2SJoerg Wunsch * Umpf. 19175f830ea2SJoerg Wunsch */ 19188177437dSPoul-Henning Kamp SET_BCDR(fdc, 1, bp->bio_bcount, 0); 19195f830ea2SJoerg Wunsch 19208177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19218177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 19228177437dSPoul-Henning Kamp bp->bio_bcount); 19235f830ea2SJoerg Wunsch 19245f830ea2SJoerg Wunsch } 1925b39c878eSAndrey A. Chernov /* formatting */ 19266182fdbdSPeter Wemm if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1927dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1928dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1929dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 19306182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 1931dc8603e3SJoerg Wunsch /* controller fell over */ 19325f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 193356a23089SPoul-Henning Kamp isa_dmadone(idf, 19348177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19358177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19365c1a1eaeSBruce Evans fdc->dmachan); 1937dc8603e3SJoerg Wunsch fdc->retry = 6; 19386182fdbdSPeter Wemm return (retrier(fdc)); 1939dc8603e3SJoerg Wunsch } 1940250300ebSJoerg Wunsch } else if (rdsectid) { 1941250300ebSJoerg Wunsch if (fd_cmd(fdc, 2, NE7CMD_READID, head << 2 | fdu, 0)) { 1942250300ebSJoerg Wunsch /* controller jamming */ 1943250300ebSJoerg Wunsch fdc->retry = 6; 1944250300ebSJoerg Wunsch return (retrier(fdc)); 1945250300ebSJoerg Wunsch } 19466182fdbdSPeter Wemm } else { 1947250300ebSJoerg Wunsch /* read or write operation */ 19483b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 194969acd21dSWarner Losh /* 195069acd21dSWarner Losh * this seems to be necessary even when 195169acd21dSWarner Losh * reading data 195269acd21dSWarner Losh */ 19533b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 195469acd21dSWarner Losh 195569acd21dSWarner Losh /* 195669acd21dSWarner Losh * perform the write pseudo-DMA before 195769acd21dSWarner Losh * the WRITE command is sent 195869acd21dSWarner Losh */ 195969acd21dSWarner Losh if (!read) 19608177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19618177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 196269acd21dSWarner Losh fdblk); 196369acd21dSWarner Losh } 19646182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 1965dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1966dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1967dc5df763SJoerg Wunsch fd->track, /* track */ 1968dc5df763SJoerg Wunsch head, 1969dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1970dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1971dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1972dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1973dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 19746182fdbdSPeter Wemm 0)) { 1975dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 19765f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 197756a23089SPoul-Henning Kamp isa_dmadone(idf, 19788177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19798177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19805c1a1eaeSBruce Evans fdc->dmachan); 1981dc8603e3SJoerg Wunsch fdc->retry = 6; 19826182fdbdSPeter Wemm return (retrier(fdc)); 19835b81b6b3SRodney W. Grimes } 1984b39c878eSAndrey A. Chernov } 1985250300ebSJoerg Wunsch if (!rdsectid && (fdc->flags & FDC_NODMA)) 198669acd21dSWarner Losh /* 198769acd21dSWarner Losh * if this is a read, then simply await interrupt 198869acd21dSWarner Losh * before performing PIO 198969acd21dSWarner Losh */ 19908177437dSPoul-Henning Kamp if (read && !fdcpio(fdc,bp->bio_cmd, 19918177437dSPoul-Henning Kamp bp->bio_data+fd->skip,fdblk)) { 19923b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 199369acd21dSWarner Losh return(0); /* will return later */ 199464860614SJoerg Wunsch } 199569acd21dSWarner Losh 199669acd21dSWarner Losh /* 199769acd21dSWarner Losh * write (or format) operation will fall through and 199869acd21dSWarner Losh * await completion interrupt 199969acd21dSWarner Losh */ 20005b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 20016182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 20025b81b6b3SRodney W. Grimes return (0); /* will return later */ 2003fb35bd37SJoerg Wunsch 200469acd21dSWarner Losh case PIOREAD: 200569acd21dSWarner Losh /* 200669acd21dSWarner Losh * actually perform the PIO read. The IOCOMPLETE case 200769acd21dSWarner Losh * removes the timeout for us. 200869acd21dSWarner Losh */ 20098177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 201069acd21dSWarner Losh fdc->state = IOCOMPLETE; 201169acd21dSWarner Losh /* FALLTHROUGH */ 20125b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 20136182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 2014dc5df763SJoerg Wunsch 201564860614SJoerg Wunsch if (fd_read_status(fdc)) { 2016250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 20178177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20188177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 20195c1a1eaeSBruce Evans fdc->dmachan); 2020dc5df763SJoerg Wunsch if (fdc->retry < 6) 2021dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 20226182fdbdSPeter Wemm return (retrier(fdc)); 20235b81b6b3SRodney W. Grimes } 2024dc5df763SJoerg Wunsch 20253a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 2026dc5df763SJoerg Wunsch 20273a2f7427SDavid Greenman /* FALLTHROUGH */ 20283a2f7427SDavid Greenman case IOTIMEDOUT: 2029250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 20308177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20318177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 20326182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 20333a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20343a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 2035b39c878eSAndrey A. Chernov /* 20363a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 20373a2f7427SDavid Greenman * and didn't release it in time for the 20383a2f7427SDavid Greenman * next FDC transfer. 20393a2f7427SDavid Greenman * Just restart it, don't increment retry 20403a2f7427SDavid Greenman * count. (vak) 2041b39c878eSAndrey A. Chernov */ 2042b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2043b39c878eSAndrey A. Chernov return (1); 2044b39c878eSAndrey A. Chernov } 20453a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 20463a2f7427SDavid Greenman && fdc->retry < 6) 20473a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 20483a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20493a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 20503a2f7427SDavid Greenman && fdc->retry < 3) 20513a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 20526182fdbdSPeter Wemm return (retrier(fdc)); 20535b81b6b3SRodney W. Grimes } 20545b81b6b3SRodney W. Grimes /* All OK */ 2055250300ebSJoerg Wunsch if (rdsectid) { 2056250300ebSJoerg Wunsch /* copy out ID field contents */ 205764860614SJoerg Wunsch idp = (struct fdc_readid *)bp->bio_data; 2058250300ebSJoerg Wunsch idp->cyl = fdc->status[3]; 2059250300ebSJoerg Wunsch idp->head = fdc->status[4]; 2060250300ebSJoerg Wunsch idp->sec = fdc->status[5]; 2061250300ebSJoerg Wunsch idp->secshift = fdc->status[6]; 2062250300ebSJoerg Wunsch } 20633a2f7427SDavid Greenman fd->skip += fdblk; 2064fb35bd37SJoerg Wunsch if (!rdsectid && !format && fd->skip < bp->bio_bcount) { 20655b81b6b3SRodney W. Grimes /* set up next transfer */ 20665b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20676182fdbdSPeter Wemm } else { 20685b81b6b3SRodney W. Grimes /* ALL DONE */ 20695b81b6b3SRodney W. Grimes fd->skip = 0; 2070fb35bd37SJoerg Wunsch bp->bio_resid = 0; 2071e93e63cbSBruce Evans fdc->bp = NULL; 20725f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2073a468031cSPoul-Henning Kamp biofinish(bp, &fd->device_stats, 0); 20745b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20755b81b6b3SRodney W. Grimes fdc->fdu = -1; 20765b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 20775b81b6b3SRodney W. Grimes } 20785b81b6b3SRodney W. Grimes return (1); 2079fb35bd37SJoerg Wunsch 20805b81b6b3SRodney W. Grimes case RESETCTLR: 20813a2f7427SDavid Greenman fdc_reset(fdc); 20825b81b6b3SRodney W. Grimes fdc->retry++; 20835c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 20845c1a1eaeSBruce Evans return (0); 2085fb35bd37SJoerg Wunsch 20865c1a1eaeSBruce Evans case RESETCOMPLETE: 20875c1a1eaeSBruce Evans /* 20885c1a1eaeSBruce Evans * Discard all the results from the reset so that they 20895c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 20905c1a1eaeSBruce Evans */ 20910e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 20920e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 20935c1a1eaeSBruce Evans fdc->state = STARTRECAL; 2094fb35bd37SJoerg Wunsch /* FALLTHROUGH */ 20955c1a1eaeSBruce Evans case STARTRECAL: 20966182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2097dc8603e3SJoerg Wunsch /* arrgl */ 2098dc8603e3SJoerg Wunsch fdc->retry = 6; 20996182fdbdSPeter Wemm return (retrier(fdc)); 2100dc8603e3SJoerg Wunsch } 21015b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 21025b81b6b3SRodney W. Grimes return (0); /* will return later */ 2103fb35bd37SJoerg Wunsch 21045b81b6b3SRodney W. Grimes case RECALWAIT: 21055b81b6b3SRodney W. Grimes /* allow heads to settle */ 21066182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 21075b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 21085b81b6b3SRodney W. Grimes return (0); /* will return later */ 2109fb35bd37SJoerg Wunsch 21105b81b6b3SRodney W. Grimes case RECALCOMPLETE: 21113a2f7427SDavid Greenman do { 21123a2f7427SDavid Greenman /* 2113dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 21143a2f7427SDavid Greenman */ 2115dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2116dc5df763SJoerg Wunsch return 0; 2117dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2118dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2119dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 21203a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 21213a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 21225b81b6b3SRodney W. Grimes { 2123dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2124dc8603e3SJoerg Wunsch /* 2125dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 2126dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2127dc8603e3SJoerg Wunsch * since people used to complain much about 2128dc8603e3SJoerg Wunsch * the failure message, try not logging 2129dc8603e3SJoerg Wunsch * this one if it seems to be the first 2130dc8603e3SJoerg Wunsch * time in a line 2131dc8603e3SJoerg Wunsch */ 2132dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2133dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 21343a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 21356182fdbdSPeter Wemm return (retrier(fdc)); 21365b81b6b3SRodney W. Grimes } 21375b81b6b3SRodney W. Grimes fd->track = 0; 21385b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 21395b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 21405b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 2141fb35bd37SJoerg Wunsch 21425b81b6b3SRodney W. Grimes case MOTORWAIT: 21435b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 21445b81b6b3SRodney W. Grimes { 21455b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 21465b81b6b3SRodney W. Grimes } 21475c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 21485c1a1eaeSBruce Evans fdc->state = RESETCTLR; 21495c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 2150fb35bd37SJoerg Wunsch } else 2151fb35bd37SJoerg Wunsch fdc->state = DOSEEK; 21525b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 2153fb35bd37SJoerg Wunsch 21545b81b6b3SRodney W. Grimes default: 2155b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 215664860614SJoerg Wunsch if (fd_read_status(fdc) == 0) 2157a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 21585b81b6b3SRodney W. Grimes fdc->status[0], 21595b81b6b3SRodney W. Grimes fdc->status[1], 21605b81b6b3SRodney W. Grimes fdc->status[2], 21615b81b6b3SRodney W. Grimes fdc->status[3], 21625b81b6b3SRodney W. Grimes fdc->status[4], 21635b81b6b3SRodney W. Grimes fdc->status[5], 21645b81b6b3SRodney W. Grimes fdc->status[6] ); 21653a2f7427SDavid Greenman else 2166dac0f2dbSJoerg Wunsch printf("No status available "); 2167dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2168dac0f2dbSJoerg Wunsch { 2169dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 21705b81b6b3SRodney W. Grimes return (0); 21715b81b6b3SRodney W. Grimes } 2172dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2173dac0f2dbSJoerg Wunsch return (0); 2174dac0f2dbSJoerg Wunsch } 2175fb35bd37SJoerg Wunsch /* keep the compiler happy -- noone should ever get here */ 2176fb35bd37SJoerg Wunsch return (999999); 21775b81b6b3SRodney W. Grimes } 21785b81b6b3SRodney W. Grimes 2179aaf08d94SGarrett Wollman static int 21806182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 21815b81b6b3SRodney W. Grimes { 21828177437dSPoul-Henning Kamp struct bio *bp; 21836182fdbdSPeter Wemm struct fd_data *fd; 21846182fdbdSPeter Wemm int fdu; 21855b81b6b3SRodney W. Grimes 2186e93e63cbSBruce Evans bp = fdc->bp; 21875b81b6b3SRodney W. Grimes 21886182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 21898177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 21906182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 21916182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 21923a2f7427SDavid Greenman goto fail; 21936182fdbdSPeter Wemm 21946182fdbdSPeter Wemm switch (fdc->retry) { 21955b81b6b3SRodney W. Grimes case 0: case 1: case 2: 21965b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 21975b81b6b3SRodney W. Grimes break; 21985b81b6b3SRodney W. Grimes case 3: case 4: case 5: 21995b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 22005b81b6b3SRodney W. Grimes break; 22015b81b6b3SRodney W. Grimes case 6: 22025b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 22035b81b6b3SRodney W. Grimes break; 22045b81b6b3SRodney W. Grimes case 7: 22055b81b6b3SRodney W. Grimes break; 22065b81b6b3SRodney W. Grimes default: 22073a2f7427SDavid Greenman fail: 2208fb35bd37SJoerg Wunsch if ((fd->options & FDOPT_NOERRLOG) == 0) { 2209fb35bd37SJoerg Wunsch diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE, 22103a2f7427SDavid Greenman (struct disklabel *)NULL); 2211fb35bd37SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) { 2212dc5df763SJoerg Wunsch printf( 2213a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2214dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2215dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2216dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2217dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2218dc5df763SJoerg Wunsch fdc->status[5]); 2219dc5df763SJoerg Wunsch } 2220dc5df763SJoerg Wunsch else 2221dc5df763SJoerg Wunsch printf(" (No status)\n"); 22225b81b6b3SRodney W. Grimes } 22232995d110SJoerg Wunsch if ((fd->options & FDOPT_NOERROR) == 0) { 22248177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22258177437dSPoul-Henning Kamp bp->bio_error = EIO; 2226fb35bd37SJoerg Wunsch bp->bio_resid = bp->bio_bcount - fdc->fd->skip; 2227fb35bd37SJoerg Wunsch } else 2228fb35bd37SJoerg Wunsch bp->bio_resid = 0; 2229e93e63cbSBruce Evans fdc->bp = NULL; 22305b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 22315f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2232a468031cSPoul-Henning Kamp biofinish(bp, &fdc->fd->device_stats, 0); 223392ed385aSRodney W. Grimes fdc->state = FINDWORK; 22345c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 22355b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 22365b81b6b3SRodney W. Grimes fdc->fdu = -1; 223792ed385aSRodney W. Grimes return (1); 22385b81b6b3SRodney W. Grimes } 22395b81b6b3SRodney W. Grimes fdc->retry++; 22405b81b6b3SRodney W. Grimes return (1); 22415b81b6b3SRodney W. Grimes } 22425b81b6b3SRodney W. Grimes 22431fdb6e6cSPoul-Henning Kamp static void 22441fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp) 22451fdb6e6cSPoul-Henning Kamp { 22461fdb6e6cSPoul-Henning Kamp wakeup(bp); 22471fdb6e6cSPoul-Henning Kamp } 22481fdb6e6cSPoul-Henning Kamp 2249b39c878eSAndrey A. Chernov static int 2250f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data) 2251b39c878eSAndrey A. Chernov { 2252b39c878eSAndrey A. Chernov fdu_t fdu; 2253b39c878eSAndrey A. Chernov fd_p fd; 22541fdb6e6cSPoul-Henning Kamp struct bio *bp; 2255f664aeeeSJoerg Wunsch struct fd_formb *finfo; 2256f664aeeeSJoerg Wunsch struct fdc_readid *idfield; 22573a2f7427SDavid Greenman size_t fdblk; 2258b39c878eSAndrey A. Chernov 2259b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 22606182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22613a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2262f664aeeeSJoerg Wunsch finfo = (struct fd_formb *)data; 2263f664aeeeSJoerg Wunsch idfield = (struct fdc_readid *)data; 2264b39c878eSAndrey A. Chernov 2265fb35bd37SJoerg Wunsch bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO); 2266b39c878eSAndrey A. Chernov 2267b39c878eSAndrey A. Chernov /* 2268f664aeeeSJoerg Wunsch * Set up a bio request for fdstrategy(). bio_blkno is faked 2269f664aeeeSJoerg Wunsch * so that fdstrategy() will seek to the the requested 2270f664aeeeSJoerg Wunsch * cylinder, and use the desired head. Since we are not 2271f664aeeeSJoerg Wunsch * interested in bioqdisksort() munging with our faked bio 2272f664aeeeSJoerg Wunsch * request, we mark it as being an ordered request. 2273b39c878eSAndrey A. Chernov */ 2274f664aeeeSJoerg Wunsch bp->bio_cmd = cmd; 2275f664aeeeSJoerg Wunsch if (cmd == BIO_FORMAT) { 2276f664aeeeSJoerg Wunsch bp->bio_blkno = 2277f664aeeeSJoerg Wunsch (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2278f664aeeeSJoerg Wunsch finfo->head * fd->ft->sectrac) * 2279f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2280f664aeeeSJoerg Wunsch bp->bio_bcount = sizeof(struct fd_idfield_data) * 2281f664aeeeSJoerg Wunsch finfo->fd_formb_nsecs; 2282f664aeeeSJoerg Wunsch } else if (cmd == BIO_RDSECTID) { 2283f664aeeeSJoerg Wunsch bp->bio_blkno = 2284f664aeeeSJoerg Wunsch (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2285f664aeeeSJoerg Wunsch idfield->head * fd->ft->sectrac) * 2286f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2287250300ebSJoerg Wunsch bp->bio_bcount = sizeof(struct fdc_readid); 2288f664aeeeSJoerg Wunsch } else 2289f664aeeeSJoerg Wunsch panic("wrong cmd in fdmisccmd()"); 2290f664aeeeSJoerg Wunsch bp->bio_data = data; 2291250300ebSJoerg Wunsch bp->bio_dev = dev; 2292250300ebSJoerg Wunsch bp->bio_done = fdbiodone; 2293f664aeeeSJoerg Wunsch bp->bio_flags = BIO_ORDERED; 2294250300ebSJoerg Wunsch 2295f5f7ba03SJordan K. Hubbard /* 2296fb35bd37SJoerg Wunsch * Now run the command. The wait loop is a version of bufwait() 2297fb35bd37SJoerg Wunsch * adapted for struct bio instead of struct buf and specialized 2298fb35bd37SJoerg Wunsch * for the current context. 2299f5f7ba03SJordan K. Hubbard */ 2300f664aeeeSJoerg Wunsch fdstrategy(bp); 2301f664aeeeSJoerg Wunsch while ((bp->bio_flags & BIO_DONE) == 0) 2302f664aeeeSJoerg Wunsch tsleep(bp, PRIBIO, "fdcmd", 0); 2303f664aeeeSJoerg Wunsch 2304f664aeeeSJoerg Wunsch free(bp, M_TEMP); 2305f664aeeeSJoerg Wunsch return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0); 2306f664aeeeSJoerg Wunsch } 23075b81b6b3SRodney W. Grimes 23083e425b96SJulian Elischer static int 23094c34deeeSJoerg Wunsch fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 2310f5f7ba03SJordan K. Hubbard { 2311f664aeeeSJoerg Wunsch fdu_t fdu; 2312f664aeeeSJoerg Wunsch fd_p fd; 2313f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2314fb35bd37SJoerg Wunsch struct disklabel *lp; 23152995d110SJoerg Wunsch struct fdc_status *fsp; 2316250300ebSJoerg Wunsch struct fdc_readid *rid; 2317f664aeeeSJoerg Wunsch size_t fdblk; 2318fb35bd37SJoerg Wunsch int error; 2319f5f7ba03SJordan K. Hubbard 2320f664aeeeSJoerg Wunsch fdu = FDUNIT(minor(dev)); 2321f664aeeeSJoerg Wunsch fd = devclass_get_softc(fd_devclass, fdu); 2322fb35bd37SJoerg Wunsch 2323fb35bd37SJoerg Wunsch 23243a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2325fb35bd37SJoerg Wunsch error = 0; 2326f5f7ba03SJordan K. Hubbard 23276182fdbdSPeter Wemm switch (cmd) { 2328f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2329fb35bd37SJoerg Wunsch lp = malloc(sizeof(*lp), M_TEMP, M_ZERO); 2330fb35bd37SJoerg Wunsch lp->d_secsize = fdblk; 23316182fdbdSPeter Wemm fdt = fd->ft; 2332fb35bd37SJoerg Wunsch lp->d_secpercyl = fdt->size / fdt->tracks; 2333fb35bd37SJoerg Wunsch lp->d_type = DTYPE_FLOPPY; 2334fb35bd37SJoerg Wunsch if (readdisklabel(dkmodpart(dev, RAW_PART), lp) != NULL) 2335f5f7ba03SJordan K. Hubbard error = EINVAL; 2336fb35bd37SJoerg Wunsch else 2337fb35bd37SJoerg Wunsch *(struct disklabel *)addr = *lp; 2338fb35bd37SJoerg Wunsch free(lp, M_TEMP); 2339f5f7ba03SJordan K. Hubbard break; 2340f5f7ba03SJordan K. Hubbard 2341f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2342f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2343fb35bd37SJoerg Wunsch return (EBADF); 2344fb35bd37SJoerg Wunsch /* 2345fb35bd37SJoerg Wunsch * XXX perhaps should call setdisklabel() to do error checking 2346fb35bd37SJoerg Wunsch * although there is nowhere to "set" the result. Perhaps 2347fb35bd37SJoerg Wunsch * should always just fail. 2348fb35bd37SJoerg Wunsch */ 2349f5f7ba03SJordan K. Hubbard break; 2350f5f7ba03SJordan K. Hubbard 2351f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2352f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2353fb35bd37SJoerg Wunsch return (EBADF); 2354f5f7ba03SJordan K. Hubbard break; 2355f5f7ba03SJordan K. Hubbard 2356f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 2357fb35bd37SJoerg Wunsch if ((flag & FWRITE) == 0) 2358fb35bd37SJoerg Wunsch return (EBADF); 2359fb35bd37SJoerg Wunsch lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO); 2360fb35bd37SJoerg Wunsch error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0); 2361fb35bd37SJoerg Wunsch if (error != 0) 2362fb35bd37SJoerg Wunsch error = writedisklabel(dev, lp); 2363fb35bd37SJoerg Wunsch free(lp, M_TEMP); 2364b39c878eSAndrey A. Chernov break; 2365f664aeeeSJoerg Wunsch 2366b39c878eSAndrey A. Chernov case FD_FORM: 2367b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2368fb35bd37SJoerg Wunsch return (EBADF); /* must be opened for writing */ 2369fb35bd37SJoerg Wunsch if (((struct fd_formb *)addr)->format_version != 2370b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2371fb35bd37SJoerg Wunsch return (EINVAL); /* wrong version of formatting prog */ 2372f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_FORMAT, addr); 2373b39c878eSAndrey A. Chernov break; 2374b39c878eSAndrey A. Chernov 2375b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 23763e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2377f5f7ba03SJordan K. Hubbard break; 2378f5f7ba03SJordan K. Hubbard 23793a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 23803a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 2381f711d546SPoul-Henning Kamp if (suser(p) != 0) 2382fb35bd37SJoerg Wunsch return (EPERM); 23833e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 23843a2f7427SDavid Greenman break; 23853a2f7427SDavid Greenman 23863a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 23873e425b96SJulian Elischer *(int *)addr = fd->options; 23883a2f7427SDavid Greenman break; 23893a2f7427SDavid Greenman 23903a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 23913e425b96SJulian Elischer fd->options = *(int *)addr; 23923a2f7427SDavid Greenman break; 23933a2f7427SDavid Greenman 2394f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG 2395f664aeeeSJoerg Wunsch case FD_DEBUG: 2396f664aeeeSJoerg Wunsch fd_debug = *(int *)addr; 2397f664aeeeSJoerg Wunsch break; 2398f664aeeeSJoerg Wunsch #endif 2399f664aeeeSJoerg Wunsch 24002995d110SJoerg Wunsch case FD_CLRERR: 24012995d110SJoerg Wunsch if (suser(p) != 0) 2402fb35bd37SJoerg Wunsch return (EPERM); 24032995d110SJoerg Wunsch fd->fdc->fdc_errs = 0; 24042995d110SJoerg Wunsch break; 24052995d110SJoerg Wunsch 24062995d110SJoerg Wunsch case FD_GSTAT: 24072995d110SJoerg Wunsch fsp = (struct fdc_status *)addr; 24082995d110SJoerg Wunsch if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2409fb35bd37SJoerg Wunsch return (EINVAL); 24102995d110SJoerg Wunsch memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 24112995d110SJoerg Wunsch break; 24122995d110SJoerg Wunsch 2413250300ebSJoerg Wunsch case FD_READID: 2414250300ebSJoerg Wunsch rid = (struct fdc_readid *)addr; 2415250300ebSJoerg Wunsch if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2416fb35bd37SJoerg Wunsch return (EINVAL); 2417f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_RDSECTID, addr); 2418250300ebSJoerg Wunsch break; 2419250300ebSJoerg Wunsch 2420f5f7ba03SJordan K. Hubbard default: 24213a2f7427SDavid Greenman error = ENOTTY; 2422f5f7ba03SJordan K. Hubbard break; 2423f5f7ba03SJordan K. Hubbard } 2424f5f7ba03SJordan K. Hubbard return (error); 2425f5f7ba03SJordan K. Hubbard } 2426