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; 1846182fdbdSPeter Wemm device_t dev; 1856182fdbdSPeter Wemm fdu_t fdu; 1866182fdbdSPeter Wemm }; 18737286586SPeter Wemm 18837286586SPeter Wemm struct fdc_ivars { 18937286586SPeter Wemm int fdunit; 19037286586SPeter Wemm }; 1916182fdbdSPeter Wemm static devclass_t fd_devclass; 1925b81b6b3SRodney W. Grimes 1935b81b6b3SRodney W. Grimes /***********************************************************************\ 1945b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 1955b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 1965b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 1975b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 1985b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 1995b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 2005b81b6b3SRodney W. Grimes \***********************************************************************/ 201b99f0a4aSAndrew Moore 2023a2f7427SDavid Greenman /* internal functions */ 20364860614SJoerg Wunsch static driver_intr_t fdc_intr; 2046182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 2053a2f7427SDavid Greenman # define TURNON 1 2063a2f7427SDavid Greenman # define TURNOFF 0 2073a2f7427SDavid Greenman static timeout_t fd_turnoff; 2083a2f7427SDavid Greenman static timeout_t fd_motor_on; 2096182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 2103a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2116182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 21280909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int); 2136182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 2145c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 2153a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2166182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 2176182fdbdSPeter Wemm static int retrier(struct fdc_data *); 218f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *); 2193a2f7427SDavid Greenman 220d66c374fSTor Egge static int enable_fifo(fdc_p fdc); 22180909a7dSJoerg Wunsch static void fd_clone (void *arg, char *name, int namelen, dev_t *dev); 222d66c374fSTor Egge 223d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 224d66c374fSTor Egge 225d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 226cba2a7c6SBruce Evans static char const * const fdstates[] = 2275b81b6b3SRodney W. Grimes { 2285b81b6b3SRodney W. Grimes "DEVIDLE", 2295b81b6b3SRodney W. Grimes "FINDWORK", 2305b81b6b3SRodney W. Grimes "DOSEEK", 2315b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 2325b81b6b3SRodney W. Grimes "IOCOMPLETE", 2335b81b6b3SRodney W. Grimes "RECALCOMPLETE", 2345b81b6b3SRodney W. Grimes "STARTRECAL", 2355b81b6b3SRodney W. Grimes "RESETCTLR", 2365b81b6b3SRodney W. Grimes "SEEKWAIT", 2375b81b6b3SRodney W. Grimes "RECALWAIT", 2385b81b6b3SRodney W. Grimes "MOTORWAIT", 2395c1a1eaeSBruce Evans "IOTIMEDOUT", 2405c1a1eaeSBruce Evans "RESETCOMPLETE", 241ff9607b0SBruce Evans "PIOREAD", 2425b81b6b3SRodney W. Grimes }; 2435b81b6b3SRodney W. Grimes 2443a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 245cba2a7c6SBruce Evans static int volatile fd_debug = 0; 2465b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 2475b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 248d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 2495b81b6b3SRodney W. Grimes #define TRACE0(arg) 2505b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 251d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 2525b81b6b3SRodney W. Grimes 253427ccf00SDoug Rabson static void 254427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 255427ccf00SDoug Rabson { 256427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 257427ccf00SDoug Rabson } 258427ccf00SDoug Rabson 259427ccf00SDoug Rabson static u_int8_t 260427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 261427ccf00SDoug Rabson { 262427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 263427ccf00SDoug Rabson } 264427ccf00SDoug Rabson 265427ccf00SDoug Rabson static void 266427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 267427ccf00SDoug Rabson { 268427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 269427ccf00SDoug Rabson } 270427ccf00SDoug Rabson 271427ccf00SDoug Rabson static u_int8_t 272427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 273427ccf00SDoug Rabson { 274427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 275427ccf00SDoug Rabson } 276427ccf00SDoug Rabson 277427ccf00SDoug Rabson static void 2785f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 279427ccf00SDoug Rabson { 280427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 281427ccf00SDoug Rabson } 282427ccf00SDoug Rabson 28358c9d623SPeter Wemm #if NCARD > 0 2845f830ea2SJoerg Wunsch static void 2855f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 2865f830ea2SJoerg Wunsch { 2875f830ea2SJoerg Wunsch bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 2885f830ea2SJoerg Wunsch } 28958c9d623SPeter Wemm #endif 2905f830ea2SJoerg Wunsch 291427ccf00SDoug Rabson #if 0 292427ccf00SDoug Rabson 293427ccf00SDoug Rabson static u_int8_t 294427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 295427ccf00SDoug Rabson { 296427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 297427ccf00SDoug Rabson } 298427ccf00SDoug Rabson 299427ccf00SDoug Rabson #endif 300427ccf00SDoug Rabson 301f8ce7dd5SJoerg Wunsch /* 30264860614SJoerg Wunsch * The open function is named Fdopen() to avoid confusion with fdopen() 30364860614SJoerg Wunsch * in fd(4). The difference is now only meaningful for debuggers. 304f8ce7dd5SJoerg Wunsch */ 305f8ce7dd5SJoerg Wunsch static d_open_t Fdopen; 30687f6c662SJulian Elischer static d_close_t fdclose; 30787f6c662SJulian Elischer static d_ioctl_t fdioctl; 30887f6c662SJulian Elischer static d_strategy_t fdstrategy; 30987f6c662SJulian Elischer 31087f6c662SJulian Elischer #define CDEV_MAJOR 9 3114e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 3124e2f199eSPoul-Henning Kamp /* open */ Fdopen, 3134e2f199eSPoul-Henning Kamp /* close */ fdclose, 3144e2f199eSPoul-Henning Kamp /* read */ physread, 3154e2f199eSPoul-Henning Kamp /* write */ physwrite, 3164e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 3174e2f199eSPoul-Henning Kamp /* poll */ nopoll, 3184e2f199eSPoul-Henning Kamp /* mmap */ nommap, 3194e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 3204e2f199eSPoul-Henning Kamp /* name */ "fd", 3214e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 3224e2f199eSPoul-Henning Kamp /* dump */ nodump, 3234e2f199eSPoul-Henning Kamp /* psize */ nopsize, 3244e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 3254e2f199eSPoul-Henning Kamp }; 3264e2f199eSPoul-Henning Kamp 327dc5df763SJoerg Wunsch static int 3286182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 329dc5df763SJoerg Wunsch { 3306182fdbdSPeter Wemm fdc->fdc_errs++; 33116b04b6aSJoerg Wunsch if (s) { 332b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 333b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 334b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 335b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 336b6e5f28eSPeter Wemm "logging any more\n"); 33716b04b6aSJoerg Wunsch } 338dc5df763SJoerg Wunsch 339dc5df763SJoerg Wunsch return FD_FAILED; 340dc5df763SJoerg Wunsch } 341dc5df763SJoerg Wunsch 342dc5df763SJoerg Wunsch /* 343dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 344dc5df763SJoerg Wunsch * Unit number, 345dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 346dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 347dc5df763SJoerg Wunsch */ 3486f4e0bebSPoul-Henning Kamp static int 3496182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 350dc5df763SJoerg Wunsch { 351dc5df763SJoerg Wunsch u_char cmd; 352dc5df763SJoerg Wunsch int n_in; 353dc5df763SJoerg Wunsch int n; 354dc5df763SJoerg Wunsch va_list ap; 355dc5df763SJoerg Wunsch 356dc5df763SJoerg Wunsch va_start(ap, n_out); 357dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 358dc5df763SJoerg Wunsch va_end(ap); 359dc5df763SJoerg Wunsch va_start(ap, n_out); 360dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 361dc5df763SJoerg Wunsch { 3626182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 363dc5df763SJoerg Wunsch { 364dc5df763SJoerg Wunsch char msg[50]; 3652127f260SArchie Cobbs snprintf(msg, sizeof(msg), 366dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 367dc5df763SJoerg Wunsch cmd, n + 1, n_out); 3686182fdbdSPeter Wemm return fdc_err(fdc, msg); 369dc5df763SJoerg Wunsch } 370dc5df763SJoerg Wunsch } 371dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 372dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 373dc5df763SJoerg Wunsch { 374dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 3756182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 376dc5df763SJoerg Wunsch { 377dc5df763SJoerg Wunsch char msg[50]; 3782127f260SArchie Cobbs snprintf(msg, sizeof(msg), 379dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 380dc5df763SJoerg Wunsch cmd, n + 1, n_in); 3816182fdbdSPeter Wemm return fdc_err(fdc, msg); 382dc5df763SJoerg Wunsch } 383dc5df763SJoerg Wunsch } 384dc5df763SJoerg Wunsch 385dc5df763SJoerg Wunsch return 0; 386dc5df763SJoerg Wunsch } 387dc5df763SJoerg Wunsch 3886f4e0bebSPoul-Henning Kamp static int 389d66c374fSTor Egge enable_fifo(fdc_p fdc) 390d66c374fSTor Egge { 391d66c374fSTor Egge int i, j; 392d66c374fSTor Egge 393d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 394d66c374fSTor Egge 395d66c374fSTor Egge /* 396d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 397d66c374fSTor Egge * this might be an invalid command. Thus we send the 398d66c374fSTor Egge * first byte, and check for an early turn of data directon. 399d66c374fSTor Egge */ 400d66c374fSTor Egge 4016182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 4026182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 403d66c374fSTor Egge 404d66c374fSTor Egge /* If command is invalid, return */ 405d66c374fSTor Egge j = 100000; 406427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 407d66c374fSTor Egge != NE7_RQM && j-- > 0) 408d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 409d66c374fSTor Egge fdc_reset(fdc); 410d66c374fSTor Egge return FD_FAILED; 411d66c374fSTor Egge } 412d66c374fSTor Egge if (j<0 || 4136182fdbdSPeter Wemm fd_cmd(fdc, 3, 414d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 415d66c374fSTor Egge fdc_reset(fdc); 4166182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 417d66c374fSTor Egge } 418d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 419d66c374fSTor Egge return 0; 420d66c374fSTor Egge } 4216182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 422d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 4236182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 424d66c374fSTor Egge return 0; 425d66c374fSTor Egge } 426d66c374fSTor Egge 427d66c374fSTor Egge static int 428dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 429dc5df763SJoerg Wunsch { 430dc5df763SJoerg Wunsch int st3; 431dc5df763SJoerg Wunsch 4326182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 433dc5df763SJoerg Wunsch { 4346182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 435dc5df763SJoerg Wunsch } 436dc5df763SJoerg Wunsch if (st3p) 437dc5df763SJoerg Wunsch *st3p = st3; 438dc5df763SJoerg Wunsch 439dc5df763SJoerg Wunsch return 0; 440dc5df763SJoerg Wunsch } 441dc5df763SJoerg Wunsch 4426f4e0bebSPoul-Henning Kamp static int 443dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 444dc5df763SJoerg Wunsch { 4456182fdbdSPeter Wemm int cyl, st0, ret; 446dc5df763SJoerg Wunsch 4476182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 4486182fdbdSPeter Wemm if (ret) { 4496182fdbdSPeter Wemm (void)fdc_err(fdc, 450dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 451dc5df763SJoerg Wunsch return ret; 452dc5df763SJoerg Wunsch } 453dc5df763SJoerg Wunsch 454dc5df763SJoerg Wunsch if (st0p) 455dc5df763SJoerg Wunsch *st0p = st0; 456dc5df763SJoerg Wunsch 4576182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 458dc5df763SJoerg Wunsch /* 459dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 460dc5df763SJoerg Wunsch */ 461dc5df763SJoerg Wunsch return FD_NOT_VALID; 462dc5df763SJoerg Wunsch } 463dc5df763SJoerg Wunsch 4646182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 4656182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 466dc5df763SJoerg Wunsch } 467dc5df763SJoerg Wunsch 468dc5df763SJoerg Wunsch if (cylp) 469dc5df763SJoerg Wunsch *cylp = cyl; 470dc5df763SJoerg Wunsch 471dc5df763SJoerg Wunsch return 0; 472dc5df763SJoerg Wunsch } 473dc5df763SJoerg Wunsch 474dc5df763SJoerg Wunsch 4756f4e0bebSPoul-Henning Kamp static int 47664860614SJoerg Wunsch fd_read_status(fdc_p fdc) 477dc5df763SJoerg Wunsch { 478dc5df763SJoerg Wunsch int i, ret; 479b5e8ce9fSBruce Evans 4806182fdbdSPeter Wemm for (i = 0; i < 7; i++) { 481b5e8ce9fSBruce Evans /* 48264860614SJoerg Wunsch * XXX types are poorly chosen. Only bytes can be read 483a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 484b5e8ce9fSBruce Evans * fd_in() gives ints. 485b5e8ce9fSBruce Evans */ 486b5e8ce9fSBruce Evans int status; 487b5e8ce9fSBruce Evans 4886182fdbdSPeter Wemm ret = fd_in(fdc, &status); 489b5e8ce9fSBruce Evans fdc->status[i] = status; 490b5e8ce9fSBruce Evans if (ret != 0) 491dc5df763SJoerg Wunsch break; 492dc5df763SJoerg Wunsch } 493dc5df763SJoerg Wunsch 494dc5df763SJoerg Wunsch if (ret == 0) 495dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 496dc5df763SJoerg Wunsch else 497dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 498dc5df763SJoerg Wunsch 499dc5df763SJoerg Wunsch return ret; 500dc5df763SJoerg Wunsch } 501dc5df763SJoerg Wunsch 5025b81b6b3SRodney W. Grimes /****************************************************************************/ 5035b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5045b81b6b3SRodney W. Grimes /****************************************************************************/ 505dc5df763SJoerg Wunsch 5063a2f7427SDavid Greenman static int 50737286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 5085b81b6b3SRodney W. Grimes { 50937286586SPeter Wemm device_t dev; 510e219897aSJoerg Wunsch int ispnp, ispcmcia, nports; 5115b81b6b3SRodney W. Grimes 51237286586SPeter Wemm dev = fdc->fdc_dev; 5135f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 5145f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 5156182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 5166182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 5176182fdbdSPeter Wemm 518b6e5f28eSPeter Wemm /* 5195f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 5205f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 5215f830ea2SJoerg Wunsch * register at 0x3f6. 5225f830ea2SJoerg Wunsch * 523b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 5245f830ea2SJoerg Wunsch * 5255f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 5265f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 5275f830ea2SJoerg Wunsch * one with offset 7 as control register. 528b6e5f28eSPeter Wemm */ 529e219897aSJoerg Wunsch nports = ispcmcia ? 8 : (ispnp ? 1 : 6); 5306182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 5316182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 532e219897aSJoerg Wunsch nports, RF_ACTIVE); 5336182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 534e219897aSJoerg Wunsch device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 535e219897aSJoerg Wunsch nports); 53637286586SPeter Wemm return ENXIO; 5376182fdbdSPeter Wemm } 538427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 539427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 540427ccf00SDoug Rabson 5415f830ea2SJoerg Wunsch if (!ispcmcia) { 542427ccf00SDoug Rabson /* 5435f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 5445f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 5455f830ea2SJoerg Wunsch * by checking the size and adjust the port address 5465f830ea2SJoerg Wunsch * accordingly. 547b6e5f28eSPeter Wemm */ 548b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 549b6e5f28eSPeter Wemm fdc->port_off = -2; 550b6e5f28eSPeter Wemm 551b6e5f28eSPeter Wemm /* 5525f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 5535f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 5545f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 555a2639a18SPeter Wemm * 5565f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 5575f830ea2SJoerg Wunsch * leave out the control register! It seems that some 5585f830ea2SJoerg Wunsch * non-antique controller chips have a different 5595f830ea2SJoerg Wunsch * method of programming the transfer speed which 5605f830ea2SJoerg Wunsch * doesn't require the control register, but it's 5615f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 5625f830ea2SJoerg Wunsch * address for the control register. 563427ccf00SDoug Rabson */ 564b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 565b9da888fSPeter Wemm u_long ctlstart; 566b9da888fSPeter Wemm 567b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 5685f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 5695f830ea2SJoerg Wunsch fdc->port_off + 7; 570b6e5f28eSPeter Wemm 571b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 572b9da888fSPeter Wemm } 573b6e5f28eSPeter Wemm 574b6e5f28eSPeter Wemm /* 575b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 576b6e5f28eSPeter Wemm */ 577427ccf00SDoug Rabson fdc->rid_ctl = 1; 5785f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 5795f830ea2SJoerg Wunsch &fdc->rid_ctl, 580b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 581427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 5825f830ea2SJoerg Wunsch device_printf(dev, 583e219897aSJoerg Wunsch "cannot reserve control I/O port range (control port)\n"); 58437286586SPeter Wemm return ENXIO; 585427ccf00SDoug Rabson } 586427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 587427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 5885f830ea2SJoerg Wunsch } 5896182fdbdSPeter Wemm 5906182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 5916182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 5926182fdbdSPeter Wemm RF_ACTIVE); 5936182fdbdSPeter Wemm if (fdc->res_irq == 0) { 594427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 59537286586SPeter Wemm return ENXIO; 5966182fdbdSPeter Wemm } 5975f830ea2SJoerg Wunsch 5985f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 5996182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 6006182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 6016182fdbdSPeter Wemm RF_ACTIVE); 6026182fdbdSPeter Wemm if (fdc->res_drq == 0) { 603427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 60437286586SPeter Wemm return ENXIO; 6056182fdbdSPeter Wemm } 6066182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 6075f830ea2SJoerg Wunsch } 60837286586SPeter Wemm 60937286586SPeter Wemm return 0; 61037286586SPeter Wemm } 61137286586SPeter Wemm 61237286586SPeter Wemm static void 61337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 61437286586SPeter Wemm { 61537286586SPeter Wemm device_t dev; 61637286586SPeter Wemm 61737286586SPeter Wemm dev = fdc->fdc_dev; 61837286586SPeter Wemm if (fdc->res_irq != 0) { 61937286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62037286586SPeter Wemm fdc->res_irq); 62137286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62237286586SPeter Wemm fdc->res_irq); 62337286586SPeter Wemm } 62437286586SPeter Wemm if (fdc->res_ctl != 0) { 62537286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 62637286586SPeter Wemm fdc->res_ctl); 62737286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 62837286586SPeter Wemm fdc->res_ctl); 62937286586SPeter Wemm } 63037286586SPeter Wemm if (fdc->res_ioport != 0) { 63137286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 63237286586SPeter Wemm fdc->res_ioport); 63337286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 63437286586SPeter Wemm fdc->res_ioport); 63537286586SPeter Wemm } 63637286586SPeter Wemm if (fdc->res_drq != 0) { 63737286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 63837286586SPeter Wemm fdc->res_drq); 63937286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64037286586SPeter Wemm fdc->res_drq); 64137286586SPeter Wemm } 64237286586SPeter Wemm } 64337286586SPeter Wemm 64437286586SPeter Wemm /****************************************************************************/ 64537286586SPeter Wemm /* autoconfiguration stuff */ 64637286586SPeter Wemm /****************************************************************************/ 64737286586SPeter Wemm 64837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 64937286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 65037286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 65137286586SPeter Wemm {0} 65237286586SPeter Wemm }; 65337286586SPeter Wemm 65437286586SPeter Wemm static int 6553aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 65637286586SPeter Wemm { 65737286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 65837286586SPeter Wemm 65937286586SPeter Wemm switch (which) { 66037286586SPeter Wemm case FDC_IVAR_FDUNIT: 66137286586SPeter Wemm *result = ivars->fdunit; 66237286586SPeter Wemm break; 66337286586SPeter Wemm default: 66437286586SPeter Wemm return ENOENT; 66537286586SPeter Wemm } 66637286586SPeter Wemm return 0; 66737286586SPeter Wemm } 66837286586SPeter Wemm 66937286586SPeter Wemm /* 67037286586SPeter Wemm * fdc controller section. 67137286586SPeter Wemm */ 67237286586SPeter Wemm static int 67337286586SPeter Wemm fdc_probe(device_t dev) 67437286586SPeter Wemm { 67537286586SPeter Wemm int error, ic_type; 67637286586SPeter Wemm struct fdc_data *fdc; 67737286586SPeter Wemm 67837286586SPeter Wemm fdc = device_get_softc(dev); 67937286586SPeter Wemm bzero(fdc, sizeof *fdc); 68037286586SPeter Wemm fdc->fdc_dev = dev; 6815f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 68237286586SPeter Wemm 68337286586SPeter Wemm /* Check pnp ids */ 68437286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 68537286586SPeter Wemm if (error == ENXIO) 68637286586SPeter Wemm return ENXIO; 6875f830ea2SJoerg Wunsch if (error == 0) 6885f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 68937286586SPeter Wemm 69037286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 69137286586SPeter Wemm error = fdc_alloc_resources(fdc); 69237286586SPeter Wemm if (error) 69337286586SPeter Wemm goto out; 6945b81b6b3SRodney W. Grimes 69516111cedSAndrew Moore /* First - lets reset the floppy controller */ 696427ccf00SDoug Rabson fdout_wr(fdc, 0); 69716111cedSAndrew Moore DELAY(100); 698427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 69916111cedSAndrew Moore 7005b81b6b3SRodney W. Grimes /* see if it can handle a command */ 7016182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7026182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 7036182fdbdSPeter Wemm error = ENXIO; 7046182fdbdSPeter Wemm goto out; 7055b81b6b3SRodney W. Grimes } 7066182fdbdSPeter Wemm 7076182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 7086182fdbdSPeter Wemm ic_type = (u_char)ic_type; 7096182fdbdSPeter Wemm switch (ic_type) { 7106182fdbdSPeter Wemm case 0x80: 7116182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 7126182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 7136182fdbdSPeter Wemm break; 7146182fdbdSPeter Wemm case 0x81: 7156182fdbdSPeter Wemm device_set_desc(dev, "Intel 82077 or clone"); 7166182fdbdSPeter Wemm fdc->fdct = FDC_I82077; 7176182fdbdSPeter Wemm break; 7186182fdbdSPeter Wemm case 0x90: 7196182fdbdSPeter Wemm device_set_desc(dev, "NEC 72065B or clone"); 7206182fdbdSPeter Wemm fdc->fdct = FDC_NE72065; 7216182fdbdSPeter Wemm break; 7226182fdbdSPeter Wemm default: 7236182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 7246182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 7256182fdbdSPeter Wemm break; 7266182fdbdSPeter Wemm } 7276182fdbdSPeter Wemm } 7286182fdbdSPeter Wemm 7296182fdbdSPeter Wemm out: 73037286586SPeter Wemm fdc_release_resources(fdc); 7316182fdbdSPeter Wemm return (error); 7325b81b6b3SRodney W. Grimes } 7335b81b6b3SRodney W. Grimes 7345f830ea2SJoerg Wunsch #if NCARD > 0 7355f830ea2SJoerg Wunsch 7365f830ea2SJoerg Wunsch static int 7375f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 7385f830ea2SJoerg Wunsch { 7395f830ea2SJoerg Wunsch int error; 7405f830ea2SJoerg Wunsch struct fdc_data *fdc; 7415f830ea2SJoerg Wunsch 7425f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7435f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 7445f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 7455f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 7465f830ea2SJoerg Wunsch 7475f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 7485f830ea2SJoerg Wunsch 7495f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 7505f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 7515f830ea2SJoerg Wunsch if (error) 7525f830ea2SJoerg Wunsch goto out; 7535f830ea2SJoerg Wunsch 7545f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 7555f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 7565f830ea2SJoerg Wunsch DELAY(100); 7575f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 7585f830ea2SJoerg Wunsch 7595f830ea2SJoerg Wunsch /* see if it can handle a command */ 7605f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7615f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 7625f830ea2SJoerg Wunsch error = ENXIO; 7635f830ea2SJoerg Wunsch goto out; 7645f830ea2SJoerg Wunsch } 7655f830ea2SJoerg Wunsch 7665f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 7675f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 7685f830ea2SJoerg Wunsch 7695f830ea2SJoerg Wunsch out: 7705f830ea2SJoerg Wunsch fdc_release_resources(fdc); 7715f830ea2SJoerg Wunsch return (error); 7725f830ea2SJoerg Wunsch } 7735f830ea2SJoerg Wunsch 774e219897aSJoerg Wunsch #endif /* NCARD > 0 */ 775e219897aSJoerg Wunsch 7765f830ea2SJoerg Wunsch static int 777e219897aSJoerg Wunsch fdc_detach(device_t dev) 7785f830ea2SJoerg Wunsch { 7795f830ea2SJoerg Wunsch struct fdc_data *fdc; 7805f830ea2SJoerg Wunsch int error; 7815f830ea2SJoerg Wunsch 7825f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7835f830ea2SJoerg Wunsch 7845f830ea2SJoerg Wunsch /* have our children detached first */ 7855f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 7865f830ea2SJoerg Wunsch return (error); 7875f830ea2SJoerg Wunsch 788e219897aSJoerg Wunsch /* reset controller, turn motor off */ 789e219897aSJoerg Wunsch fdout_wr(fdc, 0); 790e219897aSJoerg Wunsch 791e219897aSJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) 792e219897aSJoerg Wunsch isa_dma_release(fdc->dmachan); 793e219897aSJoerg Wunsch 7945f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 7955f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 7965f830ea2SJoerg Wunsch return (0); 7975f830ea2SJoerg Wunsch } 7985f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 7995f830ea2SJoerg Wunsch 8005f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 8015f830ea2SJoerg Wunsch fdc->fdc_intr); 8025f830ea2SJoerg Wunsch fdc_release_resources(fdc); 8035f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 8045f830ea2SJoerg Wunsch return (0); 8055f830ea2SJoerg Wunsch } 8065f830ea2SJoerg Wunsch 8075b81b6b3SRodney W. Grimes /* 80837286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc. 8095b81b6b3SRodney W. Grimes */ 8106182fdbdSPeter Wemm static void 81137286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit) 8125b81b6b3SRodney W. Grimes { 81337286586SPeter Wemm int disabled; 81437286586SPeter Wemm struct fdc_ivars *ivar; 8156182fdbdSPeter Wemm device_t child; 81692200632SGarrett Wollman 8177cc0979fSDavid Malone ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 81837286586SPeter Wemm if (ivar == NULL) 8196182fdbdSPeter Wemm return; 82037286586SPeter Wemm if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 82137286586SPeter Wemm ivar->fdunit = 0; 822fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit); 82337286586SPeter Wemm if (child == NULL) 8246182fdbdSPeter Wemm return; 82537286586SPeter Wemm device_set_ivars(child, ivar); 826a97c75b7SDoug Rabson if (resource_int_value(name, unit, "disabled", &disabled) == 0 827a97c75b7SDoug Rabson && disabled != 0) 8286182fdbdSPeter Wemm device_disable(child); 8296182fdbdSPeter Wemm } 8306182fdbdSPeter Wemm 8316182fdbdSPeter Wemm static int 8326182fdbdSPeter Wemm fdc_attach(device_t dev) 8336182fdbdSPeter Wemm { 83437286586SPeter Wemm struct fdc_data *fdc; 8352398f0cdSPeter Wemm int i, error, dunit; 8362398f0cdSPeter Wemm const char *name, *dname; 837427ccf00SDoug Rabson 83837286586SPeter Wemm fdc = device_get_softc(dev); 83937286586SPeter Wemm error = fdc_alloc_resources(fdc); 84037286586SPeter Wemm if (error) { 841f8ce7dd5SJoerg Wunsch device_printf(dev, "cannot re-acquire resources\n"); 84237286586SPeter Wemm return error; 84337286586SPeter Wemm } 84437286586SPeter Wemm error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 8453c36743eSMark Murray INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 8463c36743eSMark Murray &fdc->fdc_intr); 84737286586SPeter Wemm if (error) { 84837286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n"); 84937286586SPeter Wemm return error; 85037286586SPeter Wemm } 85137286586SPeter Wemm fdc->fdcu = device_get_unit(dev); 8525b81b6b3SRodney W. Grimes fdc->flags |= FDC_ATTACHED; 8536182fdbdSPeter Wemm 8545f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 855f8ce7dd5SJoerg Wunsch /* 856f8ce7dd5SJoerg Wunsch * Acquire the DMA channel forever, the driver will do 857f8ce7dd5SJoerg Wunsch * the rest 858f8ce7dd5SJoerg Wunsch * XXX should integrate with rman 859f8ce7dd5SJoerg Wunsch */ 860100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 861f8ce7dd5SJoerg Wunsch isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 8625f830ea2SJoerg Wunsch } 8635b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 8646182fdbdSPeter Wemm 8653a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 86664860614SJoerg Wunsch fdout_wr(fdc, fdc->fdout = 0); 8678177437dSPoul-Henning Kamp bioq_init(&fdc->head); 8685b81b6b3SRodney W. Grimes 8696182fdbdSPeter Wemm /* 87037286586SPeter Wemm * Probe and attach any children. We should probably detect 87137286586SPeter Wemm * devices from the BIOS unless overridden. 8726182fdbdSPeter Wemm */ 873ada54f9eSPeter Wemm name = device_get_nameunit(dev); 8742398f0cdSPeter Wemm i = 0; 8752398f0cdSPeter Wemm while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 8762398f0cdSPeter Wemm fdc_add_child(dev, dname, dunit); 87737286586SPeter Wemm 8786182fdbdSPeter Wemm return (bus_generic_attach(dev)); 8796182fdbdSPeter Wemm } 8806182fdbdSPeter Wemm 88115317dd8SMatthew N. Dodd static int 8826182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 8836182fdbdSPeter Wemm { 88415317dd8SMatthew N. Dodd int retval = 0; 88515317dd8SMatthew N. Dodd 88615317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 88715317dd8SMatthew N. Dodd retval += printf(" on %s drive %d\n", device_get_nameunit(me), 88837286586SPeter Wemm fdc_get_fdunit(child)); 88915317dd8SMatthew N. Dodd 89015317dd8SMatthew N. Dodd return (retval); 8916182fdbdSPeter Wemm } 8926182fdbdSPeter Wemm 89316e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 89416e68fc6SPeter Wemm /* Device interface */ 89516e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 89616e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 897e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 89816e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 89916e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 90016e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 90116e68fc6SPeter Wemm 90216e68fc6SPeter Wemm /* Bus interface */ 90316e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 90437286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 90516e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 90616e68fc6SPeter Wemm 90716e68fc6SPeter Wemm { 0, 0 } 90816e68fc6SPeter Wemm }; 90916e68fc6SPeter Wemm 91016e68fc6SPeter Wemm static driver_t fdc_driver = { 91116e68fc6SPeter Wemm "fdc", 91216e68fc6SPeter Wemm fdc_methods, 91316e68fc6SPeter Wemm sizeof(struct fdc_data) 91416e68fc6SPeter Wemm }; 91516e68fc6SPeter Wemm 91616e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 91716e68fc6SPeter Wemm 9185f830ea2SJoerg Wunsch #if NCARD > 0 9195f830ea2SJoerg Wunsch 9205f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 9215f830ea2SJoerg Wunsch /* Device interface */ 9225f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 9235f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 924e219897aSJoerg Wunsch DEVMETHOD(device_detach, fdc_detach), 9255f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 9265f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 9275f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 9285f830ea2SJoerg Wunsch 9295f830ea2SJoerg Wunsch /* Bus interface */ 9305f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 9315f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 9325f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 9335f830ea2SJoerg Wunsch 9345f830ea2SJoerg Wunsch { 0, 0 } 9355f830ea2SJoerg Wunsch }; 9365f830ea2SJoerg Wunsch 9375f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 9385f830ea2SJoerg Wunsch "fdc", 9395f830ea2SJoerg Wunsch fdc_pccard_methods, 9405f830ea2SJoerg Wunsch sizeof(struct fdc_data) 9415f830ea2SJoerg Wunsch }; 9425f830ea2SJoerg Wunsch 9435f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 9445f830ea2SJoerg Wunsch 9455f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 9465f830ea2SJoerg Wunsch 9473f54a085SPoul-Henning Kamp static struct { 9483f54a085SPoul-Henning Kamp char *match; 9493f54a085SPoul-Henning Kamp int minor; 9503f54a085SPoul-Henning Kamp int link; 9513f54a085SPoul-Henning Kamp } fd_suffix[] = { 9523f54a085SPoul-Henning Kamp { "a", 0, 1 }, 9533f54a085SPoul-Henning Kamp { "b", 0, 1 }, 9543f54a085SPoul-Henning Kamp { "c", 0, 1 }, 9553f54a085SPoul-Henning Kamp { "d", 0, 1 }, 9563f54a085SPoul-Henning Kamp { "e", 0, 1 }, 9573f54a085SPoul-Henning Kamp { "f", 0, 1 }, 9583f54a085SPoul-Henning Kamp { "g", 0, 1 }, 9593f54a085SPoul-Henning Kamp { "h", 0, 1 }, 9603f54a085SPoul-Henning Kamp { ".1720", 1, 0 }, 9613f54a085SPoul-Henning Kamp { ".1480", 2, 0 }, 9623f54a085SPoul-Henning Kamp { ".1440", 3, 0 }, 9633f54a085SPoul-Henning Kamp { ".1200", 4, 0 }, 9643f54a085SPoul-Henning Kamp { ".820", 5, 0 }, 9653f54a085SPoul-Henning Kamp { ".800", 6, 0 }, 9663f54a085SPoul-Henning Kamp { ".720", 7, 0 }, 9673f54a085SPoul-Henning Kamp { ".360", 8, 0 }, 9683f54a085SPoul-Henning Kamp { ".640", 9, 0 }, 9693f54a085SPoul-Henning Kamp { ".1232", 10, 0 }, 9703f54a085SPoul-Henning Kamp { 0, 0 } 9713f54a085SPoul-Henning Kamp }; 9723f54a085SPoul-Henning Kamp static void 97380909a7dSJoerg Wunsch fd_clone (void *arg, char *name, int namelen, dev_t *dev) 9743f54a085SPoul-Henning Kamp { 9753f54a085SPoul-Henning Kamp int u, d, i; 9763f54a085SPoul-Henning Kamp char *n; 9773f54a085SPoul-Henning Kamp dev_t pdev; 9783f54a085SPoul-Henning Kamp 9793f54a085SPoul-Henning Kamp if (*dev != NODEV) 9803f54a085SPoul-Henning Kamp return; 981db901281SPoul-Henning Kamp if (dev_stdclone(name, &n, "fd", &u) != 2) 9823f54a085SPoul-Henning Kamp return; 9833f54a085SPoul-Henning Kamp for (i = 0; ; i++) { 9843f54a085SPoul-Henning Kamp if (fd_suffix[i].match == NULL) 9853f54a085SPoul-Henning Kamp return; 9863f54a085SPoul-Henning Kamp if (strcmp(n, fd_suffix[i].match)) 9873f54a085SPoul-Henning Kamp continue; 9883f54a085SPoul-Henning Kamp d = fd_suffix[i].minor; 9893f54a085SPoul-Henning Kamp break; 9903f54a085SPoul-Henning Kamp } 9913f54a085SPoul-Henning Kamp if (fd_suffix[i].link == 0) { 9923f54a085SPoul-Henning Kamp *dev = make_dev(&fd_cdevsw, (u << 6) + d, 9933f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, name); 9943f54a085SPoul-Henning Kamp } else { 9953f54a085SPoul-Henning Kamp pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d); 9963f54a085SPoul-Henning Kamp *dev = make_dev_alias(pdev, name); 9973f54a085SPoul-Henning Kamp } 9983f54a085SPoul-Henning Kamp } 9993f54a085SPoul-Henning Kamp 100016e68fc6SPeter Wemm /******************************************************************/ 100116e68fc6SPeter Wemm /* 100216e68fc6SPeter Wemm * devices attached to the controller section. 100316e68fc6SPeter Wemm */ 10046182fdbdSPeter Wemm static int 10056182fdbdSPeter Wemm fd_probe(device_t dev) 10066182fdbdSPeter Wemm { 10076182fdbdSPeter Wemm int i; 10086182fdbdSPeter Wemm u_int fdt, st0, st3; 10096182fdbdSPeter Wemm struct fd_data *fd; 10106182fdbdSPeter Wemm struct fdc_data *fdc; 10116182fdbdSPeter Wemm fdsu_t fdsu; 10126182fdbdSPeter Wemm static int fd_fifo = 0; 10136182fdbdSPeter Wemm 10146182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 10156182fdbdSPeter Wemm fd = device_get_softc(dev); 10166182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 10176182fdbdSPeter Wemm 10186182fdbdSPeter Wemm bzero(fd, sizeof *fd); 10196182fdbdSPeter Wemm fd->dev = dev; 10206182fdbdSPeter Wemm fd->fdc = fdc; 10216182fdbdSPeter Wemm fd->fdsu = fdsu; 10226182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 10236182fdbdSPeter Wemm 1024a97c75b7SDoug Rabson #ifdef __i386__ 1025b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 10266182fdbdSPeter Wemm switch (fd->fdu) { 10276182fdbdSPeter Wemm case 0: 10285f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 10295f830ea2SJoerg Wunsch fdt = RTCFDT_144M; 10305f830ea2SJoerg Wunsch else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 10310722d6abSJoerg Wunsch fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 10320722d6abSJoerg Wunsch else 10330722d6abSJoerg Wunsch fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 1034b99f0a4aSAndrew Moore break; 10356182fdbdSPeter Wemm case 1: 10366182fdbdSPeter Wemm fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 1037dc5df763SJoerg Wunsch break; 1038dc5df763SJoerg Wunsch default: 10396182fdbdSPeter Wemm fdt = RTCFDT_NONE; 1040dc5df763SJoerg Wunsch break; 10416b7bd95bSJoerg Wunsch } 1042a97c75b7SDoug Rabson #else 1043a97c75b7SDoug Rabson fdt = RTCFDT_144M; /* XXX probably */ 1044a97c75b7SDoug Rabson #endif 10456182fdbdSPeter Wemm 10466182fdbdSPeter Wemm /* is there a unit? */ 10476182fdbdSPeter Wemm if (fdt == RTCFDT_NONE) 10486182fdbdSPeter Wemm return (ENXIO); 10496182fdbdSPeter Wemm 10506182fdbdSPeter Wemm /* select it */ 10516182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 10526182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 10536182fdbdSPeter Wemm 10548de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 10556182fdbdSPeter Wemm if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 1056e34c71eaSJoerg Wunsch && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 10576182fdbdSPeter Wemm && enable_fifo(fdc) == 0) { 1058b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1059b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1060d66c374fSTor Egge } 10616182fdbdSPeter Wemm fd_fifo = 1; 10626182fdbdSPeter Wemm 10636182fdbdSPeter Wemm if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 10646182fdbdSPeter Wemm && (st3 & NE7_ST3_T0)) { 1065dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 1066dc5df763SJoerg Wunsch /* seek some steps: */ 10676182fdbdSPeter Wemm fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 1068dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 10696182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1070dc5df763SJoerg Wunsch } 1071dc5df763SJoerg Wunsch 1072dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 10736182fdbdSPeter Wemm if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1074dc5df763SJoerg Wunsch /* Seek some steps... */ 10756182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1076dc5df763SJoerg Wunsch /* ...wait a moment... */ 1077dc5df763SJoerg Wunsch DELAY(300000); 1078dc5df763SJoerg Wunsch /* make ctrlr happy: */ 10796182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1080dc5df763SJoerg Wunsch } 1081dc5df763SJoerg Wunsch } 1082dc5df763SJoerg Wunsch 10836b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 10846b7bd95bSJoerg Wunsch /* 10856b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 10866b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 10876b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 10886b7bd95bSJoerg Wunsch * more than 77 steps 10896b7bd95bSJoerg Wunsch */ 1090dc5df763SJoerg Wunsch /* go back to 0: */ 10916182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 10926b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 10936b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 10945b81b6b3SRodney W. Grimes 10956b7bd95bSJoerg Wunsch /* anything responding? */ 1096dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1097dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 10986b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 10996b7bd95bSJoerg Wunsch } 1100dc5df763SJoerg Wunsch } 11016b7bd95bSJoerg Wunsch 11026182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 11033a2f7427SDavid Greenman 11043a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 11056182fdbdSPeter Wemm return (ENXIO); 11065b81b6b3SRodney W. Grimes 1107dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1108b99f0a4aSAndrew Moore fd->fdc = fdc; 1109b99f0a4aSAndrew Moore fd->fdsu = fdsu; 11103a2f7427SDavid Greenman fd->options = 0; 111102a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 111202a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 11135b81b6b3SRodney W. Grimes 1114b99f0a4aSAndrew Moore switch (fdt) { 11157ca0641bSAndrey A. Chernov case RTCFDT_12M: 11166182fdbdSPeter Wemm device_set_desc(dev, "1200-KB 5.25\" drive"); 1117b99f0a4aSAndrew Moore fd->type = FD_1200; 11187ca0641bSAndrey A. Chernov break; 11190722d6abSJoerg Wunsch case RTCFDT_144M | RTCFDT_144M_PRETENDED: 11206182fdbdSPeter Wemm device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 11210722d6abSJoerg Wunsch fdt = RTCFDT_144M; 11226182fdbdSPeter Wemm fd->type = FD_1440; 11237ca0641bSAndrey A. Chernov case RTCFDT_144M: 11246182fdbdSPeter Wemm device_set_desc(dev, "1440-KB 3.5\" drive"); 1125b99f0a4aSAndrew Moore fd->type = FD_1440; 11267ca0641bSAndrey A. Chernov break; 1127290dd077SJoerg Wunsch case RTCFDT_288M: 112886a727d9SJoerg Wunsch case RTCFDT_288M_1: 11296182fdbdSPeter Wemm device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1130290dd077SJoerg Wunsch fd->type = FD_1440; 1131290dd077SJoerg Wunsch break; 11327ca0641bSAndrey A. Chernov case RTCFDT_360K: 11336182fdbdSPeter Wemm device_set_desc(dev, "360-KB 5.25\" drive"); 1134b99f0a4aSAndrew Moore fd->type = FD_360; 11357ca0641bSAndrey A. Chernov break; 1136ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 11376182fdbdSPeter Wemm printf("720-KB 3.5\" drive"); 1138b99f0a4aSAndrew Moore fd->type = FD_720; 1139ed2fa05eSAndrey A. Chernov break; 11407ca0641bSAndrey A. Chernov default: 11416182fdbdSPeter Wemm return (ENXIO); 11425b81b6b3SRodney W. Grimes } 11436182fdbdSPeter Wemm return (0); 11446182fdbdSPeter Wemm } 11456182fdbdSPeter Wemm 11466182fdbdSPeter Wemm static int 11476182fdbdSPeter Wemm fd_attach(device_t dev) 11486182fdbdSPeter Wemm { 11496182fdbdSPeter Wemm struct fd_data *fd; 115064860614SJoerg Wunsch static int cdevsw_add_done; 11516182fdbdSPeter Wemm 115202211baeSNick Hibma if (!cdevsw_add_done) { 1153475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 115464860614SJoerg Wunsch cdevsw_add_done = 1; 115502211baeSNick Hibma } 115664860614SJoerg Wunsch fd = device_get_softc(dev); 11575f431174SJoerg Wunsch fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000); 11585f431174SJoerg Wunsch fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6, 11593f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 11606182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 1161f8ce7dd5SJoerg Wunsch device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 11622a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 11632a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 11646182fdbdSPeter Wemm return (0); 11655b81b6b3SRodney W. Grimes } 11665b81b6b3SRodney W. Grimes 11675f830ea2SJoerg Wunsch static int 11685f830ea2SJoerg Wunsch fd_detach(device_t dev) 11695f830ea2SJoerg Wunsch { 11705f830ea2SJoerg Wunsch struct fd_data *fd; 11715f830ea2SJoerg Wunsch 11725f830ea2SJoerg Wunsch fd = device_get_softc(dev); 1173e219897aSJoerg Wunsch devstat_remove_entry(&fd->device_stats); 1174e219897aSJoerg Wunsch destroy_dev(fd->masterdev); 1175e219897aSJoerg Wunsch cdevsw_remove(&fd_cdevsw); 1176e219897aSJoerg Wunsch /* XXX need to destroy cloned devs as well */ 1177e219897aSJoerg Wunsch EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag); 11785f830ea2SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 11795f830ea2SJoerg Wunsch 11805f830ea2SJoerg Wunsch return (0); 11815f830ea2SJoerg Wunsch } 11825f830ea2SJoerg Wunsch 118316e68fc6SPeter Wemm static device_method_t fd_methods[] = { 118416e68fc6SPeter Wemm /* Device interface */ 118516e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 118616e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 11875f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 118816e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 118916e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 119016e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 119116e68fc6SPeter Wemm 119216e68fc6SPeter Wemm { 0, 0 } 119316e68fc6SPeter Wemm }; 119416e68fc6SPeter Wemm 119516e68fc6SPeter Wemm static driver_t fd_driver = { 119616e68fc6SPeter Wemm "fd", 119716e68fc6SPeter Wemm fd_methods, 119816e68fc6SPeter Wemm sizeof(struct fd_data) 119916e68fc6SPeter Wemm }; 120016e68fc6SPeter Wemm 1201475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 120216e68fc6SPeter Wemm 12035b81b6b3SRodney W. Grimes /****************************************************************************/ 12045b81b6b3SRodney W. Grimes /* motor control stuff */ 12055e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 12065b81b6b3SRodney W. Grimes /****************************************************************************/ 12073a2f7427SDavid Greenman static void 12086182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 12095b81b6b3SRodney W. Grimes { 12106182fdbdSPeter Wemm int fdout = fdc->fdout; 12113a2f7427SDavid Greenman int needspecify = 0; 12123a2f7427SDavid Greenman 12133a2f7427SDavid Greenman if(turnon) { 12143a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 12153a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 12163a2f7427SDavid Greenman } else 12173a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 12183a2f7427SDavid Greenman 12195e235068SJordan K. Hubbard if(!turnon 12205e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 12215e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 12225e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 12235e235068SJordan K. Hubbard else { 12243a2f7427SDavid Greenman /* make sure controller is selected and specified */ 12253a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 12263a2f7427SDavid Greenman needspecify = 1; 12273a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 12285b81b6b3SRodney W. Grimes } 12295b81b6b3SRodney W. Grimes 1230427ccf00SDoug Rabson fdout_wr(fdc, fdout); 12316182fdbdSPeter Wemm fdc->fdout = fdout; 12323a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 12333a2f7427SDavid Greenman 12343a2f7427SDavid Greenman if (needspecify) { 1235dc8603e3SJoerg Wunsch /* 1236f8ce7dd5SJoerg Wunsch * we silently assume the command will be accepted 1237f8ce7dd5SJoerg Wunsch * after an FDC reset 1238f8ce7dd5SJoerg Wunsch * 1239f8ce7dd5SJoerg Wunsch * Steinbach's Guideline for Systems Programming: 1240f8ce7dd5SJoerg Wunsch * Never test for an error condition you don't know 1241f8ce7dd5SJoerg Wunsch * how to handle. 1242dc8603e3SJoerg Wunsch */ 12436182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1244dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1245dc5df763SJoerg Wunsch 0); 12466182fdbdSPeter Wemm if (fdc->flags & FDC_HAS_FIFO) 12476182fdbdSPeter Wemm (void) enable_fifo(fdc); 12483a2f7427SDavid Greenman } 12493a2f7427SDavid Greenman } 12503a2f7427SDavid Greenman 1251381fe1aaSGarrett Wollman static void 12526182fdbdSPeter Wemm fd_turnoff(void *xfd) 12535b81b6b3SRodney W. Grimes { 1254f5f7ba03SJordan K. Hubbard int s; 12556182fdbdSPeter Wemm fd_p fd = xfd; 1256dc16046fSJoerg Wunsch 12576182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 12588335c1b8SBruce Evans 12595f830ea2SJoerg Wunsch s = splbio(); 12608335c1b8SBruce Evans /* 12618335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 12625f830ea2SJoerg Wunsch * 12635f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 12645f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 12655f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 12665f830ea2SJoerg Wunsch * so the state machine gets re-entered. 12678335c1b8SBruce Evans */ 12686182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 12695f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 12705f830ea2SJoerg Wunsch splx(s); 12718335c1b8SBruce Evans return; 12728335c1b8SBruce Evans } 12738335c1b8SBruce Evans 12745b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 12756182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1276f5f7ba03SJordan K. Hubbard splx(s); 12775b81b6b3SRodney W. Grimes } 12785b81b6b3SRodney W. Grimes 12793a2f7427SDavid Greenman static void 12806182fdbdSPeter Wemm fd_motor_on(void *xfd) 12815b81b6b3SRodney W. Grimes { 1282f5f7ba03SJordan K. Hubbard int s; 12836182fdbdSPeter Wemm fd_p fd = xfd; 1284f5f7ba03SJordan K. Hubbard 1285f5f7ba03SJordan K. Hubbard s = splbio(); 12865b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 12875b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 12885b81b6b3SRodney W. Grimes { 12896182fdbdSPeter Wemm fdc_intr(fd->fdc); 12905b81b6b3SRodney W. Grimes } 1291f5f7ba03SJordan K. Hubbard splx(s); 12925b81b6b3SRodney W. Grimes } 12935b81b6b3SRodney W. Grimes 12943a2f7427SDavid Greenman static void 12956182fdbdSPeter Wemm fd_turnon(fd_p fd) 12965b81b6b3SRodney W. Grimes { 12975b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 12985b81b6b3SRodney W. Grimes { 12993a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 13006182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 13016182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 13025b81b6b3SRodney W. Grimes } 13035b81b6b3SRodney W. Grimes } 13045b81b6b3SRodney W. Grimes 1305381fe1aaSGarrett Wollman static void 1306dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 13075b81b6b3SRodney W. Grimes { 13083a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1309427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13103a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13113a2f7427SDavid Greenman DELAY(100); 13123a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1313427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 13143a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 13153a2f7427SDavid Greenman DELAY(100); 1316427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 13173a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 13183a2f7427SDavid Greenman 131964860614SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 13206182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1321dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1322dc5df763SJoerg Wunsch 0); 1323d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1324d66c374fSTor Egge (void) enable_fifo(fdc); 13255b81b6b3SRodney W. Grimes } 13265b81b6b3SRodney W. Grimes 13275b81b6b3SRodney W. Grimes /****************************************************************************/ 13285b81b6b3SRodney W. Grimes /* fdc in/out */ 13295b81b6b3SRodney W. Grimes /****************************************************************************/ 1330b5e8ce9fSBruce Evans static int 13316182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1332dc5df763SJoerg Wunsch { 1333dc5df763SJoerg Wunsch int i, j = 100000; 1334427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1335dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 1336dc5df763SJoerg Wunsch if (i == NE7_RQM) 13376182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1338dc5df763SJoerg Wunsch if (j <= 0) 13396182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1340d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1341427ccf00SDoug Rabson i = fddata_rd(fdc); 1342dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1343dc5df763SJoerg Wunsch *ptr = i; 1344dc5df763SJoerg Wunsch return 0; 1345d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1346427ccf00SDoug Rabson i = fddata_rd(fdc); 1347dc5df763SJoerg Wunsch if (ptr) 1348dc5df763SJoerg Wunsch *ptr = i; 1349dc5df763SJoerg Wunsch return 0; 1350d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1351dc5df763SJoerg Wunsch } 1352dc5df763SJoerg Wunsch 1353dc5df763SJoerg Wunsch int 13546182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 13555b81b6b3SRodney W. Grimes { 13563b3837dbSRodney W. Grimes int i; 13575b81b6b3SRodney W. Grimes 13583b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 13593b3837dbSRodney W. Grimes i = 100000; 1360427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0); 13616182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 13623b3837dbSRodney W. Grimes 13633b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 13643b3837dbSRodney W. Grimes i = 100000; 1365427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0); 136616b04b6aSJoerg Wunsch if (i <= 0) 13676182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 13683b3837dbSRodney W. Grimes 13693b3837dbSRodney W. Grimes /* Send the command and return */ 1370427ccf00SDoug Rabson fddata_wr(fdc, x); 13713a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 13725b81b6b3SRodney W. Grimes return (0); 13735b81b6b3SRodney W. Grimes } 13745b81b6b3SRodney W. Grimes 13755b81b6b3SRodney W. Grimes /****************************************************************************/ 13765b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 13775b81b6b3SRodney W. Grimes /****************************************************************************/ 1378381fe1aaSGarrett Wollman int 1379671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 13805b81b6b3SRodney W. Grimes { 13815b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 138220a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 13836182fdbdSPeter Wemm fd_p fd; 1384b99f0a4aSAndrew Moore fdc_p fdc; 13855b81b6b3SRodney W. Grimes 13865b81b6b3SRodney W. Grimes /* check bounds */ 13876182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1388b99f0a4aSAndrew Moore return (ENXIO); 13896182fdbdSPeter Wemm fdc = fd->fdc; 13906182fdbdSPeter Wemm if ((fdc == NULL) || (fd->type == NO_TYPE)) 1391b99f0a4aSAndrew Moore return (ENXIO); 1392b99f0a4aSAndrew Moore if (type > NUMDENS) 1393b99f0a4aSAndrew Moore return (ENXIO); 13947ca0641bSAndrey A. Chernov if (type == 0) 13956182fdbdSPeter Wemm type = fd->type; 13967ca0641bSAndrey A. Chernov else { 13973e425b96SJulian Elischer /* 13983e425b96SJulian Elischer * For each type of basic drive, make sure we are trying 13993e425b96SJulian Elischer * to open a type it can do, 14003e425b96SJulian Elischer */ 14016182fdbdSPeter Wemm if (type != fd->type) { 14026182fdbdSPeter Wemm switch (fd->type) { 14037ca0641bSAndrey A. Chernov case FD_360: 14047ca0641bSAndrey A. Chernov return (ENXIO); 1405ed2fa05eSAndrey A. Chernov case FD_720: 1406b39c878eSAndrey A. Chernov if ( type != FD_820 1407b39c878eSAndrey A. Chernov && type != FD_800 14086fb89845SKATO Takenori && type != FD_640 1409ed2fa05eSAndrey A. Chernov ) 1410ed2fa05eSAndrey A. Chernov return (ENXIO); 1411ed2fa05eSAndrey A. Chernov break; 14127ca0641bSAndrey A. Chernov case FD_1200: 1413b39c878eSAndrey A. Chernov switch (type) { 1414b39c878eSAndrey A. Chernov case FD_1480: 1415b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1416fa4700b4SAndrey A. Chernov break; 14177ca0641bSAndrey A. Chernov case FD_1440: 1418b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1419b39c878eSAndrey A. Chernov break; 14206fb89845SKATO Takenori case FD_1232: 14216fb89845SKATO Takenori break; 1422b39c878eSAndrey A. Chernov case FD_820: 1423b39c878eSAndrey A. Chernov type = FD_820in5_25; 1424b39c878eSAndrey A. Chernov break; 1425b39c878eSAndrey A. Chernov case FD_800: 1426b39c878eSAndrey A. Chernov type = FD_800in5_25; 1427b39c878eSAndrey A. Chernov break; 1428b39c878eSAndrey A. Chernov case FD_720: 1429b39c878eSAndrey A. Chernov type = FD_720in5_25; 1430b39c878eSAndrey A. Chernov break; 14316fb89845SKATO Takenori case FD_640: 14326fb89845SKATO Takenori type = FD_640in5_25; 14336fb89845SKATO Takenori break; 1434b39c878eSAndrey A. Chernov case FD_360: 1435b39c878eSAndrey A. Chernov type = FD_360in5_25; 1436b39c878eSAndrey A. Chernov break; 1437b39c878eSAndrey A. Chernov default: 1438b39c878eSAndrey A. Chernov return(ENXIO); 1439b39c878eSAndrey A. Chernov } 1440b39c878eSAndrey A. Chernov break; 1441b39c878eSAndrey A. Chernov case FD_1440: 1442b39c878eSAndrey A. Chernov if ( type != FD_1720 1443b39c878eSAndrey A. Chernov && type != FD_1480 1444ed2fa05eSAndrey A. Chernov && type != FD_1200 1445b39c878eSAndrey A. Chernov && type != FD_820 1446b39c878eSAndrey A. Chernov && type != FD_800 1447b39c878eSAndrey A. Chernov && type != FD_720 14486fb89845SKATO Takenori && type != FD_640 14497ca0641bSAndrey A. Chernov ) 1450dffff499SAndrey A. Chernov return(ENXIO); 1451fa4700b4SAndrey A. Chernov break; 14527ca0641bSAndrey A. Chernov } 14537ca0641bSAndrey A. Chernov } 1454fa4700b4SAndrey A. Chernov } 14556182fdbdSPeter Wemm fd->ft = fd_types + type - 1; 14566182fdbdSPeter Wemm fd->flags |= FD_OPEN; 14575f830ea2SJoerg Wunsch 14585b81b6b3SRodney W. Grimes return 0; 14595b81b6b3SRodney W. Grimes } 14605b81b6b3SRodney W. Grimes 1461381fe1aaSGarrett Wollman int 1462671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 14635b81b6b3SRodney W. Grimes { 14645b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 14656182fdbdSPeter Wemm struct fd_data *fd; 1466b99f0a4aSAndrew Moore 14676182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14686182fdbdSPeter Wemm fd->flags &= ~FD_OPEN; 14692995d110SJoerg Wunsch fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1470dc16046fSJoerg Wunsch 14715b81b6b3SRodney W. Grimes return (0); 14725b81b6b3SRodney W. Grimes } 14735b81b6b3SRodney W. Grimes 14743a2f7427SDavid Greenman /****************************************************************************/ 14753a2f7427SDavid Greenman /* fdstrategy */ 14763a2f7427SDavid Greenman /****************************************************************************/ 14773a2f7427SDavid Greenman void 14788177437dSPoul-Henning Kamp fdstrategy(struct bio *bp) 14793a2f7427SDavid Greenman { 1480bb6382faSJoerg Wunsch unsigned nblocks, blknum, cando; 14813a2f7427SDavid Greenman int s; 14823a2f7427SDavid Greenman fdu_t fdu; 14833a2f7427SDavid Greenman fdc_p fdc; 14843a2f7427SDavid Greenman fd_p fd; 14853a2f7427SDavid Greenman size_t fdblk; 14863a2f7427SDavid Greenman 14878177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 14886182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14896182fdbdSPeter Wemm if (fd == 0) 14906182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 14918177437dSPoul-Henning Kamp (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 14923a2f7427SDavid Greenman fdc = fd->fdc; 149369acd21dSWarner Losh if (fd->type == NO_TYPE) { 14948177437dSPoul-Henning Kamp bp->bio_error = ENXIO; 14958177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 14963b178206SWarner Losh goto bad; 149764860614SJoerg Wunsch } 1498d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 1499f664aeeeSJoerg Wunsch if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) { 15008177437dSPoul-Henning Kamp if (bp->bio_blkno < 0) { 1501dc5df763SJoerg Wunsch printf( 15026a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 15038177437dSPoul-Henning Kamp fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 15048177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15058177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15063a2f7427SDavid Greenman goto bad; 15073a2f7427SDavid Greenman } 15088177437dSPoul-Henning Kamp if ((bp->bio_bcount % fdblk) != 0) { 15098177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15108177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15113a2f7427SDavid Greenman goto bad; 15123a2f7427SDavid Greenman } 15133a2f7427SDavid Greenman } 15143a2f7427SDavid Greenman 15153a2f7427SDavid Greenman /* 15163a2f7427SDavid Greenman * Set up block calculations. 15173a2f7427SDavid Greenman */ 15188177437dSPoul-Henning Kamp if (bp->bio_blkno > 20000000) { 1519bb6382faSJoerg Wunsch /* 1520bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1521bb6382faSJoerg Wunsch * multiplication below from overflowing. 1522bb6382faSJoerg Wunsch */ 15238177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15248177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15253a2f7427SDavid Greenman goto bad; 15263a2f7427SDavid Greenman } 15278177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk; 1528bb6382faSJoerg Wunsch nblocks = fd->ft->size; 15298177437dSPoul-Henning Kamp bp->bio_resid = 0; 15308177437dSPoul-Henning Kamp if (blknum + (bp->bio_bcount / fdblk) > nblocks) { 1531bb6382faSJoerg Wunsch if (blknum <= nblocks) { 1532bb6382faSJoerg Wunsch cando = (nblocks - blknum) * fdblk; 15338177437dSPoul-Henning Kamp bp->bio_resid = bp->bio_bcount - cando; 1534bb6382faSJoerg Wunsch if (cando == 0) 1535bb6382faSJoerg Wunsch goto bad; /* not actually bad but EOF */ 1536bb6382faSJoerg Wunsch } else { 15378177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15388177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 1539bb6382faSJoerg Wunsch goto bad; 1540bb6382faSJoerg Wunsch } 1541bb6382faSJoerg Wunsch } 15428177437dSPoul-Henning Kamp bp->bio_pblkno = bp->bio_blkno; 15433a2f7427SDavid Greenman s = splbio(); 15448177437dSPoul-Henning Kamp bioqdisksort(&fdc->head, bp); 15456182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1546b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 15475f830ea2SJoerg Wunsch device_busy(fd->dev); 15486182fdbdSPeter Wemm fdstart(fdc); 15493a2f7427SDavid Greenman splx(s); 15503a2f7427SDavid Greenman return; 15513a2f7427SDavid Greenman 15523a2f7427SDavid Greenman bad: 15533a2f7427SDavid Greenman biodone(bp); 15543a2f7427SDavid Greenman } 15553a2f7427SDavid Greenman 15565b81b6b3SRodney W. Grimes /***************************************************************\ 15575b81b6b3SRodney W. Grimes * fdstart * 15585b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 15595b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 15605b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 15615b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 15625b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 15635b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 15645b81b6b3SRodney W. Grimes \***************************************************************/ 1565381fe1aaSGarrett Wollman static void 15666182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 15675b81b6b3SRodney W. Grimes { 15685b81b6b3SRodney W. Grimes int s; 15695b81b6b3SRodney W. Grimes 15705b81b6b3SRodney W. Grimes s = splbio(); 15716182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 15725b81b6b3SRodney W. Grimes { 15736182fdbdSPeter Wemm fdc_intr(fdc); 15745b81b6b3SRodney W. Grimes } 15755b81b6b3SRodney W. Grimes splx(s); 15765b81b6b3SRodney W. Grimes } 15775b81b6b3SRodney W. Grimes 1578381fe1aaSGarrett Wollman static void 15796182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 15805b81b6b3SRodney W. Grimes { 15815c1a1eaeSBruce Evans fdc_p fdc; 1582f5f7ba03SJordan K. Hubbard int s; 15835b81b6b3SRodney W. Grimes 15846182fdbdSPeter Wemm fdc = xfdc; 15855c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 15865b81b6b3SRodney W. Grimes 15873a2f7427SDavid Greenman /* 15883a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 15893a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 15903a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 15913a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 15923a2f7427SDavid Greenman * Many thanks, Big Blue! 15935c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 15945c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 15955c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 15965c1a1eaeSBruce Evans * will reset the FDC and retry once. 15973a2f7427SDavid Greenman */ 15983a2f7427SDavid Greenman s = splbio(); 15995c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 16005c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 16015c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 16026182fdbdSPeter Wemm fdc_intr(fdc); 1603f5f7ba03SJordan K. Hubbard splx(s); 16045b81b6b3SRodney W. Grimes } 16055b81b6b3SRodney W. Grimes 16065b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1607381fe1aaSGarrett Wollman static void 16086182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 16095b81b6b3SRodney W. Grimes { 16105b81b6b3SRodney W. Grimes int s; 16113a2f7427SDavid Greenman 16125b81b6b3SRodney W. Grimes s = splbio(); 16136182fdbdSPeter Wemm fdc_intr(xfdc); 16145b81b6b3SRodney W. Grimes splx(s); 16155b81b6b3SRodney W. Grimes } 16165b81b6b3SRodney W. Grimes 16175b81b6b3SRodney W. Grimes /***********************************************************************\ 161864860614SJoerg Wunsch * fdc_intr * 16195b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 16205b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 16215b81b6b3SRodney W. Grimes \***********************************************************************/ 1622fe310de8SBruce Evans static void 16236182fdbdSPeter Wemm fdc_intr(void *xfdc) 16245b81b6b3SRodney W. Grimes { 16256182fdbdSPeter Wemm fdc_p fdc = xfdc; 16266182fdbdSPeter Wemm while(fdstate(fdc)) 1627381fe1aaSGarrett Wollman ; 16285b81b6b3SRodney W. Grimes } 16295b81b6b3SRodney W. Grimes 163069acd21dSWarner Losh /* 163169acd21dSWarner Losh * magic pseudo-DMA initialization for YE FDC. Sets count and 163269acd21dSWarner Losh * direction 163369acd21dSWarner Losh */ 16343b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 16353b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 16363b178206SWarner Losh ((cnt)-1) & 0xff); \ 16373b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 16383b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 163969acd21dSWarner Losh 164069acd21dSWarner Losh /* 164169acd21dSWarner Losh * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 164269acd21dSWarner Losh */ 16434c34deeeSJoerg Wunsch static int 16444c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 164569acd21dSWarner Losh { 164669acd21dSWarner Losh u_char *cptr = (u_char *)addr; 164769acd21dSWarner Losh 164821144e3bSPoul-Henning Kamp if (flags == BIO_READ) { 164969acd21dSWarner Losh if (fdc->state != PIOREAD) { 165069acd21dSWarner Losh fdc->state = PIOREAD; 165169acd21dSWarner Losh return(0); 165264860614SJoerg Wunsch } 16533b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 16543b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16553b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 165669acd21dSWarner Losh } else { 16573b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16583b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 16593b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 166064860614SJoerg Wunsch } 166169acd21dSWarner Losh return(1); 166269acd21dSWarner Losh } 166369acd21dSWarner Losh 16645b81b6b3SRodney W. Grimes /***********************************************************************\ 16655b81b6b3SRodney W. Grimes * The controller state machine. * 16665b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 16675b81b6b3SRodney W. Grimes \***********************************************************************/ 16683a2f7427SDavid Greenman static int 16696182fdbdSPeter Wemm fdstate(fdc_p fdc) 16705b81b6b3SRodney W. Grimes { 1671250300ebSJoerg Wunsch int read, format, rdsectid, head, i, sec = 0, sectrac, st0, cyl, st3, idf; 1672bb6382faSJoerg Wunsch unsigned blknum = 0, b_cylinder = 0; 167364860614SJoerg Wunsch struct fdc_readid *idp; 16745b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 16755b81b6b3SRodney W. Grimes fd_p fd; 16768177437dSPoul-Henning Kamp register struct bio *bp; 1677b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 16783a2f7427SDavid Greenman size_t fdblk; 16795b81b6b3SRodney W. Grimes 1680e93e63cbSBruce Evans bp = fdc->bp; 1681e93e63cbSBruce Evans if (bp == NULL) { 16828177437dSPoul-Henning Kamp bp = bioq_first(&fdc->head); 1683e93e63cbSBruce Evans if (bp != NULL) { 16848177437dSPoul-Henning Kamp bioq_remove(&fdc->head, bp); 1685e93e63cbSBruce Evans fdc->bp = bp; 1686e93e63cbSBruce Evans } 1687e93e63cbSBruce Evans } 1688e93e63cbSBruce Evans if (bp == NULL) { 16895b81b6b3SRodney W. Grimes /***********************************************\ 16905b81b6b3SRodney W. Grimes * nothing left for this controller to do * 16915b81b6b3SRodney W. Grimes * Force into the IDLE state, * 16925b81b6b3SRodney W. Grimes \***********************************************/ 16935b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 16946182fdbdSPeter Wemm if (fdc->fd) { 1695b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1696b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 16975b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 16985b81b6b3SRodney W. Grimes fdc->fdu = -1; 16995b81b6b3SRodney W. Grimes } 17006182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 17015b81b6b3SRodney W. Grimes return (0); 17025b81b6b3SRodney W. Grimes } 17038177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 17046182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 17053a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1706b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 1707b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 17088177437dSPoul-Henning Kamp read = bp->bio_cmd == BIO_READ; 170956a23089SPoul-Henning Kamp if (read) 171056a23089SPoul-Henning Kamp idf = ISADMA_READ; 171156a23089SPoul-Henning Kamp else 171256a23089SPoul-Henning Kamp idf = ISADMA_WRITE; 171364860614SJoerg Wunsch format = bp->bio_cmd == BIO_FORMAT; 1714f664aeeeSJoerg Wunsch rdsectid = bp->bio_cmd == BIO_RDSECTID; 1715bb6382faSJoerg Wunsch if (format) { 17168177437dSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data; 1717bb6382faSJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1718bb6382faSJoerg Wunsch - (char *)finfo; 1719bb6382faSJoerg Wunsch } 1720bb6382faSJoerg Wunsch if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 17218177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk + 1722bb6382faSJoerg Wunsch fd->skip/fdblk; 1723bb6382faSJoerg Wunsch b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1724bb6382faSJoerg Wunsch } 17255b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 17265b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 17275b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 17286182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 17296182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 17305b81b6b3SRodney W. Grimes switch (fdc->state) 17315b81b6b3SRodney W. Grimes { 17325b81b6b3SRodney W. Grimes case DEVIDLE: 17335b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 17345b81b6b3SRodney W. Grimes fdc->retry = 0; 17355b81b6b3SRodney W. Grimes fd->skip = 0; 17365b81b6b3SRodney W. Grimes fdc->fd = fd; 17375b81b6b3SRodney W. Grimes fdc->fdu = fdu; 17385f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 17393a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 17405b81b6b3SRodney W. Grimes /*******************************************************\ 17415b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 1742dc733423SDag-Erling Smørgrav * it will start up in its own good time * 17435b81b6b3SRodney W. Grimes \*******************************************************/ 17446182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 17455b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17465b81b6b3SRodney W. Grimes return (0); /* come back later */ 17475b81b6b3SRodney W. Grimes } 17485b81b6b3SRodney W. Grimes /*******************************************************\ 17495b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 17505b81b6b3SRodney W. Grimes \*******************************************************/ 17515b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 17525b81b6b3SRodney W. Grimes { 17535b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17546182fdbdSPeter Wemm fd_turnon(fd); 17555b81b6b3SRodney W. Grimes return (0); 17565b81b6b3SRodney W. Grimes } 17575b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 17585b81b6b3SRodney W. Grimes { 17596182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 17605b81b6b3SRodney W. Grimes } 17615c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 17625c1a1eaeSBruce Evans fdc->state = RESETCTLR; 17635c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 17645c1a1eaeSBruce Evans } else 17655e235068SJordan K. Hubbard fdc->state = DOSEEK; 17665b81b6b3SRodney W. Grimes break; 17675b81b6b3SRodney W. Grimes case DOSEEK: 1768bb6382faSJoerg Wunsch if (b_cylinder == (unsigned)fd->track) 17695b81b6b3SRodney W. Grimes { 17705b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17715b81b6b3SRodney W. Grimes break; 17725b81b6b3SRodney W. Grimes } 17736182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1774bb6382faSJoerg Wunsch fd->fdsu, b_cylinder * fd->ft->steptrac, 1775dc5df763SJoerg Wunsch 0)) 1776dc8603e3SJoerg Wunsch { 1777dc8603e3SJoerg Wunsch /* 1778dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1779dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1780dc8603e3SJoerg Wunsch */ 1781dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 17826182fdbdSPeter Wemm return(retrier(fdc)); 1783dc8603e3SJoerg Wunsch } 1784dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 17855b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 17865b81b6b3SRodney W. Grimes return(0); /* will return later */ 17875b81b6b3SRodney W. Grimes case SEEKWAIT: 17885b81b6b3SRodney W. Grimes /* allow heads to settle */ 17896182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 17905b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17915b81b6b3SRodney W. Grimes return(0); /* will return later */ 17925b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 17935b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 17946182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 1795bb6382faSJoerg Wunsch int descyl = b_cylinder * fd->ft->steptrac; 17963a2f7427SDavid Greenman do { 17973a2f7427SDavid Greenman /* 1798dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1799dc5df763SJoerg Wunsch * which cannot really happen since the 1800dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1801dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1802dc5df763SJoerg Wunsch * line, so do one of the following: 1803dc5df763SJoerg Wunsch * 1804dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1805dc5df763SJoerg Wunsch * known to not go stuck after responding 1806dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1807dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1808dc5df763SJoerg Wunsch * real interrupt condition. 1809dc5df763SJoerg Wunsch * 1810dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1811dc5df763SJoerg Wunsch * up immediately. The controller will 1812dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1813dc5df763SJoerg Wunsch * conditions right after reset (for the 1814dc5df763SJoerg Wunsch * corresponding four drives), so this is 1815dc5df763SJoerg Wunsch * our only chance to get notice that it 1816dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 18173a2f7427SDavid Greenman */ 1818dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1819dc5df763SJoerg Wunsch == FD_NOT_VALID) 1820dc5df763SJoerg Wunsch return 0; 1821dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1822dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1823dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 18243a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1825dc5df763SJoerg Wunsch 18266182fdbdSPeter Wemm if (0 == descyl) { 1827dc5df763SJoerg Wunsch int failed = 0; 18283a2f7427SDavid Greenman /* 18293a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 18303a2f7427SDavid Greenman * really there 18313a2f7427SDavid Greenman */ 1832dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1833dc5df763SJoerg Wunsch failed = 1; 18343a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 18353a2f7427SDavid Greenman printf( 18363a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 18373a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1838dc5df763SJoerg Wunsch failed = 1; 1839dc5df763SJoerg Wunsch } 1840dc5df763SJoerg Wunsch 18416182fdbdSPeter Wemm if (failed) { 18423a2f7427SDavid Greenman if(fdc->retry < 3) 18433a2f7427SDavid Greenman fdc->retry = 3; 18446182fdbdSPeter Wemm return (retrier(fdc)); 18453a2f7427SDavid Greenman } 18463a2f7427SDavid Greenman } 1847dc5df763SJoerg Wunsch 18486182fdbdSPeter Wemm if (cyl != descyl) { 18493a2f7427SDavid Greenman printf( 18503a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 18512d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 1852e5d7d243SBruce Evans if (fdc->retry < 3) 1853e5d7d243SBruce Evans fdc->retry = 3; 18546182fdbdSPeter Wemm return (retrier(fdc)); 18555b81b6b3SRodney W. Grimes } 18565b81b6b3SRodney W. Grimes } 18575b81b6b3SRodney W. Grimes 1858bb6382faSJoerg Wunsch fd->track = b_cylinder; 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); 18625b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 18635b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 18645b81b6b3SRodney W. Grimes head = sec / sectrac; 18655b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 18663a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 18673a2f7427SDavid Greenman 1868250300ebSJoerg Wunsch if(format || !(read || rdsectid)) 18693a2f7427SDavid Greenman { 18703a2f7427SDavid Greenman /* make sure the drive is writable */ 1871dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1872dc8603e3SJoerg Wunsch { 1873dc8603e3SJoerg Wunsch /* stuck controller? */ 18745f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 187556a23089SPoul-Henning Kamp isa_dmadone(idf, 18768177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 18778177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 18785c1a1eaeSBruce Evans fdc->dmachan); 1879dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 18806182fdbdSPeter Wemm return (retrier(fdc)); 1881dc8603e3SJoerg Wunsch } 18823a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 18833a2f7427SDavid Greenman { 18843a2f7427SDavid Greenman /* 18853a2f7427SDavid Greenman * XXX YES! this is ugly. 18863a2f7427SDavid Greenman * in order to force the current operation 18873a2f7427SDavid Greenman * to fail, we will have to fake an FDC 18883a2f7427SDavid Greenman * error - all error handling is done 18893a2f7427SDavid Greenman * by the retrier() 18903a2f7427SDavid Greenman */ 18913a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 18923a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 18933a2f7427SDavid Greenman fdc->status[2] = 0; 18943a2f7427SDavid Greenman fdc->status[3] = fd->track; 18953a2f7427SDavid Greenman fdc->status[4] = head; 18963a2f7427SDavid Greenman fdc->status[5] = sec; 18973a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 18983a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 18993a2f7427SDavid Greenman return (1); 19003a2f7427SDavid Greenman } 19013a2f7427SDavid Greenman } 19025b81b6b3SRodney W. Grimes 19036182fdbdSPeter Wemm if (format) { 19045f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 19055f830ea2SJoerg Wunsch /* 19065f830ea2SJoerg Wunsch * This seems to be necessary for 19075f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 19085f830ea2SJoerg Wunsch * it, we end up filling the sector ID 19095f830ea2SJoerg Wunsch * fields of the newly formatted track 19105f830ea2SJoerg Wunsch * entirely with garbage, causing 19115f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 19125f830ea2SJoerg Wunsch * the place when trying to read them 19135f830ea2SJoerg Wunsch * back. 19145f830ea2SJoerg Wunsch * 19155f830ea2SJoerg Wunsch * Umpf. 19165f830ea2SJoerg Wunsch */ 19178177437dSPoul-Henning Kamp SET_BCDR(fdc, 1, bp->bio_bcount, 0); 19185f830ea2SJoerg Wunsch 19198177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19208177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 19218177437dSPoul-Henning Kamp bp->bio_bcount); 19225f830ea2SJoerg Wunsch 19235f830ea2SJoerg Wunsch } 1924b39c878eSAndrey A. Chernov /* formatting */ 19256182fdbdSPeter Wemm if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1926dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1927dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1928dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 19296182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 1930dc8603e3SJoerg Wunsch /* controller fell over */ 19315f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 193256a23089SPoul-Henning Kamp isa_dmadone(idf, 19338177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19348177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19355c1a1eaeSBruce Evans fdc->dmachan); 1936dc8603e3SJoerg Wunsch fdc->retry = 6; 19376182fdbdSPeter Wemm return (retrier(fdc)); 1938dc8603e3SJoerg Wunsch } 1939250300ebSJoerg Wunsch } else if (rdsectid) { 1940250300ebSJoerg Wunsch if (fd_cmd(fdc, 2, NE7CMD_READID, head << 2 | fdu, 0)) { 1941250300ebSJoerg Wunsch /* controller jamming */ 1942250300ebSJoerg Wunsch fdc->retry = 6; 1943250300ebSJoerg Wunsch return (retrier(fdc)); 1944250300ebSJoerg Wunsch } 19456182fdbdSPeter Wemm } else { 1946250300ebSJoerg Wunsch /* read or write operation */ 19473b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 194869acd21dSWarner Losh /* 194969acd21dSWarner Losh * this seems to be necessary even when 195069acd21dSWarner Losh * reading data 195169acd21dSWarner Losh */ 19523b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 195369acd21dSWarner Losh 195469acd21dSWarner Losh /* 195569acd21dSWarner Losh * perform the write pseudo-DMA before 195669acd21dSWarner Losh * the WRITE command is sent 195769acd21dSWarner Losh */ 195869acd21dSWarner Losh if (!read) 19598177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19608177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 196169acd21dSWarner Losh fdblk); 196269acd21dSWarner Losh } 19636182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 1964dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1965dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1966dc5df763SJoerg Wunsch fd->track, /* track */ 1967dc5df763SJoerg Wunsch head, 1968dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1969dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1970dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1971dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1972dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 19736182fdbdSPeter Wemm 0)) { 1974dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 19755f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 197656a23089SPoul-Henning Kamp isa_dmadone(idf, 19778177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19788177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19795c1a1eaeSBruce Evans fdc->dmachan); 1980dc8603e3SJoerg Wunsch fdc->retry = 6; 19816182fdbdSPeter Wemm return (retrier(fdc)); 19825b81b6b3SRodney W. Grimes } 1983b39c878eSAndrey A. Chernov } 1984250300ebSJoerg Wunsch if (!rdsectid && (fdc->flags & FDC_NODMA)) 198569acd21dSWarner Losh /* 198669acd21dSWarner Losh * if this is a read, then simply await interrupt 198769acd21dSWarner Losh * before performing PIO 198869acd21dSWarner Losh */ 19898177437dSPoul-Henning Kamp if (read && !fdcpio(fdc,bp->bio_cmd, 19908177437dSPoul-Henning Kamp bp->bio_data+fd->skip,fdblk)) { 19913b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 199269acd21dSWarner Losh return(0); /* will return later */ 199364860614SJoerg Wunsch } 199469acd21dSWarner Losh 199569acd21dSWarner Losh /* 199669acd21dSWarner Losh * write (or format) operation will fall through and 199769acd21dSWarner Losh * await completion interrupt 199869acd21dSWarner Losh */ 19995b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 20006182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 20015b81b6b3SRodney W. Grimes return (0); /* will return later */ 200269acd21dSWarner Losh case PIOREAD: 200369acd21dSWarner Losh /* 200469acd21dSWarner Losh * actually perform the PIO read. The IOCOMPLETE case 200569acd21dSWarner Losh * removes the timeout for us. 200669acd21dSWarner Losh */ 20078177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 200869acd21dSWarner Losh fdc->state = IOCOMPLETE; 200969acd21dSWarner Losh /* FALLTHROUGH */ 20105b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 20116182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 2012dc5df763SJoerg Wunsch 201364860614SJoerg Wunsch if (fd_read_status(fdc)) { 2014250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 20158177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20168177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 20175c1a1eaeSBruce Evans fdc->dmachan); 2018dc5df763SJoerg Wunsch if (fdc->retry < 6) 2019dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 20206182fdbdSPeter Wemm return (retrier(fdc)); 20215b81b6b3SRodney W. Grimes } 2022dc5df763SJoerg Wunsch 20233a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 2024dc5df763SJoerg Wunsch 20253a2f7427SDavid Greenman /* FALLTHROUGH */ 2026dc5df763SJoerg Wunsch 20273a2f7427SDavid Greenman case IOTIMEDOUT: 2028250300ebSJoerg Wunsch if (!rdsectid && !(fdc->flags & FDC_NODMA)) 20298177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20308177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 20316182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 20323a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20333a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 2034b39c878eSAndrey A. Chernov /* 20353a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 20363a2f7427SDavid Greenman * and didn't release it in time for the 20373a2f7427SDavid Greenman * next FDC transfer. 20383a2f7427SDavid Greenman * Just restart it, don't increment retry 20393a2f7427SDavid Greenman * count. (vak) 2040b39c878eSAndrey A. Chernov */ 2041b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2042b39c878eSAndrey A. Chernov return (1); 2043b39c878eSAndrey A. Chernov } 20443a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 20453a2f7427SDavid Greenman && fdc->retry < 6) 20463a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 20473a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20483a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 20493a2f7427SDavid Greenman && fdc->retry < 3) 20503a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 20516182fdbdSPeter Wemm return (retrier(fdc)); 20525b81b6b3SRodney W. Grimes } 20535b81b6b3SRodney W. Grimes /* All OK */ 2054250300ebSJoerg Wunsch if (rdsectid) { 2055250300ebSJoerg Wunsch /* copy out ID field contents */ 205664860614SJoerg Wunsch idp = (struct fdc_readid *)bp->bio_data; 2057250300ebSJoerg Wunsch idp->cyl = fdc->status[3]; 2058250300ebSJoerg Wunsch idp->head = fdc->status[4]; 2059250300ebSJoerg Wunsch idp->sec = fdc->status[5]; 2060250300ebSJoerg Wunsch idp->secshift = fdc->status[6]; 2061250300ebSJoerg Wunsch } 20623a2f7427SDavid Greenman fd->skip += fdblk; 2063250300ebSJoerg Wunsch if (!rdsectid && !format && fd->skip < bp->bio_bcount - bp->bio_resid) { 20645b81b6b3SRodney W. Grimes /* set up next transfer */ 20655b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20666182fdbdSPeter Wemm } else { 20675b81b6b3SRodney W. Grimes /* ALL DONE */ 20685b81b6b3SRodney W. Grimes fd->skip = 0; 2069e93e63cbSBruce Evans fdc->bp = NULL; 20705f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2071a468031cSPoul-Henning Kamp biofinish(bp, &fd->device_stats, 0); 20725b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20735b81b6b3SRodney W. Grimes fdc->fdu = -1; 20745b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 20755b81b6b3SRodney W. Grimes } 20765b81b6b3SRodney W. Grimes return (1); 20775b81b6b3SRodney W. Grimes case RESETCTLR: 20783a2f7427SDavid Greenman fdc_reset(fdc); 20795b81b6b3SRodney W. Grimes fdc->retry++; 20805c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 20815c1a1eaeSBruce Evans return (0); 20825c1a1eaeSBruce Evans case RESETCOMPLETE: 20835c1a1eaeSBruce Evans /* 20845c1a1eaeSBruce Evans * Discard all the results from the reset so that they 20855c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 20865c1a1eaeSBruce Evans */ 20870e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 20880e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 20895c1a1eaeSBruce Evans fdc->state = STARTRECAL; 20905c1a1eaeSBruce Evans /* Fall through. */ 20915c1a1eaeSBruce Evans case STARTRECAL: 20926182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2093dc8603e3SJoerg Wunsch /* arrgl */ 2094dc8603e3SJoerg Wunsch fdc->retry = 6; 20956182fdbdSPeter Wemm return (retrier(fdc)); 2096dc8603e3SJoerg Wunsch } 20975b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 20985b81b6b3SRodney W. Grimes return (0); /* will return later */ 20995b81b6b3SRodney W. Grimes case RECALWAIT: 21005b81b6b3SRodney W. Grimes /* allow heads to settle */ 21016182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 21025b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 21035b81b6b3SRodney W. Grimes return (0); /* will return later */ 21045b81b6b3SRodney W. Grimes case RECALCOMPLETE: 21053a2f7427SDavid Greenman do { 21063a2f7427SDavid Greenman /* 2107dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 21083a2f7427SDavid Greenman */ 2109dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2110dc5df763SJoerg Wunsch return 0; 2111dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2112dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2113dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 21143a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 21153a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 21165b81b6b3SRodney W. Grimes { 2117dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2118dc8603e3SJoerg Wunsch /* 2119dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 2120dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2121dc8603e3SJoerg Wunsch * since people used to complain much about 2122dc8603e3SJoerg Wunsch * the failure message, try not logging 2123dc8603e3SJoerg Wunsch * this one if it seems to be the first 2124dc8603e3SJoerg Wunsch * time in a line 2125dc8603e3SJoerg Wunsch */ 2126dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2127dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 21283a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 21296182fdbdSPeter Wemm return (retrier(fdc)); 21305b81b6b3SRodney W. Grimes } 21315b81b6b3SRodney W. Grimes fd->track = 0; 21325b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 21335b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 21345b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21355b81b6b3SRodney W. Grimes case MOTORWAIT: 21365b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 21375b81b6b3SRodney W. Grimes { 21385b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 21395b81b6b3SRodney W. Grimes } 21405c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 21415c1a1eaeSBruce Evans fdc->state = RESETCTLR; 21425c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 21435c1a1eaeSBruce Evans } else { 21445e235068SJordan K. Hubbard /* 21455c1a1eaeSBruce Evans * If all motors were off, then the controller was 21465c1a1eaeSBruce Evans * reset, so it has lost track of the current 21475c1a1eaeSBruce Evans * cylinder. Recalibrate to handle this case. 2148f86e4077SBruce Evans * But first, discard the results of the reset. 21495e235068SJordan K. Hubbard */ 2150f86e4077SBruce Evans fdc->state = RESETCOMPLETE; 21515c1a1eaeSBruce Evans } 21525b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21535b81b6b3SRodney W. Grimes default: 2154b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 215564860614SJoerg Wunsch if (fd_read_status(fdc) == 0) 2156a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 21575b81b6b3SRodney W. Grimes fdc->status[0], 21585b81b6b3SRodney W. Grimes fdc->status[1], 21595b81b6b3SRodney W. Grimes fdc->status[2], 21605b81b6b3SRodney W. Grimes fdc->status[3], 21615b81b6b3SRodney W. Grimes fdc->status[4], 21625b81b6b3SRodney W. Grimes fdc->status[5], 21635b81b6b3SRodney W. Grimes fdc->status[6] ); 21643a2f7427SDavid Greenman else 2165dac0f2dbSJoerg Wunsch printf("No status available "); 2166dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2167dac0f2dbSJoerg Wunsch { 2168dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 21695b81b6b3SRodney W. Grimes return (0); 21705b81b6b3SRodney W. Grimes } 2171dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2172dac0f2dbSJoerg Wunsch return (0); 2173dac0f2dbSJoerg Wunsch } 2174dac0f2dbSJoerg Wunsch /*XXX confusing: some branches return immediately, others end up here*/ 21755b81b6b3SRodney W. Grimes return (1); /* Come back immediatly to new state */ 21765b81b6b3SRodney W. Grimes } 21775b81b6b3SRodney W. Grimes 2178aaf08d94SGarrett Wollman static int 21796182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 21805b81b6b3SRodney W. Grimes { 21818177437dSPoul-Henning Kamp struct bio *bp; 21826182fdbdSPeter Wemm struct fd_data *fd; 21836182fdbdSPeter Wemm int fdu; 21845b81b6b3SRodney W. Grimes 2185e93e63cbSBruce Evans bp = fdc->bp; 21865b81b6b3SRodney W. Grimes 21876182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 21888177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 21896182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 21906182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 21913a2f7427SDavid Greenman goto fail; 21926182fdbdSPeter Wemm 21936182fdbdSPeter Wemm switch (fdc->retry) { 21945b81b6b3SRodney W. Grimes case 0: case 1: case 2: 21955b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 21965b81b6b3SRodney W. Grimes break; 21975b81b6b3SRodney W. Grimes case 3: case 4: case 5: 21985b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 21995b81b6b3SRodney W. Grimes break; 22005b81b6b3SRodney W. Grimes case 6: 22015b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 22025b81b6b3SRodney W. Grimes break; 22035b81b6b3SRodney W. Grimes case 7: 22045b81b6b3SRodney W. Grimes break; 22055b81b6b3SRodney W. Grimes default: 22063a2f7427SDavid Greenman fail: 22075b81b6b3SRodney W. Grimes { 22082995d110SJoerg Wunsch int printerror = (fd->options & FDOPT_NOERRLOG) == 0; 22092995d110SJoerg Wunsch 22102995d110SJoerg Wunsch if (printerror) 221164860614SJoerg Wunsch diskerr(bp, "hard error", 221264860614SJoerg Wunsch fdc->fd->skip / DEV_BSIZE, 22133a2f7427SDavid Greenman (struct disklabel *)NULL); 22142995d110SJoerg Wunsch if (printerror) { 2215dc5df763SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) 2216dc5df763SJoerg Wunsch { 2217dc5df763SJoerg Wunsch printf( 2218a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2219dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2220dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2221dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2222dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2223dc5df763SJoerg Wunsch fdc->status[5]); 2224dc5df763SJoerg Wunsch } 2225dc5df763SJoerg Wunsch else 2226dc5df763SJoerg Wunsch printf(" (No status)\n"); 22275b81b6b3SRodney W. Grimes } 22282995d110SJoerg Wunsch } 22292995d110SJoerg Wunsch if ((fd->options & FDOPT_NOERROR) == 0) { 22308177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22318177437dSPoul-Henning Kamp bp->bio_error = EIO; 22328177437dSPoul-Henning Kamp bp->bio_resid += bp->bio_bcount - fdc->fd->skip; 22332995d110SJoerg Wunsch } 2234e93e63cbSBruce Evans fdc->bp = NULL; 22355b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 22365f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2237a468031cSPoul-Henning Kamp biofinish(bp, &fdc->fd->device_stats, 0); 223892ed385aSRodney W. Grimes fdc->state = FINDWORK; 22395c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 22405b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 22415b81b6b3SRodney W. Grimes fdc->fdu = -1; 224292ed385aSRodney W. Grimes return (1); 22435b81b6b3SRodney W. Grimes } 22445b81b6b3SRodney W. Grimes fdc->retry++; 22455b81b6b3SRodney W. Grimes return (1); 22465b81b6b3SRodney W. Grimes } 22475b81b6b3SRodney W. Grimes 22481fdb6e6cSPoul-Henning Kamp static void 22491fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp) 22501fdb6e6cSPoul-Henning Kamp { 22511fdb6e6cSPoul-Henning Kamp wakeup(bp); 22521fdb6e6cSPoul-Henning Kamp } 22531fdb6e6cSPoul-Henning Kamp 2254b39c878eSAndrey A. Chernov static int 2255f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data) 2256b39c878eSAndrey A. Chernov { 2257b39c878eSAndrey A. Chernov fdu_t fdu; 2258b39c878eSAndrey A. Chernov fd_p fd; 22591fdb6e6cSPoul-Henning Kamp struct bio *bp; 2260f664aeeeSJoerg Wunsch struct fd_formb *finfo; 2261f664aeeeSJoerg Wunsch struct fdc_readid *idfield; 22623a2f7427SDavid Greenman size_t fdblk; 2263b39c878eSAndrey A. Chernov 2264b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 22656182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22663a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2267f664aeeeSJoerg Wunsch finfo = (struct fd_formb *)data; 2268f664aeeeSJoerg Wunsch idfield = (struct fdc_readid *)data; 2269b39c878eSAndrey A. Chernov 2270f664aeeeSJoerg Wunsch bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO | M_NOWAIT); 2271b39c878eSAndrey A. Chernov if (bp == 0) 2272f664aeeeSJoerg Wunsch return (ENOMEM); 2273b39c878eSAndrey A. Chernov 2274b39c878eSAndrey A. Chernov /* 2275f664aeeeSJoerg Wunsch * Set up a bio request for fdstrategy(). bio_blkno is faked 2276f664aeeeSJoerg Wunsch * so that fdstrategy() will seek to the the requested 2277f664aeeeSJoerg Wunsch * cylinder, and use the desired head. Since we are not 2278f664aeeeSJoerg Wunsch * interested in bioqdisksort() munging with our faked bio 2279f664aeeeSJoerg Wunsch * request, we mark it as being an ordered request. 2280b39c878eSAndrey A. Chernov */ 2281f664aeeeSJoerg Wunsch bp->bio_cmd = cmd; 2282f664aeeeSJoerg Wunsch if (cmd == BIO_FORMAT) { 2283f664aeeeSJoerg Wunsch bp->bio_blkno = 2284f664aeeeSJoerg Wunsch (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2285f664aeeeSJoerg Wunsch finfo->head * fd->ft->sectrac) * 2286f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2287f664aeeeSJoerg Wunsch bp->bio_bcount = sizeof(struct fd_idfield_data) * 2288f664aeeeSJoerg Wunsch finfo->fd_formb_nsecs; 2289f664aeeeSJoerg Wunsch } else if (cmd == BIO_RDSECTID) { 2290f664aeeeSJoerg Wunsch bp->bio_blkno = 2291f664aeeeSJoerg Wunsch (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2292f664aeeeSJoerg Wunsch idfield->head * fd->ft->sectrac) * 2293f664aeeeSJoerg Wunsch fdblk / DEV_BSIZE; 2294250300ebSJoerg Wunsch bp->bio_bcount = sizeof(struct fdc_readid); 2295f664aeeeSJoerg Wunsch } else 2296f664aeeeSJoerg Wunsch panic("wrong cmd in fdmisccmd()"); 2297f664aeeeSJoerg Wunsch bp->bio_data = data; 2298250300ebSJoerg Wunsch bp->bio_dev = dev; 2299250300ebSJoerg Wunsch bp->bio_done = fdbiodone; 2300f664aeeeSJoerg Wunsch bp->bio_flags = BIO_ORDERED; 2301250300ebSJoerg Wunsch 2302f5f7ba03SJordan K. Hubbard /* 2303f664aeeeSJoerg Wunsch * Now run the cmd. The wait loop is a version of bufwait() 2304f664aeeeSJoerg Wunsch * adapted for struct bio instead of struct buf and 2305f664aeeeSJoerg Wunsch * specialized for the current context. 2306f5f7ba03SJordan K. Hubbard */ 2307f664aeeeSJoerg Wunsch fdstrategy(bp); 2308f664aeeeSJoerg Wunsch while((bp->bio_flags & BIO_DONE) == 0) 2309f664aeeeSJoerg Wunsch tsleep(bp, PRIBIO, "fdcmd", 0); 2310f664aeeeSJoerg Wunsch 2311f664aeeeSJoerg Wunsch free(bp, M_TEMP); 2312f664aeeeSJoerg Wunsch return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0); 2313f664aeeeSJoerg Wunsch } 23145b81b6b3SRodney W. Grimes 23153e425b96SJulian Elischer static int 23164c34deeeSJoerg Wunsch fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 2317f5f7ba03SJordan K. Hubbard { 2318f664aeeeSJoerg Wunsch fdu_t fdu; 2319f664aeeeSJoerg Wunsch fd_p fd; 2320f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2321f5f7ba03SJordan K. Hubbard struct disklabel *dl; 23222995d110SJoerg Wunsch struct fdc_status *fsp; 2323250300ebSJoerg Wunsch struct fdc_readid *rid; 2324f664aeeeSJoerg Wunsch char *buffer; 2325f664aeeeSJoerg Wunsch size_t fdblk; 23263a2f7427SDavid Greenman int error = 0; 2327f5f7ba03SJordan K. Hubbard 2328f664aeeeSJoerg Wunsch fdu = FDUNIT(minor(dev)); 2329f664aeeeSJoerg Wunsch fd = devclass_get_softc(fd_devclass, fdu); 23303a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2331f5f7ba03SJordan K. Hubbard 23326182fdbdSPeter Wemm switch (cmd) { 2333f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2334f664aeeeSJoerg Wunsch buffer = malloc(DEV_BSIZE, M_TEMP, M_ZERO | M_NOWAIT); 2335f664aeeeSJoerg Wunsch if (buffer == 0) { 2336f664aeeeSJoerg Wunsch error = ENOMEM; 2337f664aeeeSJoerg Wunsch break; 2338f664aeeeSJoerg Wunsch } 2339f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 23403a2f7427SDavid Greenman dl->d_secsize = fdblk; 23416182fdbdSPeter Wemm fdt = fd->ft; 2342f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 2343f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 2344f5f7ba03SJordan K. Hubbard 234549ff4debSPoul-Henning Kamp if (readdisklabel(dkmodpart(dev, RAW_PART), dl) 2346191e1a59SBruce Evans == NULL) 2347f5f7ba03SJordan K. Hubbard error = 0; 2348f5f7ba03SJordan K. Hubbard else 2349f5f7ba03SJordan K. Hubbard error = EINVAL; 2350f5f7ba03SJordan K. Hubbard 2351f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 2352f664aeeeSJoerg Wunsch free(buffer, M_TEMP); 2353f5f7ba03SJordan K. Hubbard break; 2354f5f7ba03SJordan K. Hubbard 2355f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2356f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2357f5f7ba03SJordan K. Hubbard error = EBADF; 2358f5f7ba03SJordan K. Hubbard break; 2359f5f7ba03SJordan K. Hubbard 2360f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2361f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2362f5f7ba03SJordan K. Hubbard error = EBADF; 2363f5f7ba03SJordan K. Hubbard break; 2364f5f7ba03SJordan K. Hubbard 2365f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 23666182fdbdSPeter Wemm if ((flag & FWRITE) == 0) { 2367f5f7ba03SJordan K. Hubbard error = EBADF; 2368f5f7ba03SJordan K. Hubbard break; 2369f5f7ba03SJordan K. Hubbard } 2370f5f7ba03SJordan K. Hubbard 2371f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 2372f5f7ba03SJordan K. Hubbard 2373f664aeeeSJoerg Wunsch buffer = malloc(DEV_BSIZE, M_TEMP, M_ZERO | M_NOWAIT); 2374f664aeeeSJoerg Wunsch if (buffer == 0) { 2375f664aeeeSJoerg Wunsch error = ENOMEM; 2376f664aeeeSJoerg Wunsch break; 2377f664aeeeSJoerg Wunsch } 2378191e1a59SBruce Evans if ((error = setdisklabel((struct disklabel *)buffer, dl, 2379191e1a59SBruce Evans (u_long)0)) != 0) 2380f5f7ba03SJordan K. Hubbard break; 2381f5f7ba03SJordan K. Hubbard 238249ff4debSPoul-Henning Kamp error = writedisklabel(dev, (struct disklabel *)buffer); 2383f664aeeeSJoerg Wunsch free(buffer, M_TEMP); 2384b39c878eSAndrey A. Chernov break; 2385f664aeeeSJoerg Wunsch 2386b39c878eSAndrey A. Chernov case FD_FORM: 2387b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2388b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 2389b39c878eSAndrey A. Chernov else if (((struct fd_formb *)addr)->format_version != 2390b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2391b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 2392b39c878eSAndrey A. Chernov else 2393f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_FORMAT, addr); 2394b39c878eSAndrey A. Chernov break; 2395b39c878eSAndrey A. Chernov 2396b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 23973e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2398f5f7ba03SJordan K. Hubbard break; 2399f5f7ba03SJordan K. Hubbard 24003a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 24013a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 2402f711d546SPoul-Henning Kamp if (suser(p) != 0) 24033a2f7427SDavid Greenman return EPERM; 24043e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 24053a2f7427SDavid Greenman break; 24063a2f7427SDavid Greenman 24073a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 24083e425b96SJulian Elischer *(int *)addr = fd->options; 24093a2f7427SDavid Greenman break; 24103a2f7427SDavid Greenman 24113a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 24123e425b96SJulian Elischer fd->options = *(int *)addr; 24133a2f7427SDavid Greenman break; 24143a2f7427SDavid Greenman 2415f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG 2416f664aeeeSJoerg Wunsch case FD_DEBUG: 2417f664aeeeSJoerg Wunsch fd_debug = *(int *)addr; 2418f664aeeeSJoerg Wunsch break; 2419f664aeeeSJoerg Wunsch #endif 2420f664aeeeSJoerg Wunsch 24212995d110SJoerg Wunsch case FD_CLRERR: 24222995d110SJoerg Wunsch if (suser(p) != 0) 24232995d110SJoerg Wunsch return EPERM; 24242995d110SJoerg Wunsch fd->fdc->fdc_errs = 0; 24252995d110SJoerg Wunsch break; 24262995d110SJoerg Wunsch 24272995d110SJoerg Wunsch case FD_GSTAT: 24282995d110SJoerg Wunsch fsp = (struct fdc_status *)addr; 24292995d110SJoerg Wunsch if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 24302995d110SJoerg Wunsch return EINVAL; 24312995d110SJoerg Wunsch memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 24322995d110SJoerg Wunsch break; 24332995d110SJoerg Wunsch 2434250300ebSJoerg Wunsch case FD_READID: 2435250300ebSJoerg Wunsch rid = (struct fdc_readid *)addr; 2436250300ebSJoerg Wunsch if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2437250300ebSJoerg Wunsch return EINVAL; 2438f664aeeeSJoerg Wunsch error = fdmisccmd(dev, BIO_RDSECTID, addr); 2439250300ebSJoerg Wunsch break; 2440250300ebSJoerg Wunsch 2441f5f7ba03SJordan K. Hubbard default: 24423a2f7427SDavid Greenman error = ENOTTY; 2443f5f7ba03SJordan K. Hubbard break; 2444f5f7ba03SJordan K. Hubbard } 2445f5f7ba03SJordan K. Hubbard return (error); 2446f5f7ba03SJordan K. Hubbard } 2447