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. 325b81b6b3SRodney W. Grimes * 335b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 345b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 355b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 365b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 375b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 385b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 395b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 405b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 415b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 425b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 435b81b6b3SRodney W. Grimes * SUCH DAMAGE. 445b81b6b3SRodney W. Grimes * 45dc4ff321SRodney W. Grimes * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 46c3aac50fSPeter Wemm * $FreeBSD$ 475b81b6b3SRodney W. Grimes * 485b81b6b3SRodney W. Grimes */ 495b81b6b3SRodney W. Grimes 50d2fb4892SJoerg Wunsch #include "opt_fdc.h" 515f830ea2SJoerg Wunsch #include "card.h" 525b81b6b3SRodney W. Grimes 53b99f0a4aSAndrew Moore #include <sys/param.h> 54b99f0a4aSAndrew Moore #include <sys/systm.h> 559626b608SPoul-Henning Kamp #include <sys/bio.h> 566182fdbdSPeter Wemm #include <sys/bus.h> 576182fdbdSPeter Wemm #include <sys/conf.h> 58b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h> 59fb919e4dSMark Murray #include <sys/disklabel.h> 606182fdbdSPeter Wemm #include <sys/fcntl.h> 61fb919e4dSMark Murray #include <sys/kernel.h> 62fb919e4dSMark Murray #include <sys/lock.h> 63b99f0a4aSAndrew Moore #include <sys/malloc.h> 646182fdbdSPeter Wemm #include <sys/module.h> 65fb919e4dSMark Murray #include <sys/mutex.h> 663a2f7427SDavid Greenman #include <sys/proc.h> 67b99f0a4aSAndrew Moore #include <sys/syslog.h> 686182fdbdSPeter Wemm 696182fdbdSPeter Wemm #include <sys/bus.h> 706182fdbdSPeter Wemm #include <machine/bus.h> 716182fdbdSPeter Wemm #include <sys/rman.h> 726182fdbdSPeter Wemm 736182fdbdSPeter Wemm #include <machine/clock.h> 746182fdbdSPeter Wemm #include <machine/ioctl_fd.h> 756182fdbdSPeter Wemm #include <machine/resource.h> 76dc5df763SJoerg Wunsch #include <machine/stdarg.h> 776182fdbdSPeter Wemm 786182fdbdSPeter Wemm #include <isa/isavar.h> 79a97c75b7SDoug Rabson #include <isa/isareg.h> 80a97c75b7SDoug Rabson #include <isa/fdreg.h> 81a97c75b7SDoug Rabson #include <isa/fdc.h> 82a97c75b7SDoug Rabson #include <isa/rtc.h> 836182fdbdSPeter Wemm 84b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */ 855b81b6b3SRodney W. Grimes 860722d6abSJoerg Wunsch /* configuration flags */ 870722d6abSJoerg Wunsch #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 88e34c71eaSJoerg Wunsch #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 890722d6abSJoerg Wunsch 900722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */ 910722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED 0x1000 920722d6abSJoerg Wunsch 93dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 94dc5df763SJoerg Wunsch #define FD_FAILED -1 95dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 96dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 97dc5df763SJoerg Wunsch 986fb89845SKATO Takenori #define NUMTYPES 17 996fb89845SKATO Takenori #define NUMDENS (NUMTYPES - 7) 1007ca0641bSAndrey A. Chernov 1013a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */ 102b99f0a4aSAndrew Moore #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 103b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 104b99f0a4aSAndrew Moore #define FD_1720 1 105b99f0a4aSAndrew Moore #define FD_1480 2 106b99f0a4aSAndrew Moore #define FD_1440 3 107b99f0a4aSAndrew Moore #define FD_1200 4 108b99f0a4aSAndrew Moore #define FD_820 5 109b99f0a4aSAndrew Moore #define FD_800 6 110b99f0a4aSAndrew Moore #define FD_720 7 111b99f0a4aSAndrew Moore #define FD_360 8 1126fb89845SKATO Takenori #define FD_640 9 1136fb89845SKATO Takenori #define FD_1232 10 114ed2fa05eSAndrey A. Chernov 1156fb89845SKATO Takenori #define FD_1480in5_25 11 1166fb89845SKATO Takenori #define FD_1440in5_25 12 1176fb89845SKATO Takenori #define FD_820in5_25 13 1186fb89845SKATO Takenori #define FD_800in5_25 14 1196fb89845SKATO Takenori #define FD_720in5_25 15 1206fb89845SKATO Takenori #define FD_360in5_25 16 1216fb89845SKATO Takenori #define FD_640in5_25 17 122b99f0a4aSAndrew Moore 1237ca0641bSAndrey A. Chernov 1246f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] = 1255b81b6b3SRodney W. Grimes { 126126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 127126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 128126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 129126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 130126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 131126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 132126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 133b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 1346fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */ 1356fb89845SKATO Takenori { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */ 136ed2fa05eSAndrey A. Chernov 137126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 138126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 139126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 140126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 141126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 142126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 1436fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */ 1445b81b6b3SRodney W. Grimes }; 1455b81b6b3SRodney W. Grimes 146b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 147dc16046fSJoerg Wunsch 1485b81b6b3SRodney W. Grimes /***********************************************************************\ 1495b81b6b3SRodney W. Grimes * Per controller structure. * 1505b81b6b3SRodney W. Grimes \***********************************************************************/ 1516182fdbdSPeter Wemm static devclass_t fdc_devclass; 1525b81b6b3SRodney W. Grimes 1535b81b6b3SRodney W. Grimes /***********************************************************************\ 1545b81b6b3SRodney W. Grimes * Per drive structure. * 155b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 1565b81b6b3SRodney W. Grimes \***********************************************************************/ 1576182fdbdSPeter Wemm struct fd_data { 158b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 1595b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 1603a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 1615b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 1625b81b6b3SRodney W. Grimes int flags; 1635b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 1645b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 1655b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 1665b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 1675b81b6b3SRodney W. Grimes int skip; 1685b81b6b3SRodney W. Grimes int hddrv; 169dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 1705b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 1713a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 17202a19910SJustin T. Gibbs struct callout_handle toffhandle; 17302a19910SJustin T. Gibbs struct callout_handle tohandle; 174b2dfb1f9SJustin T. Gibbs struct devstat device_stats; 1756182fdbdSPeter Wemm device_t dev; 1766182fdbdSPeter Wemm fdu_t fdu; 1776182fdbdSPeter Wemm }; 17837286586SPeter Wemm 17937286586SPeter Wemm struct fdc_ivars { 18037286586SPeter Wemm int fdunit; 18137286586SPeter Wemm }; 1826182fdbdSPeter Wemm static devclass_t fd_devclass; 1835b81b6b3SRodney W. Grimes 1845b81b6b3SRodney W. Grimes /***********************************************************************\ 1855b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 1865b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 1875b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 1885b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 1895b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 1905b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 1915b81b6b3SRodney W. Grimes \***********************************************************************/ 192b99f0a4aSAndrew Moore 1933a2f7427SDavid Greenman /* needed for ft driver, thus exported */ 1946182fdbdSPeter Wemm int in_fdc(struct fdc_data *); 1956182fdbdSPeter Wemm int out_fdc(struct fdc_data *, int); 1963a2f7427SDavid Greenman 1973a2f7427SDavid Greenman /* internal functions */ 1986182fdbdSPeter Wemm static void fdc_intr(void *); 1996182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 2003a2f7427SDavid Greenman # define TURNON 1 2013a2f7427SDavid Greenman # define TURNOFF 0 2023a2f7427SDavid Greenman static timeout_t fd_turnoff; 2033a2f7427SDavid Greenman static timeout_t fd_motor_on; 2046182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 2053a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2066182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 2076182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 2085c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 2093a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2106182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 2116182fdbdSPeter Wemm static int retrier(struct fdc_data *); 2123a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *); 2133a2f7427SDavid Greenman 214d66c374fSTor Egge static int enable_fifo(fdc_p fdc); 215d66c374fSTor Egge 216d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 217d66c374fSTor Egge 218aaf08d94SGarrett Wollman 2195b81b6b3SRodney W. Grimes #define DEVIDLE 0 2205b81b6b3SRodney W. Grimes #define FINDWORK 1 2215b81b6b3SRodney W. Grimes #define DOSEEK 2 2225b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 3 2235b81b6b3SRodney W. Grimes #define IOCOMPLETE 4 2245b81b6b3SRodney W. Grimes #define RECALCOMPLETE 5 2255b81b6b3SRodney W. Grimes #define STARTRECAL 6 2265b81b6b3SRodney W. Grimes #define RESETCTLR 7 2275b81b6b3SRodney W. Grimes #define SEEKWAIT 8 2285b81b6b3SRodney W. Grimes #define RECALWAIT 9 2295b81b6b3SRodney W. Grimes #define MOTORWAIT 10 2305b81b6b3SRodney W. Grimes #define IOTIMEDOUT 11 2315c1a1eaeSBruce Evans #define RESETCOMPLETE 12 23269acd21dSWarner Losh #define PIOREAD 13 2335b81b6b3SRodney W. Grimes 234d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 235cba2a7c6SBruce Evans static char const * const fdstates[] = 2365b81b6b3SRodney W. Grimes { 2375b81b6b3SRodney W. Grimes "DEVIDLE", 2385b81b6b3SRodney W. Grimes "FINDWORK", 2395b81b6b3SRodney W. Grimes "DOSEEK", 2405b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 2415b81b6b3SRodney W. Grimes "IOCOMPLETE", 2425b81b6b3SRodney W. Grimes "RECALCOMPLETE", 2435b81b6b3SRodney W. Grimes "STARTRECAL", 2445b81b6b3SRodney W. Grimes "RESETCTLR", 2455b81b6b3SRodney W. Grimes "SEEKWAIT", 2465b81b6b3SRodney W. Grimes "RECALWAIT", 2475b81b6b3SRodney W. Grimes "MOTORWAIT", 2485c1a1eaeSBruce Evans "IOTIMEDOUT", 2495c1a1eaeSBruce Evans "RESETCOMPLETE", 250ff9607b0SBruce Evans "PIOREAD", 2515b81b6b3SRodney W. Grimes }; 2525b81b6b3SRodney W. Grimes 2533a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 254cba2a7c6SBruce Evans static int volatile fd_debug = 0; 2555b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 2565b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 257d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 2585b81b6b3SRodney W. Grimes #define TRACE0(arg) 2595b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 260d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 2615b81b6b3SRodney W. Grimes 262427ccf00SDoug Rabson static void 263427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 264427ccf00SDoug Rabson { 265427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 266427ccf00SDoug Rabson } 267427ccf00SDoug Rabson 268427ccf00SDoug Rabson static u_int8_t 269427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 270427ccf00SDoug Rabson { 271427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 272427ccf00SDoug Rabson } 273427ccf00SDoug Rabson 274427ccf00SDoug Rabson static void 275427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 276427ccf00SDoug Rabson { 277427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 278427ccf00SDoug Rabson } 279427ccf00SDoug Rabson 280427ccf00SDoug Rabson static u_int8_t 281427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 282427ccf00SDoug Rabson { 283427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 284427ccf00SDoug Rabson } 285427ccf00SDoug Rabson 286427ccf00SDoug Rabson static void 2875f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 288427ccf00SDoug Rabson { 289427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 290427ccf00SDoug Rabson } 291427ccf00SDoug Rabson 29258c9d623SPeter Wemm #if NCARD > 0 2935f830ea2SJoerg Wunsch static void 2945f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 2955f830ea2SJoerg Wunsch { 2965f830ea2SJoerg Wunsch bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 2975f830ea2SJoerg Wunsch } 29858c9d623SPeter Wemm #endif 2995f830ea2SJoerg Wunsch 300427ccf00SDoug Rabson #if 0 301427ccf00SDoug Rabson 302427ccf00SDoug Rabson static u_int8_t 303427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 304427ccf00SDoug Rabson { 305427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 306427ccf00SDoug Rabson } 307427ccf00SDoug Rabson 308427ccf00SDoug Rabson #endif 309427ccf00SDoug Rabson 31087f6c662SJulian Elischer static d_open_t Fdopen; /* NOTE, not fdopen */ 31187f6c662SJulian Elischer static d_close_t fdclose; 31287f6c662SJulian Elischer static d_ioctl_t fdioctl; 31387f6c662SJulian Elischer static d_strategy_t fdstrategy; 31487f6c662SJulian Elischer 31587f6c662SJulian Elischer #define CDEV_MAJOR 9 316f7ea2f55SJulian Elischer 3174e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 3184e2f199eSPoul-Henning Kamp /* open */ Fdopen, 3194e2f199eSPoul-Henning Kamp /* close */ fdclose, 3204e2f199eSPoul-Henning Kamp /* read */ physread, 3214e2f199eSPoul-Henning Kamp /* write */ physwrite, 3224e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 3234e2f199eSPoul-Henning Kamp /* poll */ nopoll, 3244e2f199eSPoul-Henning Kamp /* mmap */ nommap, 3254e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 3264e2f199eSPoul-Henning Kamp /* name */ "fd", 3274e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 3284e2f199eSPoul-Henning Kamp /* dump */ nodump, 3294e2f199eSPoul-Henning Kamp /* psize */ nopsize, 3304e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 3314e2f199eSPoul-Henning Kamp }; 3324e2f199eSPoul-Henning Kamp 333dc5df763SJoerg Wunsch static int 3346182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 335dc5df763SJoerg Wunsch { 3366182fdbdSPeter Wemm fdc->fdc_errs++; 33716b04b6aSJoerg Wunsch if (s) { 338b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 339b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 340b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 341b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 342b6e5f28eSPeter Wemm "logging any more\n"); 34316b04b6aSJoerg Wunsch } 344dc5df763SJoerg Wunsch 345dc5df763SJoerg Wunsch return FD_FAILED; 346dc5df763SJoerg Wunsch } 347dc5df763SJoerg Wunsch 348dc5df763SJoerg Wunsch /* 349dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 350dc5df763SJoerg Wunsch * Unit number, 351dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 352dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 353dc5df763SJoerg Wunsch */ 3546f4e0bebSPoul-Henning Kamp static int 3556182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 356dc5df763SJoerg Wunsch { 357dc5df763SJoerg Wunsch u_char cmd; 358dc5df763SJoerg Wunsch int n_in; 359dc5df763SJoerg Wunsch int n; 360dc5df763SJoerg Wunsch va_list ap; 361dc5df763SJoerg Wunsch 362dc5df763SJoerg Wunsch va_start(ap, n_out); 363dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 364dc5df763SJoerg Wunsch va_end(ap); 365dc5df763SJoerg Wunsch va_start(ap, n_out); 366dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 367dc5df763SJoerg Wunsch { 3686182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 369dc5df763SJoerg Wunsch { 370dc5df763SJoerg Wunsch char msg[50]; 3712127f260SArchie Cobbs snprintf(msg, sizeof(msg), 372dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 373dc5df763SJoerg Wunsch cmd, n + 1, n_out); 3746182fdbdSPeter Wemm return fdc_err(fdc, msg); 375dc5df763SJoerg Wunsch } 376dc5df763SJoerg Wunsch } 377dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 378dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 379dc5df763SJoerg Wunsch { 380dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 3816182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 382dc5df763SJoerg Wunsch { 383dc5df763SJoerg Wunsch char msg[50]; 3842127f260SArchie Cobbs snprintf(msg, sizeof(msg), 385dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 386dc5df763SJoerg Wunsch cmd, n + 1, n_in); 3876182fdbdSPeter Wemm return fdc_err(fdc, msg); 388dc5df763SJoerg Wunsch } 389dc5df763SJoerg Wunsch } 390dc5df763SJoerg Wunsch 391dc5df763SJoerg Wunsch return 0; 392dc5df763SJoerg Wunsch } 393dc5df763SJoerg Wunsch 3946f4e0bebSPoul-Henning Kamp static int 395d66c374fSTor Egge enable_fifo(fdc_p fdc) 396d66c374fSTor Egge { 397d66c374fSTor Egge int i, j; 398d66c374fSTor Egge 399d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 400d66c374fSTor Egge 401d66c374fSTor Egge /* 402d66c374fSTor Egge * XXX: 403d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 404d66c374fSTor Egge * this might be an invalid command. Thus we send the 405d66c374fSTor Egge * first byte, and check for an early turn of data directon. 406d66c374fSTor Egge */ 407d66c374fSTor Egge 4086182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 4096182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 410d66c374fSTor Egge 411d66c374fSTor Egge /* If command is invalid, return */ 412d66c374fSTor Egge j = 100000; 413427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 414d66c374fSTor Egge != NE7_RQM && j-- > 0) 415d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 416d66c374fSTor Egge fdc_reset(fdc); 417d66c374fSTor Egge return FD_FAILED; 418d66c374fSTor Egge } 419d66c374fSTor Egge if (j<0 || 4206182fdbdSPeter Wemm fd_cmd(fdc, 3, 421d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 422d66c374fSTor Egge fdc_reset(fdc); 4236182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 424d66c374fSTor Egge } 425d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 426d66c374fSTor Egge return 0; 427d66c374fSTor Egge } 4286182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 429d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 4306182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 431d66c374fSTor Egge return 0; 432d66c374fSTor Egge } 433d66c374fSTor Egge 434d66c374fSTor Egge static int 435dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 436dc5df763SJoerg Wunsch { 437dc5df763SJoerg Wunsch int st3; 438dc5df763SJoerg Wunsch 4396182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 440dc5df763SJoerg Wunsch { 4416182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 442dc5df763SJoerg Wunsch } 443dc5df763SJoerg Wunsch if (st3p) 444dc5df763SJoerg Wunsch *st3p = st3; 445dc5df763SJoerg Wunsch 446dc5df763SJoerg Wunsch return 0; 447dc5df763SJoerg Wunsch } 448dc5df763SJoerg Wunsch 4496f4e0bebSPoul-Henning Kamp static int 450dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 451dc5df763SJoerg Wunsch { 4526182fdbdSPeter Wemm int cyl, st0, ret; 453dc5df763SJoerg Wunsch 4546182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 4556182fdbdSPeter Wemm if (ret) { 4566182fdbdSPeter Wemm (void)fdc_err(fdc, 457dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 458dc5df763SJoerg Wunsch return ret; 459dc5df763SJoerg Wunsch } 460dc5df763SJoerg Wunsch 461dc5df763SJoerg Wunsch if (st0p) 462dc5df763SJoerg Wunsch *st0p = st0; 463dc5df763SJoerg Wunsch 4646182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 465dc5df763SJoerg Wunsch /* 466dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 467dc5df763SJoerg Wunsch */ 468dc5df763SJoerg Wunsch return FD_NOT_VALID; 469dc5df763SJoerg Wunsch } 470dc5df763SJoerg Wunsch 4716182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 4726182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 473dc5df763SJoerg Wunsch } 474dc5df763SJoerg Wunsch 475dc5df763SJoerg Wunsch if (cylp) 476dc5df763SJoerg Wunsch *cylp = cyl; 477dc5df763SJoerg Wunsch 478dc5df763SJoerg Wunsch return 0; 479dc5df763SJoerg Wunsch } 480dc5df763SJoerg Wunsch 481dc5df763SJoerg Wunsch 4826f4e0bebSPoul-Henning Kamp static int 483dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu) 484dc5df763SJoerg Wunsch { 485dc5df763SJoerg Wunsch int i, ret; 486b5e8ce9fSBruce Evans 4876182fdbdSPeter Wemm for (i = 0; i < 7; i++) { 488b5e8ce9fSBruce Evans /* 489b5e8ce9fSBruce Evans * XXX types are poorly chosen. Only bytes can by read 490a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 491b5e8ce9fSBruce Evans * fd_in() gives ints. 492b5e8ce9fSBruce Evans */ 493b5e8ce9fSBruce Evans int status; 494b5e8ce9fSBruce Evans 4956182fdbdSPeter Wemm ret = fd_in(fdc, &status); 496b5e8ce9fSBruce Evans fdc->status[i] = status; 497b5e8ce9fSBruce Evans if (ret != 0) 498dc5df763SJoerg Wunsch break; 499dc5df763SJoerg Wunsch } 500dc5df763SJoerg Wunsch 501dc5df763SJoerg Wunsch if (ret == 0) 502dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 503dc5df763SJoerg Wunsch else 504dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 505dc5df763SJoerg Wunsch 506dc5df763SJoerg Wunsch return ret; 507dc5df763SJoerg Wunsch } 508dc5df763SJoerg Wunsch 5095b81b6b3SRodney W. Grimes /****************************************************************************/ 5105b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5115b81b6b3SRodney W. Grimes /****************************************************************************/ 512dc5df763SJoerg Wunsch 5133a2f7427SDavid Greenman static int 51437286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 5155b81b6b3SRodney W. Grimes { 51637286586SPeter Wemm device_t dev; 5175f830ea2SJoerg Wunsch int ispnp, ispcmcia; 5185b81b6b3SRodney W. Grimes 51937286586SPeter Wemm dev = fdc->fdc_dev; 5205f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 5215f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 5226182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 5236182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 5246182fdbdSPeter Wemm 525b6e5f28eSPeter Wemm /* 5265f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 5275f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 5285f830ea2SJoerg Wunsch * register at 0x3f6. 5295f830ea2SJoerg Wunsch * 530b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 5315f830ea2SJoerg Wunsch * 5325f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 5335f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 5345f830ea2SJoerg Wunsch * one with offset 7 as control register. 535b6e5f28eSPeter Wemm */ 5366182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 5376182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 5385f830ea2SJoerg Wunsch ispcmcia ? 8 : (ispnp ? 1 : 6), 5395f830ea2SJoerg Wunsch RF_ACTIVE); 5406182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 541427ccf00SDoug Rabson device_printf(dev, "cannot reserve I/O port range\n"); 54237286586SPeter Wemm return ENXIO; 5436182fdbdSPeter Wemm } 544427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 545427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 546427ccf00SDoug Rabson 5475f830ea2SJoerg Wunsch if (!ispcmcia) { 548427ccf00SDoug Rabson /* 5495f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 5505f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 5515f830ea2SJoerg Wunsch * by checking the size and adjust the port address 5525f830ea2SJoerg Wunsch * accordingly. 553b6e5f28eSPeter Wemm */ 554b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 555b6e5f28eSPeter Wemm fdc->port_off = -2; 556b6e5f28eSPeter Wemm 557b6e5f28eSPeter Wemm /* 5585f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 5595f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 5605f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 561a2639a18SPeter Wemm * 5625f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 5635f830ea2SJoerg Wunsch * leave out the control register! It seems that some 5645f830ea2SJoerg Wunsch * non-antique controller chips have a different 5655f830ea2SJoerg Wunsch * method of programming the transfer speed which 5665f830ea2SJoerg Wunsch * doesn't require the control register, but it's 5675f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 5685f830ea2SJoerg Wunsch * address for the control register. 569427ccf00SDoug Rabson */ 570b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 571b9da888fSPeter Wemm u_long ctlstart; 572b9da888fSPeter Wemm 573b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 5745f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 5755f830ea2SJoerg Wunsch fdc->port_off + 7; 576b6e5f28eSPeter Wemm 577b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 578b9da888fSPeter Wemm } 579b6e5f28eSPeter Wemm 580b6e5f28eSPeter Wemm /* 581b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 582b6e5f28eSPeter Wemm */ 583427ccf00SDoug Rabson fdc->rid_ctl = 1; 5845f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 5855f830ea2SJoerg Wunsch &fdc->rid_ctl, 586b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 587427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 5885f830ea2SJoerg Wunsch device_printf(dev, 5895f830ea2SJoerg Wunsch "cannot reserve control I/O port range\n"); 59037286586SPeter Wemm return ENXIO; 591427ccf00SDoug Rabson } 592427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 593427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 5945f830ea2SJoerg Wunsch } 5956182fdbdSPeter Wemm 5966182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 5976182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 5986182fdbdSPeter Wemm RF_ACTIVE); 5996182fdbdSPeter Wemm if (fdc->res_irq == 0) { 600427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 60137286586SPeter Wemm return ENXIO; 6026182fdbdSPeter Wemm } 6035f830ea2SJoerg Wunsch 6045f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 6056182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 6066182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 6076182fdbdSPeter Wemm RF_ACTIVE); 6086182fdbdSPeter Wemm if (fdc->res_drq == 0) { 609427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 61037286586SPeter Wemm return ENXIO; 6116182fdbdSPeter Wemm } 6126182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 6135f830ea2SJoerg Wunsch } 61437286586SPeter Wemm 61537286586SPeter Wemm return 0; 61637286586SPeter Wemm } 61737286586SPeter Wemm 61837286586SPeter Wemm static void 61937286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 62037286586SPeter Wemm { 62137286586SPeter Wemm device_t dev; 62237286586SPeter Wemm 62337286586SPeter Wemm dev = fdc->fdc_dev; 62437286586SPeter Wemm if (fdc->res_irq != 0) { 62537286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62637286586SPeter Wemm fdc->res_irq); 62737286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62837286586SPeter Wemm fdc->res_irq); 62937286586SPeter Wemm } 63037286586SPeter Wemm if (fdc->res_ctl != 0) { 63137286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63237286586SPeter Wemm fdc->res_ctl); 63337286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63437286586SPeter Wemm fdc->res_ctl); 63537286586SPeter Wemm } 63637286586SPeter Wemm if (fdc->res_ioport != 0) { 63737286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 63837286586SPeter Wemm fdc->res_ioport); 63937286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 64037286586SPeter Wemm fdc->res_ioport); 64137286586SPeter Wemm } 64237286586SPeter Wemm if (fdc->res_drq != 0) { 64337286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64437286586SPeter Wemm fdc->res_drq); 64537286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64637286586SPeter Wemm fdc->res_drq); 64737286586SPeter Wemm } 64837286586SPeter Wemm } 64937286586SPeter Wemm 65037286586SPeter Wemm /****************************************************************************/ 65137286586SPeter Wemm /* autoconfiguration stuff */ 65237286586SPeter Wemm /****************************************************************************/ 65337286586SPeter Wemm 65437286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 65537286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 65637286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 65737286586SPeter Wemm {0} 65837286586SPeter Wemm }; 65937286586SPeter Wemm 66037286586SPeter Wemm static int 6613aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 66237286586SPeter Wemm { 66337286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 66437286586SPeter Wemm 66537286586SPeter Wemm switch (which) { 66637286586SPeter Wemm case FDC_IVAR_FDUNIT: 66737286586SPeter Wemm *result = ivars->fdunit; 66837286586SPeter Wemm break; 66937286586SPeter Wemm default: 67037286586SPeter Wemm return ENOENT; 67137286586SPeter Wemm } 67237286586SPeter Wemm return 0; 67337286586SPeter Wemm } 67437286586SPeter Wemm 67537286586SPeter Wemm /* 67637286586SPeter Wemm * fdc controller section. 67737286586SPeter Wemm */ 67837286586SPeter Wemm static int 67937286586SPeter Wemm fdc_probe(device_t dev) 68037286586SPeter Wemm { 68137286586SPeter Wemm int error, ic_type; 68237286586SPeter Wemm struct fdc_data *fdc; 68337286586SPeter Wemm 68437286586SPeter Wemm fdc = device_get_softc(dev); 68537286586SPeter Wemm bzero(fdc, sizeof *fdc); 68637286586SPeter Wemm fdc->fdc_dev = dev; 6875f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 68837286586SPeter Wemm 68937286586SPeter Wemm /* Check pnp ids */ 69037286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 69137286586SPeter Wemm if (error == ENXIO) 69237286586SPeter Wemm return ENXIO; 6935f830ea2SJoerg Wunsch if (error == 0) 6945f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 69537286586SPeter Wemm 69637286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 69737286586SPeter Wemm error = fdc_alloc_resources(fdc); 69837286586SPeter Wemm if (error) 69937286586SPeter Wemm goto out; 7005b81b6b3SRodney W. Grimes 70116111cedSAndrew Moore /* First - lets reset the floppy controller */ 702427ccf00SDoug Rabson fdout_wr(fdc, 0); 70316111cedSAndrew Moore DELAY(100); 704427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 70516111cedSAndrew Moore 7065b81b6b3SRodney W. Grimes /* see if it can handle a command */ 7076182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7086182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 7096182fdbdSPeter Wemm error = ENXIO; 7106182fdbdSPeter Wemm goto out; 7115b81b6b3SRodney W. Grimes } 7126182fdbdSPeter Wemm 7136182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 7146182fdbdSPeter Wemm ic_type = (u_char)ic_type; 7156182fdbdSPeter Wemm switch (ic_type) { 7166182fdbdSPeter Wemm case 0x80: 7176182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 7186182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 7196182fdbdSPeter Wemm break; 7206182fdbdSPeter Wemm case 0x81: 7216182fdbdSPeter Wemm device_set_desc(dev, "Intel 82077 or clone"); 7226182fdbdSPeter Wemm fdc->fdct = FDC_I82077; 7236182fdbdSPeter Wemm break; 7246182fdbdSPeter Wemm case 0x90: 7256182fdbdSPeter Wemm device_set_desc(dev, "NEC 72065B or clone"); 7266182fdbdSPeter Wemm fdc->fdct = FDC_NE72065; 7276182fdbdSPeter Wemm break; 7286182fdbdSPeter Wemm default: 7296182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 7306182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 7316182fdbdSPeter Wemm break; 7326182fdbdSPeter Wemm } 7336182fdbdSPeter Wemm } 7346182fdbdSPeter Wemm 7356182fdbdSPeter Wemm out: 73637286586SPeter Wemm fdc_release_resources(fdc); 7376182fdbdSPeter Wemm return (error); 7385b81b6b3SRodney W. Grimes } 7395b81b6b3SRodney W. Grimes 7405f830ea2SJoerg Wunsch #if NCARD > 0 7415f830ea2SJoerg Wunsch 7425f830ea2SJoerg Wunsch static int 7435f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 7445f830ea2SJoerg Wunsch { 7455f830ea2SJoerg Wunsch int error; 7465f830ea2SJoerg Wunsch struct fdc_data *fdc; 7475f830ea2SJoerg Wunsch 7485f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7495f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 7505f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 7515f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 7525f830ea2SJoerg Wunsch 7535f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 7545f830ea2SJoerg Wunsch 7555f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 7565f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 7575f830ea2SJoerg Wunsch if (error) 7585f830ea2SJoerg Wunsch goto out; 7595f830ea2SJoerg Wunsch 7605f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 7615f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 7625f830ea2SJoerg Wunsch DELAY(100); 7635f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 7645f830ea2SJoerg Wunsch 7655f830ea2SJoerg Wunsch /* see if it can handle a command */ 7665f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7675f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 7685f830ea2SJoerg Wunsch error = ENXIO; 7695f830ea2SJoerg Wunsch goto out; 7705f830ea2SJoerg Wunsch } 7715f830ea2SJoerg Wunsch 7725f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 7735f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 7745f830ea2SJoerg Wunsch 7755f830ea2SJoerg Wunsch out: 7765f830ea2SJoerg Wunsch fdc_release_resources(fdc); 7775f830ea2SJoerg Wunsch return (error); 7785f830ea2SJoerg Wunsch } 7795f830ea2SJoerg Wunsch 7805f830ea2SJoerg Wunsch static int 7815f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev) 7825f830ea2SJoerg Wunsch { 7835f830ea2SJoerg Wunsch struct fdc_data *fdc; 7845f830ea2SJoerg Wunsch int error; 7855f830ea2SJoerg Wunsch 7865f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7875f830ea2SJoerg Wunsch 7885f830ea2SJoerg Wunsch /* have our children detached first */ 7895f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 7905f830ea2SJoerg Wunsch return (error); 7915f830ea2SJoerg Wunsch 7925f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 7935f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 7945f830ea2SJoerg Wunsch return (0); 7955f830ea2SJoerg Wunsch } 7965f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 7975f830ea2SJoerg Wunsch 7985f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 7995f830ea2SJoerg Wunsch fdc->fdc_intr); 8005f830ea2SJoerg Wunsch fdc_release_resources(fdc); 8015f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 8025f830ea2SJoerg Wunsch return (0); 8035f830ea2SJoerg Wunsch } 8045f830ea2SJoerg Wunsch 8055f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 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; 83537286586SPeter Wemm int i, error; 836ada54f9eSPeter Wemm const char *name; 837427ccf00SDoug Rabson 83837286586SPeter Wemm fdc = device_get_softc(dev); 83937286586SPeter Wemm error = fdc_alloc_resources(fdc); 84037286586SPeter Wemm if (error) { 84137286586SPeter Wemm device_printf(dev, "cannot re-aquire 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) { 855100f78bbSSujal Patel /* Acquire the DMA channel forever, The driver will do the rest */ 8566182fdbdSPeter Wemm /* XXX should integrate with rman */ 857100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 858dd87702aSBruce Evans isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 8595f830ea2SJoerg Wunsch } 8605b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 8616182fdbdSPeter Wemm 8623a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 863427ccf00SDoug Rabson fdout_wr(fdc, ((fdc->fdout = 0))); 8648177437dSPoul-Henning Kamp bioq_init(&fdc->head); 8655b81b6b3SRodney W. Grimes 8666182fdbdSPeter Wemm /* 86737286586SPeter Wemm * Probe and attach any children. We should probably detect 86837286586SPeter Wemm * devices from the BIOS unless overridden. 8696182fdbdSPeter Wemm */ 870ada54f9eSPeter Wemm name = device_get_nameunit(dev); 871ada54f9eSPeter Wemm i = -1; 872ada54f9eSPeter Wemm while ((i = resource_query_string(i, "at", name)) != -1) 87337286586SPeter Wemm fdc_add_child(dev, resource_query_name(i), 87437286586SPeter Wemm resource_query_unit(i)); 87537286586SPeter Wemm 8766182fdbdSPeter Wemm return (bus_generic_attach(dev)); 8776182fdbdSPeter Wemm } 8786182fdbdSPeter Wemm 87915317dd8SMatthew N. Dodd static int 8806182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 8816182fdbdSPeter Wemm { 88215317dd8SMatthew N. Dodd int retval = 0; 88315317dd8SMatthew N. Dodd 88415317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 88515317dd8SMatthew N. Dodd retval += printf(" on %s drive %d\n", device_get_nameunit(me), 88637286586SPeter Wemm fdc_get_fdunit(child)); 88715317dd8SMatthew N. Dodd 88815317dd8SMatthew N. Dodd return (retval); 8896182fdbdSPeter Wemm } 8906182fdbdSPeter Wemm 89116e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 89216e68fc6SPeter Wemm /* Device interface */ 89316e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 89416e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 89516e68fc6SPeter Wemm DEVMETHOD(device_detach, bus_generic_detach), 89616e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 89716e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 89816e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 89916e68fc6SPeter Wemm 90016e68fc6SPeter Wemm /* Bus interface */ 90116e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 90237286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 90316e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 90416e68fc6SPeter Wemm 90516e68fc6SPeter Wemm { 0, 0 } 90616e68fc6SPeter Wemm }; 90716e68fc6SPeter Wemm 90816e68fc6SPeter Wemm static driver_t fdc_driver = { 90916e68fc6SPeter Wemm "fdc", 91016e68fc6SPeter Wemm fdc_methods, 91116e68fc6SPeter Wemm sizeof(struct fdc_data) 91216e68fc6SPeter Wemm }; 91316e68fc6SPeter Wemm 91416e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 91516e68fc6SPeter Wemm 9165f830ea2SJoerg Wunsch #if NCARD > 0 9175f830ea2SJoerg Wunsch 9185f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 9195f830ea2SJoerg Wunsch /* Device interface */ 9205f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 9215f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 9225f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fdc_pccard_detach), 9235f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 9245f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 9255f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 9265f830ea2SJoerg Wunsch 9275f830ea2SJoerg Wunsch /* Bus interface */ 9285f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 9295f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 9305f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 9315f830ea2SJoerg Wunsch 9325f830ea2SJoerg Wunsch { 0, 0 } 9335f830ea2SJoerg Wunsch }; 9345f830ea2SJoerg Wunsch 9355f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 9365f830ea2SJoerg Wunsch "fdc", 9375f830ea2SJoerg Wunsch fdc_pccard_methods, 9385f830ea2SJoerg Wunsch sizeof(struct fdc_data) 9395f830ea2SJoerg Wunsch }; 9405f830ea2SJoerg Wunsch 9415f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 9425f830ea2SJoerg Wunsch 9435f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 9445f830ea2SJoerg Wunsch 9453f54a085SPoul-Henning Kamp static void fd_clone __P((void *arg, char *name, int namelen, dev_t *dev)); 9463f54a085SPoul-Henning Kamp 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 9733f54a085SPoul-Henning Kamp fd_clone(arg, name, namelen, dev) 9743f54a085SPoul-Henning Kamp void *arg; 9753f54a085SPoul-Henning Kamp char *name; 9763f54a085SPoul-Henning Kamp int namelen; 9773f54a085SPoul-Henning Kamp dev_t *dev; 9783f54a085SPoul-Henning Kamp { 9793f54a085SPoul-Henning Kamp int u, d, i; 9803f54a085SPoul-Henning Kamp char *n; 9813f54a085SPoul-Henning Kamp dev_t pdev; 9823f54a085SPoul-Henning Kamp 9833f54a085SPoul-Henning Kamp if (*dev != NODEV) 9843f54a085SPoul-Henning Kamp return; 985db901281SPoul-Henning Kamp if (dev_stdclone(name, &n, "fd", &u) != 2) 9863f54a085SPoul-Henning Kamp return; 9873f54a085SPoul-Henning Kamp for (i = 0; ; i++) { 9883f54a085SPoul-Henning Kamp if (fd_suffix[i].match == NULL) 9893f54a085SPoul-Henning Kamp return; 9903f54a085SPoul-Henning Kamp if (strcmp(n, fd_suffix[i].match)) 9913f54a085SPoul-Henning Kamp continue; 9923f54a085SPoul-Henning Kamp d = fd_suffix[i].minor; 9933f54a085SPoul-Henning Kamp break; 9943f54a085SPoul-Henning Kamp } 9953f54a085SPoul-Henning Kamp if (fd_suffix[i].link == 0) { 9963f54a085SPoul-Henning Kamp *dev = make_dev(&fd_cdevsw, (u << 6) + d, 9973f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, name); 9983f54a085SPoul-Henning Kamp } else { 9993f54a085SPoul-Henning Kamp pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d); 10003f54a085SPoul-Henning Kamp *dev = make_dev_alias(pdev, name); 10013f54a085SPoul-Henning Kamp } 10023f54a085SPoul-Henning Kamp } 10033f54a085SPoul-Henning Kamp 100416e68fc6SPeter Wemm /******************************************************************/ 100516e68fc6SPeter Wemm /* 100616e68fc6SPeter Wemm * devices attached to the controller section. 100716e68fc6SPeter Wemm */ 10086182fdbdSPeter Wemm static int 10096182fdbdSPeter Wemm fd_probe(device_t dev) 10106182fdbdSPeter Wemm { 10116182fdbdSPeter Wemm int i; 10126182fdbdSPeter Wemm u_int fdt, st0, st3; 10136182fdbdSPeter Wemm struct fd_data *fd; 10146182fdbdSPeter Wemm struct fdc_data *fdc; 10156182fdbdSPeter Wemm fdsu_t fdsu; 10166182fdbdSPeter Wemm static int fd_fifo = 0; 10176182fdbdSPeter Wemm 10186182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 10196182fdbdSPeter Wemm fd = device_get_softc(dev); 10206182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 10216182fdbdSPeter Wemm 10226182fdbdSPeter Wemm bzero(fd, sizeof *fd); 10236182fdbdSPeter Wemm fd->dev = dev; 10246182fdbdSPeter Wemm fd->fdc = fdc; 10256182fdbdSPeter Wemm fd->fdsu = fdsu; 10266182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 10276182fdbdSPeter Wemm 1028a97c75b7SDoug Rabson #ifdef __i386__ 1029b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 10306182fdbdSPeter Wemm switch (fd->fdu) { 10316182fdbdSPeter Wemm case 0: 10325f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 10335f830ea2SJoerg Wunsch fdt = RTCFDT_144M; 10345f830ea2SJoerg Wunsch else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 10350722d6abSJoerg Wunsch fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 10360722d6abSJoerg Wunsch else 10370722d6abSJoerg Wunsch fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 1038b99f0a4aSAndrew Moore break; 10396182fdbdSPeter Wemm case 1: 10406182fdbdSPeter Wemm fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 1041dc5df763SJoerg Wunsch break; 1042dc5df763SJoerg Wunsch default: 10436182fdbdSPeter Wemm fdt = RTCFDT_NONE; 1044dc5df763SJoerg Wunsch break; 10456b7bd95bSJoerg Wunsch } 1046a97c75b7SDoug Rabson #else 1047a97c75b7SDoug Rabson fdt = RTCFDT_144M; /* XXX probably */ 1048a97c75b7SDoug Rabson #endif 10496182fdbdSPeter Wemm 10506182fdbdSPeter Wemm /* is there a unit? */ 10516182fdbdSPeter Wemm if (fdt == RTCFDT_NONE) 10526182fdbdSPeter Wemm return (ENXIO); 10536182fdbdSPeter Wemm 10546182fdbdSPeter Wemm /* select it */ 10556182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 10566182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 10576182fdbdSPeter Wemm 10588de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 10596182fdbdSPeter Wemm if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 1060e34c71eaSJoerg Wunsch && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 10616182fdbdSPeter Wemm && enable_fifo(fdc) == 0) { 1062b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1063b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1064d66c374fSTor Egge } 10656182fdbdSPeter Wemm fd_fifo = 1; 10666182fdbdSPeter Wemm 10676182fdbdSPeter Wemm if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 10686182fdbdSPeter Wemm && (st3 & NE7_ST3_T0)) { 1069dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 1070dc5df763SJoerg Wunsch /* seek some steps: */ 10716182fdbdSPeter Wemm fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 1072dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 10736182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1074dc5df763SJoerg Wunsch } 1075dc5df763SJoerg Wunsch 1076dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 10776182fdbdSPeter Wemm if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1078dc5df763SJoerg Wunsch /* Seek some steps... */ 10796182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1080dc5df763SJoerg Wunsch /* ...wait a moment... */ 1081dc5df763SJoerg Wunsch DELAY(300000); 1082dc5df763SJoerg Wunsch /* make ctrlr happy: */ 10836182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1084dc5df763SJoerg Wunsch } 1085dc5df763SJoerg Wunsch } 1086dc5df763SJoerg Wunsch 10876b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 10886b7bd95bSJoerg Wunsch /* 10896b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 10906b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 10916b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 10926b7bd95bSJoerg Wunsch * more than 77 steps 10936b7bd95bSJoerg Wunsch */ 1094dc5df763SJoerg Wunsch /* go back to 0: */ 10956182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 10966b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 10976b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 10985b81b6b3SRodney W. Grimes 10996b7bd95bSJoerg Wunsch /* anything responding? */ 1100dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1101dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 11026b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 11036b7bd95bSJoerg Wunsch } 1104dc5df763SJoerg Wunsch } 11056b7bd95bSJoerg Wunsch 11066182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 11073a2f7427SDavid Greenman 11083a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 11096182fdbdSPeter Wemm return (ENXIO); 11105b81b6b3SRodney W. Grimes 1111dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1112b99f0a4aSAndrew Moore fd->fdc = fdc; 1113b99f0a4aSAndrew Moore fd->fdsu = fdsu; 11143a2f7427SDavid Greenman fd->options = 0; 111502a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 111602a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 11175b81b6b3SRodney W. Grimes 1118b99f0a4aSAndrew Moore switch (fdt) { 11197ca0641bSAndrey A. Chernov case RTCFDT_12M: 11206182fdbdSPeter Wemm device_set_desc(dev, "1200-KB 5.25\" drive"); 1121b99f0a4aSAndrew Moore fd->type = FD_1200; 11227ca0641bSAndrey A. Chernov break; 11230722d6abSJoerg Wunsch case RTCFDT_144M | RTCFDT_144M_PRETENDED: 11246182fdbdSPeter Wemm device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 11250722d6abSJoerg Wunsch fdt = RTCFDT_144M; 11266182fdbdSPeter Wemm fd->type = FD_1440; 11277ca0641bSAndrey A. Chernov case RTCFDT_144M: 11286182fdbdSPeter Wemm device_set_desc(dev, "1440-KB 3.5\" drive"); 1129b99f0a4aSAndrew Moore fd->type = FD_1440; 11307ca0641bSAndrey A. Chernov break; 1131290dd077SJoerg Wunsch case RTCFDT_288M: 113286a727d9SJoerg Wunsch case RTCFDT_288M_1: 11336182fdbdSPeter Wemm device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1134290dd077SJoerg Wunsch fd->type = FD_1440; 1135290dd077SJoerg Wunsch break; 11367ca0641bSAndrey A. Chernov case RTCFDT_360K: 11376182fdbdSPeter Wemm device_set_desc(dev, "360-KB 5.25\" drive"); 1138b99f0a4aSAndrew Moore fd->type = FD_360; 11397ca0641bSAndrey A. Chernov break; 1140ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 11416182fdbdSPeter Wemm printf("720-KB 3.5\" drive"); 1142b99f0a4aSAndrew Moore fd->type = FD_720; 1143ed2fa05eSAndrey A. Chernov break; 11447ca0641bSAndrey A. Chernov default: 11456182fdbdSPeter Wemm return (ENXIO); 11465b81b6b3SRodney W. Grimes } 11476182fdbdSPeter Wemm return (0); 11486182fdbdSPeter Wemm } 11496182fdbdSPeter Wemm 11506182fdbdSPeter Wemm static int 11516182fdbdSPeter Wemm fd_attach(device_t dev) 11526182fdbdSPeter Wemm { 11536182fdbdSPeter Wemm struct fd_data *fd; 1154db901281SPoul-Henning Kamp static int cdevsw_add_done = 0; 11556182fdbdSPeter Wemm 11566182fdbdSPeter Wemm fd = device_get_softc(dev); 11576182fdbdSPeter Wemm 115802211baeSNick Hibma if (!cdevsw_add_done) { 1159475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 116002211baeSNick Hibma cdevsw_add_done++; 116102211baeSNick Hibma } 1162db901281SPoul-Henning Kamp EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000); 11633f54a085SPoul-Henning Kamp make_dev(&fd_cdevsw, (fd->fdu << 6), 11643f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 1165ada9bd8cSJulian Elischer 1166671e2ceeSBruce Evans /* 1167b2dfb1f9SJustin T. Gibbs * Export the drive to the devstat interface. 1168671e2ceeSBruce Evans */ 11696182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 11706182fdbdSPeter Wemm device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS, 11712a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 11722a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 11736182fdbdSPeter Wemm return (0); 11745b81b6b3SRodney W. Grimes } 11755b81b6b3SRodney W. Grimes 11765f830ea2SJoerg Wunsch static int 11775f830ea2SJoerg Wunsch fd_detach(device_t dev) 11785f830ea2SJoerg Wunsch { 11795f830ea2SJoerg Wunsch struct fd_data *fd; 11805f830ea2SJoerg Wunsch 11815f830ea2SJoerg Wunsch fd = device_get_softc(dev); 11825f830ea2SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 11835f830ea2SJoerg Wunsch 11845f830ea2SJoerg Wunsch return (0); 11855f830ea2SJoerg Wunsch } 11865f830ea2SJoerg Wunsch 118716e68fc6SPeter Wemm static device_method_t fd_methods[] = { 118816e68fc6SPeter Wemm /* Device interface */ 118916e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 119016e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 11915f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 119216e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 119316e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 119416e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 119516e68fc6SPeter Wemm 119616e68fc6SPeter Wemm { 0, 0 } 119716e68fc6SPeter Wemm }; 119816e68fc6SPeter Wemm 119916e68fc6SPeter Wemm static driver_t fd_driver = { 120016e68fc6SPeter Wemm "fd", 120116e68fc6SPeter Wemm fd_methods, 120216e68fc6SPeter Wemm sizeof(struct fd_data) 120316e68fc6SPeter Wemm }; 120416e68fc6SPeter Wemm 1205475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 120616e68fc6SPeter Wemm 12075b81b6b3SRodney W. Grimes /****************************************************************************/ 12085b81b6b3SRodney W. Grimes /* motor control stuff */ 12095e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 12105b81b6b3SRodney W. Grimes /****************************************************************************/ 12113a2f7427SDavid Greenman static void 12126182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 12135b81b6b3SRodney W. Grimes { 12146182fdbdSPeter Wemm int fdout = fdc->fdout; 12153a2f7427SDavid Greenman int needspecify = 0; 12163a2f7427SDavid Greenman 12173a2f7427SDavid Greenman if(turnon) { 12183a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 12193a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 12203a2f7427SDavid Greenman } else 12213a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 12223a2f7427SDavid Greenman 12235e235068SJordan K. Hubbard if(!turnon 12245e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 12255e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 12265e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 12275e235068SJordan K. Hubbard else { 12283a2f7427SDavid Greenman /* make sure controller is selected and specified */ 12293a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 12303a2f7427SDavid Greenman needspecify = 1; 12313a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 12325b81b6b3SRodney W. Grimes } 12335b81b6b3SRodney W. Grimes 1234427ccf00SDoug Rabson fdout_wr(fdc, fdout); 12356182fdbdSPeter Wemm fdc->fdout = fdout; 12363a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 12373a2f7427SDavid Greenman 12383a2f7427SDavid Greenman if (needspecify) { 1239dc8603e3SJoerg Wunsch /* 1240dc5df763SJoerg Wunsch * XXX 1241dc8603e3SJoerg Wunsch * special case: since we have just woken up the FDC 1242dc8603e3SJoerg Wunsch * from its sleep, we silently assume the command will 1243dc8603e3SJoerg Wunsch * be accepted, and do not test for a timeout 1244dc8603e3SJoerg Wunsch */ 12456182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1246dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1247dc5df763SJoerg Wunsch 0); 12486182fdbdSPeter Wemm if (fdc->flags & FDC_HAS_FIFO) 12496182fdbdSPeter Wemm (void) enable_fifo(fdc); 12503a2f7427SDavid Greenman } 12513a2f7427SDavid Greenman } 12523a2f7427SDavid Greenman 1253381fe1aaSGarrett Wollman static void 12546182fdbdSPeter Wemm fd_turnoff(void *xfd) 12555b81b6b3SRodney W. Grimes { 1256f5f7ba03SJordan K. Hubbard int s; 12576182fdbdSPeter Wemm fd_p fd = xfd; 1258dc16046fSJoerg Wunsch 12596182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 12608335c1b8SBruce Evans 12615f830ea2SJoerg Wunsch s = splbio(); 12628335c1b8SBruce Evans /* 12638335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 12645f830ea2SJoerg Wunsch * 12655f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 12665f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 12675f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 12685f830ea2SJoerg Wunsch * so the state machine gets re-entered. 12698335c1b8SBruce Evans */ 12706182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 12715f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 12725f830ea2SJoerg Wunsch splx(s); 12738335c1b8SBruce Evans return; 12748335c1b8SBruce Evans } 12758335c1b8SBruce Evans 12765b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 12776182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1278f5f7ba03SJordan K. Hubbard splx(s); 12795b81b6b3SRodney W. Grimes } 12805b81b6b3SRodney W. Grimes 12813a2f7427SDavid Greenman static void 12826182fdbdSPeter Wemm fd_motor_on(void *xfd) 12835b81b6b3SRodney W. Grimes { 1284f5f7ba03SJordan K. Hubbard int s; 12856182fdbdSPeter Wemm fd_p fd = xfd; 1286f5f7ba03SJordan K. Hubbard 1287f5f7ba03SJordan K. Hubbard s = splbio(); 12885b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 12895b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 12905b81b6b3SRodney W. Grimes { 12916182fdbdSPeter Wemm fdc_intr(fd->fdc); 12925b81b6b3SRodney W. Grimes } 1293f5f7ba03SJordan K. Hubbard splx(s); 12945b81b6b3SRodney W. Grimes } 12955b81b6b3SRodney W. Grimes 12963a2f7427SDavid Greenman static void 12976182fdbdSPeter Wemm fd_turnon(fd_p fd) 12985b81b6b3SRodney W. Grimes { 12995b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 13005b81b6b3SRodney W. Grimes { 13013a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 13026182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 13036182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 13045b81b6b3SRodney W. Grimes } 13055b81b6b3SRodney W. Grimes } 13065b81b6b3SRodney W. Grimes 1307381fe1aaSGarrett Wollman static void 1308dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 13095b81b6b3SRodney W. Grimes { 13103a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1311427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13123a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13133a2f7427SDavid Greenman DELAY(100); 13143a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1315427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 13163a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 13173a2f7427SDavid Greenman DELAY(100); 1318427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 13193a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 13203a2f7427SDavid Greenman 1321dc5df763SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 13226182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1323dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1324dc5df763SJoerg Wunsch 0); 1325d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1326d66c374fSTor Egge (void) enable_fifo(fdc); 13275b81b6b3SRodney W. Grimes } 13285b81b6b3SRodney W. Grimes 13295b81b6b3SRodney W. Grimes /****************************************************************************/ 13305b81b6b3SRodney W. Grimes /* fdc in/out */ 13315b81b6b3SRodney W. Grimes /****************************************************************************/ 13325b81b6b3SRodney W. Grimes int 13336182fdbdSPeter Wemm in_fdc(struct fdc_data *fdc) 13345b81b6b3SRodney W. Grimes { 13355b81b6b3SRodney W. Grimes int i, j = 100000; 1336427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 13375b81b6b3SRodney W. Grimes != (NE7_DIO|NE7_RQM) && j-- > 0) 1338dc5df763SJoerg Wunsch if (i == NE7_RQM) 13396182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 13405b81b6b3SRodney W. Grimes if (j <= 0) 13416182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1342d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1343427ccf00SDoug Rabson i = fddata_rd(fdc); 13443a2f7427SDavid Greenman TRACE1("[FDDATA->0x%x]", (unsigned char)i); 13455b81b6b3SRodney W. Grimes return(i); 1346d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1347427ccf00SDoug Rabson return fddata_rd(fdc); 1348d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 13495b81b6b3SRodney W. Grimes } 13505b81b6b3SRodney W. Grimes 1351dc5df763SJoerg Wunsch /* 1352dc5df763SJoerg Wunsch * fd_in: Like in_fdc, but allows you to see if it worked. 1353dc5df763SJoerg Wunsch */ 1354b5e8ce9fSBruce Evans static int 13556182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1356dc5df763SJoerg Wunsch { 1357dc5df763SJoerg Wunsch int i, j = 100000; 1358427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1359dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 1360dc5df763SJoerg Wunsch if (i == NE7_RQM) 13616182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1362dc5df763SJoerg Wunsch if (j <= 0) 13636182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1364d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1365427ccf00SDoug Rabson i = fddata_rd(fdc); 1366dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1367dc5df763SJoerg Wunsch *ptr = i; 1368dc5df763SJoerg Wunsch return 0; 1369d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1370427ccf00SDoug Rabson i = fddata_rd(fdc); 1371dc5df763SJoerg Wunsch if (ptr) 1372dc5df763SJoerg Wunsch *ptr = i; 1373dc5df763SJoerg Wunsch return 0; 1374d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1375dc5df763SJoerg Wunsch } 1376dc5df763SJoerg Wunsch 1377dc5df763SJoerg Wunsch int 13786182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 13795b81b6b3SRodney W. Grimes { 13803b3837dbSRodney W. Grimes int i; 13815b81b6b3SRodney W. Grimes 13823b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 13833b3837dbSRodney W. Grimes i = 100000; 1384427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0); 13856182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 13863b3837dbSRodney W. Grimes 13873b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 13883b3837dbSRodney W. Grimes i = 100000; 1389427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0); 139016b04b6aSJoerg Wunsch if (i <= 0) 13916182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 13923b3837dbSRodney W. Grimes 13933b3837dbSRodney W. Grimes /* Send the command and return */ 1394427ccf00SDoug Rabson fddata_wr(fdc, x); 13953a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 13965b81b6b3SRodney W. Grimes return (0); 13975b81b6b3SRodney W. Grimes } 13985b81b6b3SRodney W. Grimes 13995b81b6b3SRodney W. Grimes /****************************************************************************/ 14005b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 14015b81b6b3SRodney W. Grimes /****************************************************************************/ 1402381fe1aaSGarrett Wollman int 1403671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 14045b81b6b3SRodney W. Grimes { 14055b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 140620a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 14076182fdbdSPeter Wemm fd_p fd; 1408b99f0a4aSAndrew Moore fdc_p fdc; 14095b81b6b3SRodney W. Grimes 14105b81b6b3SRodney W. Grimes /* check bounds */ 14116182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1412b99f0a4aSAndrew Moore return (ENXIO); 14136182fdbdSPeter Wemm fdc = fd->fdc; 14146182fdbdSPeter Wemm if ((fdc == NULL) || (fd->type == NO_TYPE)) 1415b99f0a4aSAndrew Moore return (ENXIO); 1416b99f0a4aSAndrew Moore if (type > NUMDENS) 1417b99f0a4aSAndrew Moore return (ENXIO); 14187ca0641bSAndrey A. Chernov if (type == 0) 14196182fdbdSPeter Wemm type = fd->type; 14207ca0641bSAndrey A. Chernov else { 14213e425b96SJulian Elischer /* 14223e425b96SJulian Elischer * For each type of basic drive, make sure we are trying 14233e425b96SJulian Elischer * to open a type it can do, 14243e425b96SJulian Elischer */ 14256182fdbdSPeter Wemm if (type != fd->type) { 14266182fdbdSPeter Wemm switch (fd->type) { 14277ca0641bSAndrey A. Chernov case FD_360: 14287ca0641bSAndrey A. Chernov return (ENXIO); 1429ed2fa05eSAndrey A. Chernov case FD_720: 1430b39c878eSAndrey A. Chernov if ( type != FD_820 1431b39c878eSAndrey A. Chernov && type != FD_800 14326fb89845SKATO Takenori && type != FD_640 1433ed2fa05eSAndrey A. Chernov ) 1434ed2fa05eSAndrey A. Chernov return (ENXIO); 1435ed2fa05eSAndrey A. Chernov break; 14367ca0641bSAndrey A. Chernov case FD_1200: 1437b39c878eSAndrey A. Chernov switch (type) { 1438b39c878eSAndrey A. Chernov case FD_1480: 1439b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1440fa4700b4SAndrey A. Chernov break; 14417ca0641bSAndrey A. Chernov case FD_1440: 1442b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1443b39c878eSAndrey A. Chernov break; 14446fb89845SKATO Takenori case FD_1232: 14456fb89845SKATO Takenori break; 1446b39c878eSAndrey A. Chernov case FD_820: 1447b39c878eSAndrey A. Chernov type = FD_820in5_25; 1448b39c878eSAndrey A. Chernov break; 1449b39c878eSAndrey A. Chernov case FD_800: 1450b39c878eSAndrey A. Chernov type = FD_800in5_25; 1451b39c878eSAndrey A. Chernov break; 1452b39c878eSAndrey A. Chernov case FD_720: 1453b39c878eSAndrey A. Chernov type = FD_720in5_25; 1454b39c878eSAndrey A. Chernov break; 14556fb89845SKATO Takenori case FD_640: 14566fb89845SKATO Takenori type = FD_640in5_25; 14576fb89845SKATO Takenori break; 1458b39c878eSAndrey A. Chernov case FD_360: 1459b39c878eSAndrey A. Chernov type = FD_360in5_25; 1460b39c878eSAndrey A. Chernov break; 1461b39c878eSAndrey A. Chernov default: 1462b39c878eSAndrey A. Chernov return(ENXIO); 1463b39c878eSAndrey A. Chernov } 1464b39c878eSAndrey A. Chernov break; 1465b39c878eSAndrey A. Chernov case FD_1440: 1466b39c878eSAndrey A. Chernov if ( type != FD_1720 1467b39c878eSAndrey A. Chernov && type != FD_1480 1468ed2fa05eSAndrey A. Chernov && type != FD_1200 1469b39c878eSAndrey A. Chernov && type != FD_820 1470b39c878eSAndrey A. Chernov && type != FD_800 1471b39c878eSAndrey A. Chernov && type != FD_720 14726fb89845SKATO Takenori && type != FD_640 14737ca0641bSAndrey A. Chernov ) 1474dffff499SAndrey A. Chernov return(ENXIO); 1475fa4700b4SAndrey A. Chernov break; 14767ca0641bSAndrey A. Chernov } 14777ca0641bSAndrey A. Chernov } 1478fa4700b4SAndrey A. Chernov } 14796182fdbdSPeter Wemm fd->ft = fd_types + type - 1; 14806182fdbdSPeter Wemm fd->flags |= FD_OPEN; 14815f830ea2SJoerg Wunsch 14825b81b6b3SRodney W. Grimes return 0; 14835b81b6b3SRodney W. Grimes } 14845b81b6b3SRodney W. Grimes 1485381fe1aaSGarrett Wollman int 1486671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 14875b81b6b3SRodney W. Grimes { 14885b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 14896182fdbdSPeter Wemm struct fd_data *fd; 1490b99f0a4aSAndrew Moore 14916182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14926182fdbdSPeter Wemm fd->flags &= ~FD_OPEN; 14932995d110SJoerg Wunsch fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1494dc16046fSJoerg Wunsch 14955b81b6b3SRodney W. Grimes return (0); 14965b81b6b3SRodney W. Grimes } 14975b81b6b3SRodney W. Grimes 14983a2f7427SDavid Greenman /****************************************************************************/ 14993a2f7427SDavid Greenman /* fdstrategy */ 15003a2f7427SDavid Greenman /****************************************************************************/ 15013a2f7427SDavid Greenman void 15028177437dSPoul-Henning Kamp fdstrategy(struct bio *bp) 15033a2f7427SDavid Greenman { 1504bb6382faSJoerg Wunsch unsigned nblocks, blknum, cando; 15053a2f7427SDavid Greenman int s; 15063a2f7427SDavid Greenman fdu_t fdu; 15073a2f7427SDavid Greenman fdc_p fdc; 15083a2f7427SDavid Greenman fd_p fd; 15093a2f7427SDavid Greenman size_t fdblk; 15103a2f7427SDavid Greenman 15118177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 15126182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 15136182fdbdSPeter Wemm if (fd == 0) 15146182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 15158177437dSPoul-Henning Kamp (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 15163a2f7427SDavid Greenman fdc = fd->fdc; 151769acd21dSWarner Losh if (fd->type == NO_TYPE) { 15188177437dSPoul-Henning Kamp bp->bio_error = ENXIO; 15198177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15203b178206SWarner Losh goto bad; 152169acd21dSWarner Losh }; 15223a2f7427SDavid Greenman 1523d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 15248177437dSPoul-Henning Kamp if (!(bp->bio_cmd & BIO_FORMAT)) { 15258177437dSPoul-Henning Kamp if (bp->bio_blkno < 0) { 1526dc5df763SJoerg Wunsch printf( 15276a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 15288177437dSPoul-Henning Kamp fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 15298177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15308177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15313a2f7427SDavid Greenman goto bad; 15323a2f7427SDavid Greenman } 15338177437dSPoul-Henning Kamp if ((bp->bio_bcount % fdblk) != 0) { 15348177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15358177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15363a2f7427SDavid Greenman goto bad; 15373a2f7427SDavid Greenman } 15383a2f7427SDavid Greenman } 15393a2f7427SDavid Greenman 15403a2f7427SDavid Greenman /* 15413a2f7427SDavid Greenman * Set up block calculations. 15423a2f7427SDavid Greenman */ 15438177437dSPoul-Henning Kamp if (bp->bio_blkno > 20000000) { 1544bb6382faSJoerg Wunsch /* 1545bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1546bb6382faSJoerg Wunsch * multiplication below from overflowing. 1547bb6382faSJoerg Wunsch */ 15488177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15498177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15503a2f7427SDavid Greenman goto bad; 15513a2f7427SDavid Greenman } 15528177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk; 1553bb6382faSJoerg Wunsch nblocks = fd->ft->size; 15548177437dSPoul-Henning Kamp bp->bio_resid = 0; 15558177437dSPoul-Henning Kamp if (blknum + (bp->bio_bcount / fdblk) > nblocks) { 1556bb6382faSJoerg Wunsch if (blknum <= nblocks) { 1557bb6382faSJoerg Wunsch cando = (nblocks - blknum) * fdblk; 15588177437dSPoul-Henning Kamp bp->bio_resid = bp->bio_bcount - cando; 1559bb6382faSJoerg Wunsch if (cando == 0) 1560bb6382faSJoerg Wunsch goto bad; /* not actually bad but EOF */ 1561bb6382faSJoerg Wunsch } else { 15628177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15638177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 1564bb6382faSJoerg Wunsch goto bad; 1565bb6382faSJoerg Wunsch } 1566bb6382faSJoerg Wunsch } 15678177437dSPoul-Henning Kamp bp->bio_pblkno = bp->bio_blkno; 15683a2f7427SDavid Greenman s = splbio(); 15698177437dSPoul-Henning Kamp bioqdisksort(&fdc->head, bp); 15706182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1571b2dfb1f9SJustin T. Gibbs 1572b2dfb1f9SJustin T. Gibbs /* Tell devstat we are starting on the transaction */ 1573b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 15745f830ea2SJoerg Wunsch device_busy(fd->dev); 1575b2dfb1f9SJustin T. Gibbs 15766182fdbdSPeter Wemm fdstart(fdc); 15773a2f7427SDavid Greenman splx(s); 15783a2f7427SDavid Greenman return; 15793a2f7427SDavid Greenman 15803a2f7427SDavid Greenman bad: 15813a2f7427SDavid Greenman biodone(bp); 15823a2f7427SDavid Greenman } 15833a2f7427SDavid Greenman 15845b81b6b3SRodney W. Grimes /***************************************************************\ 15855b81b6b3SRodney W. Grimes * fdstart * 15865b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 15875b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 15885b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 15895b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 15905b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 15915b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 15925b81b6b3SRodney W. Grimes \***************************************************************/ 1593381fe1aaSGarrett Wollman static void 15946182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 15955b81b6b3SRodney W. Grimes { 15965b81b6b3SRodney W. Grimes int s; 15975b81b6b3SRodney W. Grimes 15985b81b6b3SRodney W. Grimes s = splbio(); 15996182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 16005b81b6b3SRodney W. Grimes { 16016182fdbdSPeter Wemm fdc_intr(fdc); 16025b81b6b3SRodney W. Grimes } 16035b81b6b3SRodney W. Grimes splx(s); 16045b81b6b3SRodney W. Grimes } 16055b81b6b3SRodney W. Grimes 1606381fe1aaSGarrett Wollman static void 16076182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 16085b81b6b3SRodney W. Grimes { 16095c1a1eaeSBruce Evans fdc_p fdc; 1610f5f7ba03SJordan K. Hubbard int s; 16115b81b6b3SRodney W. Grimes 16126182fdbdSPeter Wemm fdc = xfdc; 16135c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 16145b81b6b3SRodney W. Grimes 16153a2f7427SDavid Greenman /* 16163a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 16173a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 16183a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 16193a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 16203a2f7427SDavid Greenman * Many thanks, Big Blue! 16215c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 16225c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 16235c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 16245c1a1eaeSBruce Evans * will reset the FDC and retry once. 16253a2f7427SDavid Greenman */ 16263a2f7427SDavid Greenman s = splbio(); 16275c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 16285c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 16295c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 16306182fdbdSPeter Wemm fdc_intr(fdc); 1631f5f7ba03SJordan K. Hubbard splx(s); 16325b81b6b3SRodney W. Grimes } 16335b81b6b3SRodney W. Grimes 16345b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1635381fe1aaSGarrett Wollman static void 16366182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 16375b81b6b3SRodney W. Grimes { 16385b81b6b3SRodney W. Grimes int s; 16393a2f7427SDavid Greenman 16405b81b6b3SRodney W. Grimes s = splbio(); 16416182fdbdSPeter Wemm fdc_intr(xfdc); 16425b81b6b3SRodney W. Grimes splx(s); 16435b81b6b3SRodney W. Grimes } 16445b81b6b3SRodney W. Grimes 16455b81b6b3SRodney W. Grimes /***********************************************************************\ 16465b81b6b3SRodney W. Grimes * fdintr * 16475b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 16485b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 16495b81b6b3SRodney W. Grimes \***********************************************************************/ 1650fe310de8SBruce Evans static void 16516182fdbdSPeter Wemm fdc_intr(void *xfdc) 16525b81b6b3SRodney W. Grimes { 16536182fdbdSPeter Wemm fdc_p fdc = xfdc; 16546182fdbdSPeter Wemm while(fdstate(fdc)) 1655381fe1aaSGarrett Wollman ; 16565b81b6b3SRodney W. Grimes } 16575b81b6b3SRodney W. Grimes 165869acd21dSWarner Losh /* 165969acd21dSWarner Losh * magic pseudo-DMA initialization for YE FDC. Sets count and 166069acd21dSWarner Losh * direction 166169acd21dSWarner Losh */ 16623b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 16633b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 16643b178206SWarner Losh ((cnt)-1) & 0xff); \ 16653b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 16663b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 166769acd21dSWarner Losh 166869acd21dSWarner Losh /* 166969acd21dSWarner Losh * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 167069acd21dSWarner Losh */ 16713b178206SWarner Losh static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 167269acd21dSWarner Losh { 167369acd21dSWarner Losh u_char *cptr = (u_char *)addr; 167469acd21dSWarner Losh 167521144e3bSPoul-Henning Kamp if (flags == BIO_READ) { 167669acd21dSWarner Losh if (fdc->state != PIOREAD) { 167769acd21dSWarner Losh fdc->state = PIOREAD; 167869acd21dSWarner Losh return(0); 167969acd21dSWarner Losh }; 16803b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 16813b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16823b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 168369acd21dSWarner Losh } else { 16843b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16853b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 16863b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 168769acd21dSWarner Losh }; 168869acd21dSWarner Losh return(1); 168969acd21dSWarner Losh } 169069acd21dSWarner Losh 16915b81b6b3SRodney W. Grimes /***********************************************************************\ 16925b81b6b3SRodney W. Grimes * The controller state machine. * 16935b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 16945b81b6b3SRodney W. Grimes \***********************************************************************/ 16953a2f7427SDavid Greenman static int 16966182fdbdSPeter Wemm fdstate(fdc_p fdc) 16975b81b6b3SRodney W. Grimes { 169856a23089SPoul-Henning Kamp int read, format, head, i, sec = 0, sectrac, st0, cyl, st3, idf; 1699bb6382faSJoerg Wunsch unsigned blknum = 0, b_cylinder = 0; 17005b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 17015b81b6b3SRodney W. Grimes fd_p fd; 17028177437dSPoul-Henning Kamp register struct bio *bp; 1703b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 17043a2f7427SDavid Greenman size_t fdblk; 17055b81b6b3SRodney W. Grimes 1706e93e63cbSBruce Evans bp = fdc->bp; 1707e93e63cbSBruce Evans if (bp == NULL) { 17088177437dSPoul-Henning Kamp bp = bioq_first(&fdc->head); 1709e93e63cbSBruce Evans if (bp != NULL) { 17108177437dSPoul-Henning Kamp bioq_remove(&fdc->head, bp); 1711e93e63cbSBruce Evans fdc->bp = bp; 1712e93e63cbSBruce Evans } 1713e93e63cbSBruce Evans } 1714e93e63cbSBruce Evans if (bp == NULL) { 17155b81b6b3SRodney W. Grimes /***********************************************\ 17165b81b6b3SRodney W. Grimes * nothing left for this controller to do * 17175b81b6b3SRodney W. Grimes * Force into the IDLE state, * 17185b81b6b3SRodney W. Grimes \***********************************************/ 17195b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 17206182fdbdSPeter Wemm if (fdc->fd) { 1721b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1722b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 17235b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 17245b81b6b3SRodney W. Grimes fdc->fdu = -1; 17255b81b6b3SRodney W. Grimes } 17266182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 17275b81b6b3SRodney W. Grimes return (0); 17285b81b6b3SRodney W. Grimes } 17298177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 17306182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 17313a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1732b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 1733b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 17348177437dSPoul-Henning Kamp read = bp->bio_cmd == BIO_READ; 173556a23089SPoul-Henning Kamp if (read) 173656a23089SPoul-Henning Kamp idf = ISADMA_READ; 173756a23089SPoul-Henning Kamp else 173856a23089SPoul-Henning Kamp idf = ISADMA_WRITE; 17398177437dSPoul-Henning Kamp format = bp->bio_cmd & BIO_FORMAT; 1740bb6382faSJoerg Wunsch if (format) { 17418177437dSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data; 1742bb6382faSJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1743bb6382faSJoerg Wunsch - (char *)finfo; 1744bb6382faSJoerg Wunsch } 1745bb6382faSJoerg Wunsch if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 17468177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk + 1747bb6382faSJoerg Wunsch fd->skip/fdblk; 1748bb6382faSJoerg Wunsch b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1749bb6382faSJoerg Wunsch } 17505b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 17515b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 17525b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 17536182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 17546182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 17555b81b6b3SRodney W. Grimes switch (fdc->state) 17565b81b6b3SRodney W. Grimes { 17575b81b6b3SRodney W. Grimes case DEVIDLE: 17585b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 17595b81b6b3SRodney W. Grimes fdc->retry = 0; 17605b81b6b3SRodney W. Grimes fd->skip = 0; 17615b81b6b3SRodney W. Grimes fdc->fd = fd; 17625b81b6b3SRodney W. Grimes fdc->fdu = fdu; 17635f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 17643a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 17655b81b6b3SRodney W. Grimes /*******************************************************\ 17665b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 1767dc733423SDag-Erling Smørgrav * it will start up in its own good time * 17685b81b6b3SRodney W. Grimes \*******************************************************/ 17696182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 17705b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17715b81b6b3SRodney W. Grimes return (0); /* come back later */ 17725b81b6b3SRodney W. Grimes } 17735b81b6b3SRodney W. Grimes /*******************************************************\ 17745b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 17755b81b6b3SRodney W. Grimes \*******************************************************/ 17765b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 17775b81b6b3SRodney W. Grimes { 17785b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17796182fdbdSPeter Wemm fd_turnon(fd); 17805b81b6b3SRodney W. Grimes return (0); 17815b81b6b3SRodney W. Grimes } 17825b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 17835b81b6b3SRodney W. Grimes { 17846182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 17855b81b6b3SRodney W. Grimes } 17865c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 17875c1a1eaeSBruce Evans fdc->state = RESETCTLR; 17885c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 17895c1a1eaeSBruce Evans } else 17905e235068SJordan K. Hubbard fdc->state = DOSEEK; 17915b81b6b3SRodney W. Grimes break; 17925b81b6b3SRodney W. Grimes case DOSEEK: 1793bb6382faSJoerg Wunsch if (b_cylinder == (unsigned)fd->track) 17945b81b6b3SRodney W. Grimes { 17955b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17965b81b6b3SRodney W. Grimes break; 17975b81b6b3SRodney W. Grimes } 17986182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1799bb6382faSJoerg Wunsch fd->fdsu, b_cylinder * fd->ft->steptrac, 1800dc5df763SJoerg Wunsch 0)) 1801dc8603e3SJoerg Wunsch { 1802dc8603e3SJoerg Wunsch /* 1803dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1804dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1805dc8603e3SJoerg Wunsch */ 1806dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 18076182fdbdSPeter Wemm return(retrier(fdc)); 1808dc8603e3SJoerg Wunsch } 1809dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 18105b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 18115b81b6b3SRodney W. Grimes return(0); /* will return later */ 18125b81b6b3SRodney W. Grimes case SEEKWAIT: 18135b81b6b3SRodney W. Grimes /* allow heads to settle */ 18146182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 18155b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 18165b81b6b3SRodney W. Grimes return(0); /* will return later */ 18175b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 18185b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 18196182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 1820bb6382faSJoerg Wunsch int descyl = b_cylinder * fd->ft->steptrac; 18213a2f7427SDavid Greenman do { 18223a2f7427SDavid Greenman /* 1823dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1824dc5df763SJoerg Wunsch * which cannot really happen since the 1825dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1826dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1827dc5df763SJoerg Wunsch * line, so do one of the following: 1828dc5df763SJoerg Wunsch * 1829dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1830dc5df763SJoerg Wunsch * known to not go stuck after responding 1831dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1832dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1833dc5df763SJoerg Wunsch * real interrupt condition. 1834dc5df763SJoerg Wunsch * 1835dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1836dc5df763SJoerg Wunsch * up immediately. The controller will 1837dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1838dc5df763SJoerg Wunsch * conditions right after reset (for the 1839dc5df763SJoerg Wunsch * corresponding four drives), so this is 1840dc5df763SJoerg Wunsch * our only chance to get notice that it 1841dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 18423a2f7427SDavid Greenman */ 1843dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1844dc5df763SJoerg Wunsch == FD_NOT_VALID) 1845dc5df763SJoerg Wunsch return 0; 1846dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1847dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1848dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 18493a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1850dc5df763SJoerg Wunsch 18516182fdbdSPeter Wemm if (0 == descyl) { 1852dc5df763SJoerg Wunsch int failed = 0; 18533a2f7427SDavid Greenman /* 18543a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 18553a2f7427SDavid Greenman * really there 18563a2f7427SDavid Greenman */ 1857dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1858dc5df763SJoerg Wunsch failed = 1; 18593a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 18603a2f7427SDavid Greenman printf( 18613a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 18623a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1863dc5df763SJoerg Wunsch failed = 1; 1864dc5df763SJoerg Wunsch } 1865dc5df763SJoerg Wunsch 18666182fdbdSPeter Wemm if (failed) { 18673a2f7427SDavid Greenman if(fdc->retry < 3) 18683a2f7427SDavid Greenman fdc->retry = 3; 18696182fdbdSPeter Wemm return (retrier(fdc)); 18703a2f7427SDavid Greenman } 18713a2f7427SDavid Greenman } 1872dc5df763SJoerg Wunsch 18736182fdbdSPeter Wemm if (cyl != descyl) { 18743a2f7427SDavid Greenman printf( 18753a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 18762d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 1877e5d7d243SBruce Evans if (fdc->retry < 3) 1878e5d7d243SBruce Evans fdc->retry = 3; 18796182fdbdSPeter Wemm return (retrier(fdc)); 18805b81b6b3SRodney W. Grimes } 18815b81b6b3SRodney W. Grimes } 18825b81b6b3SRodney W. Grimes 1883bb6382faSJoerg Wunsch fd->track = b_cylinder; 18843b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 18858177437dSPoul-Henning Kamp isa_dmastart(idf, bp->bio_data+fd->skip, 18868177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 18875b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 18885b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 18895b81b6b3SRodney W. Grimes head = sec / sectrac; 18905b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 18913a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 18923a2f7427SDavid Greenman 18933a2f7427SDavid Greenman if(format || !read) 18943a2f7427SDavid Greenman { 18953a2f7427SDavid Greenman /* make sure the drive is writable */ 1896dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1897dc8603e3SJoerg Wunsch { 1898dc8603e3SJoerg Wunsch /* stuck controller? */ 18995f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 190056a23089SPoul-Henning Kamp isa_dmadone(idf, 19018177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19028177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19035c1a1eaeSBruce Evans fdc->dmachan); 1904dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 19056182fdbdSPeter Wemm return (retrier(fdc)); 1906dc8603e3SJoerg Wunsch } 19073a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 19083a2f7427SDavid Greenman { 19093a2f7427SDavid Greenman /* 19103a2f7427SDavid Greenman * XXX YES! this is ugly. 19113a2f7427SDavid Greenman * in order to force the current operation 19123a2f7427SDavid Greenman * to fail, we will have to fake an FDC 19133a2f7427SDavid Greenman * error - all error handling is done 19143a2f7427SDavid Greenman * by the retrier() 19153a2f7427SDavid Greenman */ 19163a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 19173a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 19183a2f7427SDavid Greenman fdc->status[2] = 0; 19193a2f7427SDavid Greenman fdc->status[3] = fd->track; 19203a2f7427SDavid Greenman fdc->status[4] = head; 19213a2f7427SDavid Greenman fdc->status[5] = sec; 19223a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 19233a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 19243a2f7427SDavid Greenman return (1); 19253a2f7427SDavid Greenman } 19263a2f7427SDavid Greenman } 19275b81b6b3SRodney W. Grimes 19286182fdbdSPeter Wemm if (format) { 19295f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 19305f830ea2SJoerg Wunsch /* 19315f830ea2SJoerg Wunsch * This seems to be necessary for 19325f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 19335f830ea2SJoerg Wunsch * it, we end up filling the sector ID 19345f830ea2SJoerg Wunsch * fields of the newly formatted track 19355f830ea2SJoerg Wunsch * entirely with garbage, causing 19365f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 19375f830ea2SJoerg Wunsch * the place when trying to read them 19385f830ea2SJoerg Wunsch * back. 19395f830ea2SJoerg Wunsch * 19405f830ea2SJoerg Wunsch * Umpf. 19415f830ea2SJoerg Wunsch */ 19428177437dSPoul-Henning Kamp SET_BCDR(fdc, 1, bp->bio_bcount, 0); 19435f830ea2SJoerg Wunsch 19448177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19458177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 19468177437dSPoul-Henning Kamp bp->bio_bcount); 19475f830ea2SJoerg Wunsch 19485f830ea2SJoerg Wunsch } 1949b39c878eSAndrey A. Chernov /* formatting */ 19506182fdbdSPeter Wemm if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1951dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1952dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1953dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 19546182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 1955dc8603e3SJoerg Wunsch /* controller fell over */ 19565f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 195756a23089SPoul-Henning Kamp isa_dmadone(idf, 19588177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19598177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19605c1a1eaeSBruce Evans fdc->dmachan); 1961dc8603e3SJoerg Wunsch fdc->retry = 6; 19626182fdbdSPeter Wemm return (retrier(fdc)); 1963dc8603e3SJoerg Wunsch } 19646182fdbdSPeter Wemm } else { 19653b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 196669acd21dSWarner Losh /* 196769acd21dSWarner Losh * this seems to be necessary even when 196869acd21dSWarner Losh * reading data 196969acd21dSWarner Losh */ 19703b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 197169acd21dSWarner Losh 197269acd21dSWarner Losh /* 197369acd21dSWarner Losh * perform the write pseudo-DMA before 197469acd21dSWarner Losh * the WRITE command is sent 197569acd21dSWarner Losh */ 197669acd21dSWarner Losh if (!read) 19778177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19788177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 197969acd21dSWarner Losh fdblk); 198069acd21dSWarner Losh } 19816182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 1982dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1983dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1984dc5df763SJoerg Wunsch fd->track, /* track */ 1985dc5df763SJoerg Wunsch head, 1986dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1987dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1988dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1989dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1990dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 19916182fdbdSPeter Wemm 0)) { 1992dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 19935f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 199456a23089SPoul-Henning Kamp isa_dmadone(idf, 19958177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19968177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19975c1a1eaeSBruce Evans fdc->dmachan); 1998dc8603e3SJoerg Wunsch fdc->retry = 6; 19996182fdbdSPeter Wemm return (retrier(fdc)); 20005b81b6b3SRodney W. Grimes } 2001b39c878eSAndrey A. Chernov } 20023b178206SWarner Losh if (fdc->flags & FDC_NODMA) 200369acd21dSWarner Losh /* 200469acd21dSWarner Losh * if this is a read, then simply await interrupt 200569acd21dSWarner Losh * before performing PIO 200669acd21dSWarner Losh */ 20078177437dSPoul-Henning Kamp if (read && !fdcpio(fdc,bp->bio_cmd, 20088177437dSPoul-Henning Kamp bp->bio_data+fd->skip,fdblk)) { 20093b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 201069acd21dSWarner Losh return(0); /* will return later */ 201169acd21dSWarner Losh }; 201269acd21dSWarner Losh 201369acd21dSWarner Losh /* 201469acd21dSWarner Losh * write (or format) operation will fall through and 201569acd21dSWarner Losh * await completion interrupt 201669acd21dSWarner Losh */ 20175b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 20186182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 20195b81b6b3SRodney W. Grimes return (0); /* will return later */ 202069acd21dSWarner Losh case PIOREAD: 202169acd21dSWarner Losh /* 202269acd21dSWarner Losh * actually perform the PIO read. The IOCOMPLETE case 202369acd21dSWarner Losh * removes the timeout for us. 202469acd21dSWarner Losh */ 20258177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 202669acd21dSWarner Losh fdc->state = IOCOMPLETE; 202769acd21dSWarner Losh /* FALLTHROUGH */ 20285b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 20296182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 2030dc5df763SJoerg Wunsch 20316182fdbdSPeter Wemm if (fd_read_status(fdc, fd->fdsu)) { 20325f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 20338177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20348177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 20355c1a1eaeSBruce Evans fdc->dmachan); 2036dc5df763SJoerg Wunsch if (fdc->retry < 6) 2037dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 20386182fdbdSPeter Wemm return (retrier(fdc)); 20395b81b6b3SRodney W. Grimes } 2040dc5df763SJoerg Wunsch 20413a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 2042dc5df763SJoerg Wunsch 20433a2f7427SDavid Greenman /* FALLTHROUGH */ 2044dc5df763SJoerg Wunsch 20453a2f7427SDavid Greenman case IOTIMEDOUT: 20463b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 20478177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20488177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 20496182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 20503a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20513a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 2052b39c878eSAndrey A. Chernov /* 20533a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 20543a2f7427SDavid Greenman * and didn't release it in time for the 20553a2f7427SDavid Greenman * next FDC transfer. 20563a2f7427SDavid Greenman * Just restart it, don't increment retry 20573a2f7427SDavid Greenman * count. (vak) 2058b39c878eSAndrey A. Chernov */ 2059b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2060b39c878eSAndrey A. Chernov return (1); 2061b39c878eSAndrey A. Chernov } 20623a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 20633a2f7427SDavid Greenman && fdc->retry < 6) 20643a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 20653a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20663a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 20673a2f7427SDavid Greenman && fdc->retry < 3) 20683a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 20696182fdbdSPeter Wemm return (retrier(fdc)); 20705b81b6b3SRodney W. Grimes } 20715b81b6b3SRodney W. Grimes /* All OK */ 20723a2f7427SDavid Greenman fd->skip += fdblk; 20738177437dSPoul-Henning Kamp if (!format && fd->skip < bp->bio_bcount - bp->bio_resid) { 20745b81b6b3SRodney W. Grimes /* set up next transfer */ 20755b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20766182fdbdSPeter Wemm } else { 20775b81b6b3SRodney W. Grimes /* ALL DONE */ 20785b81b6b3SRodney W. Grimes fd->skip = 0; 2079e93e63cbSBruce Evans fdc->bp = NULL; 20805f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2081a468031cSPoul-Henning Kamp biofinish(bp, &fd->device_stats, 0); 20825b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20835b81b6b3SRodney W. Grimes fdc->fdu = -1; 20845b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 20855b81b6b3SRodney W. Grimes } 20865b81b6b3SRodney W. Grimes return (1); 20875b81b6b3SRodney W. Grimes case RESETCTLR: 20883a2f7427SDavid Greenman fdc_reset(fdc); 20895b81b6b3SRodney W. Grimes fdc->retry++; 20905c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 20915c1a1eaeSBruce Evans return (0); 20925c1a1eaeSBruce Evans case RESETCOMPLETE: 20935c1a1eaeSBruce Evans /* 20945c1a1eaeSBruce Evans * Discard all the results from the reset so that they 20955c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 20965c1a1eaeSBruce Evans */ 20970e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 20980e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 20995c1a1eaeSBruce Evans fdc->state = STARTRECAL; 21005c1a1eaeSBruce Evans /* Fall through. */ 21015c1a1eaeSBruce Evans case STARTRECAL: 21026182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2103dc8603e3SJoerg Wunsch /* arrgl */ 2104dc8603e3SJoerg Wunsch fdc->retry = 6; 21056182fdbdSPeter Wemm return (retrier(fdc)); 2106dc8603e3SJoerg Wunsch } 21075b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 21085b81b6b3SRodney W. Grimes return (0); /* will return later */ 21095b81b6b3SRodney W. Grimes case RECALWAIT: 21105b81b6b3SRodney W. Grimes /* allow heads to settle */ 21116182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 21125b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 21135b81b6b3SRodney W. Grimes return (0); /* will return later */ 21145b81b6b3SRodney W. Grimes case RECALCOMPLETE: 21153a2f7427SDavid Greenman do { 21163a2f7427SDavid Greenman /* 2117dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 21183a2f7427SDavid Greenman */ 2119dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2120dc5df763SJoerg Wunsch return 0; 2121dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2122dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2123dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 21243a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 21253a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 21265b81b6b3SRodney W. Grimes { 2127dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2128dc8603e3SJoerg Wunsch /* 2129dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 2130dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2131dc8603e3SJoerg Wunsch * since people used to complain much about 2132dc8603e3SJoerg Wunsch * the failure message, try not logging 2133dc8603e3SJoerg Wunsch * this one if it seems to be the first 2134dc8603e3SJoerg Wunsch * time in a line 2135dc8603e3SJoerg Wunsch */ 2136dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2137dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 21383a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 21396182fdbdSPeter Wemm return (retrier(fdc)); 21405b81b6b3SRodney W. Grimes } 21415b81b6b3SRodney W. Grimes fd->track = 0; 21425b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 21435b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 21445b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21455b81b6b3SRodney W. Grimes case MOTORWAIT: 21465b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 21475b81b6b3SRodney W. Grimes { 21485b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 21495b81b6b3SRodney W. Grimes } 21505c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 21515c1a1eaeSBruce Evans fdc->state = RESETCTLR; 21525c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 21535c1a1eaeSBruce Evans } else { 21545e235068SJordan K. Hubbard /* 21555c1a1eaeSBruce Evans * If all motors were off, then the controller was 21565c1a1eaeSBruce Evans * reset, so it has lost track of the current 21575c1a1eaeSBruce Evans * cylinder. Recalibrate to handle this case. 2158f86e4077SBruce Evans * But first, discard the results of the reset. 21595e235068SJordan K. Hubbard */ 2160f86e4077SBruce Evans fdc->state = RESETCOMPLETE; 21615c1a1eaeSBruce Evans } 21625b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21635b81b6b3SRodney W. Grimes default: 2164b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 2165dc5df763SJoerg Wunsch if (fd_read_status(fdc, fd->fdsu) == 0) 2166a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 21675b81b6b3SRodney W. Grimes fdc->status[0], 21685b81b6b3SRodney W. Grimes fdc->status[1], 21695b81b6b3SRodney W. Grimes fdc->status[2], 21705b81b6b3SRodney W. Grimes fdc->status[3], 21715b81b6b3SRodney W. Grimes fdc->status[4], 21725b81b6b3SRodney W. Grimes fdc->status[5], 21735b81b6b3SRodney W. Grimes fdc->status[6] ); 21743a2f7427SDavid Greenman else 2175dac0f2dbSJoerg Wunsch printf("No status available "); 2176dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2177dac0f2dbSJoerg Wunsch { 2178dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 21795b81b6b3SRodney W. Grimes return (0); 21805b81b6b3SRodney W. Grimes } 2181dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2182dac0f2dbSJoerg Wunsch return (0); 2183dac0f2dbSJoerg Wunsch } 2184dac0f2dbSJoerg Wunsch /*XXX confusing: some branches return immediately, others end up here*/ 21855b81b6b3SRodney W. Grimes return (1); /* Come back immediatly to new state */ 21865b81b6b3SRodney W. Grimes } 21875b81b6b3SRodney W. Grimes 2188aaf08d94SGarrett Wollman static int 21896182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 21905b81b6b3SRodney W. Grimes { 21918177437dSPoul-Henning Kamp struct bio *bp; 21926182fdbdSPeter Wemm struct fd_data *fd; 21936182fdbdSPeter Wemm int fdu; 21945b81b6b3SRodney W. Grimes 2195e93e63cbSBruce Evans bp = fdc->bp; 21965b81b6b3SRodney W. Grimes 21976182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 21988177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 21996182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22006182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 22013a2f7427SDavid Greenman goto fail; 22026182fdbdSPeter Wemm 22036182fdbdSPeter Wemm switch (fdc->retry) { 22045b81b6b3SRodney W. Grimes case 0: case 1: case 2: 22055b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 22065b81b6b3SRodney W. Grimes break; 22075b81b6b3SRodney W. Grimes case 3: case 4: case 5: 22085b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 22095b81b6b3SRodney W. Grimes break; 22105b81b6b3SRodney W. Grimes case 6: 22115b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 22125b81b6b3SRodney W. Grimes break; 22135b81b6b3SRodney W. Grimes case 7: 22145b81b6b3SRodney W. Grimes break; 22155b81b6b3SRodney W. Grimes default: 22163a2f7427SDavid Greenman fail: 22175b81b6b3SRodney W. Grimes { 22182995d110SJoerg Wunsch int printerror = (fd->options & FDOPT_NOERRLOG) == 0; 22192995d110SJoerg Wunsch 22202995d110SJoerg Wunsch if (printerror) 2221cb7e609aSPoul-Henning Kamp diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE, 22223a2f7427SDavid Greenman (struct disklabel *)NULL); 22232995d110SJoerg Wunsch if (printerror) { 2224dc5df763SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) 2225dc5df763SJoerg Wunsch { 2226dc5df763SJoerg Wunsch printf( 2227a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2228dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2229dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2230dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2231dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2232dc5df763SJoerg Wunsch fdc->status[5]); 2233dc5df763SJoerg Wunsch } 2234dc5df763SJoerg Wunsch else 2235dc5df763SJoerg Wunsch printf(" (No status)\n"); 22365b81b6b3SRodney W. Grimes } 22372995d110SJoerg Wunsch } 22382995d110SJoerg Wunsch if ((fd->options & FDOPT_NOERROR) == 0) { 22398177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22408177437dSPoul-Henning Kamp bp->bio_error = EIO; 22418177437dSPoul-Henning Kamp bp->bio_resid += bp->bio_bcount - fdc->fd->skip; 22422995d110SJoerg Wunsch } 2243e93e63cbSBruce Evans fdc->bp = NULL; 22445b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 22455f830ea2SJoerg Wunsch device_unbusy(fd->dev); 2246a468031cSPoul-Henning Kamp biofinish(bp, &fdc->fd->device_stats, 0); 224792ed385aSRodney W. Grimes fdc->state = FINDWORK; 22485c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 22495b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 22505b81b6b3SRodney W. Grimes fdc->fdu = -1; 225192ed385aSRodney W. Grimes return (1); 22525b81b6b3SRodney W. Grimes } 22535b81b6b3SRodney W. Grimes fdc->retry++; 22545b81b6b3SRodney W. Grimes return (1); 22555b81b6b3SRodney W. Grimes } 22565b81b6b3SRodney W. Grimes 22571fdb6e6cSPoul-Henning Kamp static void 22581fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp) 22591fdb6e6cSPoul-Henning Kamp { 22601fdb6e6cSPoul-Henning Kamp wakeup(bp); 22611fdb6e6cSPoul-Henning Kamp } 22621fdb6e6cSPoul-Henning Kamp 2263b39c878eSAndrey A. Chernov static int 2264b39c878eSAndrey A. Chernov fdformat(dev, finfo, p) 2265b39c878eSAndrey A. Chernov dev_t dev; 2266b39c878eSAndrey A. Chernov struct fd_formb *finfo; 2267b39c878eSAndrey A. Chernov struct proc *p; 2268b39c878eSAndrey A. Chernov { 2269b39c878eSAndrey A. Chernov fdu_t fdu; 2270b39c878eSAndrey A. Chernov fd_p fd; 2271b39c878eSAndrey A. Chernov 22721fdb6e6cSPoul-Henning Kamp struct bio *bp; 2273b39c878eSAndrey A. Chernov int rv = 0, s; 22743a2f7427SDavid Greenman size_t fdblk; 2275b39c878eSAndrey A. Chernov 2276b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 22776182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22783a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2279b39c878eSAndrey A. Chernov 2280b39c878eSAndrey A. Chernov /* set up a buffer header for fdstrategy() */ 22811fdb6e6cSPoul-Henning Kamp bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT); 2282b39c878eSAndrey A. Chernov if(bp == 0) 22831fdb6e6cSPoul-Henning Kamp return ENOMEM; 228482f5379bSJoerg Wunsch /* 228582f5379bSJoerg Wunsch * keep the process from being swapped 228682f5379bSJoerg Wunsch */ 22872ada239cSPeter Wemm PHOLD(p); 22881fdb6e6cSPoul-Henning Kamp bzero((void *)bp, sizeof(*bp)); 22891fdb6e6cSPoul-Henning Kamp bp->bio_cmd = BIO_FORMAT; 2290b39c878eSAndrey A. Chernov 2291b39c878eSAndrey A. Chernov /* 2292b39c878eSAndrey A. Chernov * calculate a fake blkno, so fdstrategy() would initiate a 2293b39c878eSAndrey A. Chernov * seek to the requested cylinder 2294b39c878eSAndrey A. Chernov */ 22951fdb6e6cSPoul-Henning Kamp bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 22963a2f7427SDavid Greenman + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 2297b39c878eSAndrey A. Chernov 22981fdb6e6cSPoul-Henning Kamp bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 22991fdb6e6cSPoul-Henning Kamp bp->bio_data = (caddr_t)finfo; 2300b39c878eSAndrey A. Chernov 2301b39c878eSAndrey A. Chernov /* now do the format */ 23021fdb6e6cSPoul-Henning Kamp bp->bio_dev = dev; 23031fdb6e6cSPoul-Henning Kamp bp->bio_done = fdbiodone; 23041fdb6e6cSPoul-Henning Kamp fdstrategy(bp); 2305b39c878eSAndrey A. Chernov 2306b39c878eSAndrey A. Chernov /* ...and wait for it to complete */ 2307b39c878eSAndrey A. Chernov s = splbio(); 23081fdb6e6cSPoul-Henning Kamp while(!(bp->bio_flags & BIO_DONE)) { 23095e235068SJordan K. Hubbard rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 2310b39c878eSAndrey A. Chernov if (rv == EWOULDBLOCK) 2311b39c878eSAndrey A. Chernov break; 2312b39c878eSAndrey A. Chernov } 2313b39c878eSAndrey A. Chernov splx(s); 2314b39c878eSAndrey A. Chernov 231582f5379bSJoerg Wunsch if (rv == EWOULDBLOCK) { 2316b39c878eSAndrey A. Chernov /* timed out */ 2317b39c878eSAndrey A. Chernov rv = EIO; 23185f830ea2SJoerg Wunsch device_unbusy(fd->dev); 231982f5379bSJoerg Wunsch } 23201fdb6e6cSPoul-Henning Kamp if (bp->bio_flags & BIO_ERROR) 23211fdb6e6cSPoul-Henning Kamp rv = bp->bio_error; 232282f5379bSJoerg Wunsch /* 232382f5379bSJoerg Wunsch * allow the process to be swapped 232482f5379bSJoerg Wunsch */ 23252ada239cSPeter Wemm PRELE(p); 2326b39c878eSAndrey A. Chernov free(bp, M_TEMP); 2327b39c878eSAndrey A. Chernov return rv; 2328b39c878eSAndrey A. Chernov } 2329b39c878eSAndrey A. Chernov 2330f5f7ba03SJordan K. Hubbard /* 2331671e2ceeSBruce Evans * TODO: don't allocate buffer on stack. 2332f5f7ba03SJordan K. Hubbard */ 23335b81b6b3SRodney W. Grimes 23343e425b96SJulian Elischer static int 2335b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p) 2336f5f7ba03SJordan K. Hubbard dev_t dev; 2337ecbb00a2SDoug Rabson u_long cmd; 2338f5f7ba03SJordan K. Hubbard caddr_t addr; 2339f5f7ba03SJordan K. Hubbard int flag; 2340b39c878eSAndrey A. Chernov struct proc *p; 2341f5f7ba03SJordan K. Hubbard { 23423a2f7427SDavid Greenman fdu_t fdu = FDUNIT(minor(dev)); 23436182fdbdSPeter Wemm fd_p fd = devclass_get_softc(fd_devclass, fdu); 23443a2f7427SDavid Greenman size_t fdblk; 23453a2f7427SDavid Greenman 2346f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2347f5f7ba03SJordan K. Hubbard struct disklabel *dl; 23482995d110SJoerg Wunsch struct fdc_status *fsp; 2349f5f7ba03SJordan K. Hubbard char buffer[DEV_BSIZE]; 23503a2f7427SDavid Greenman int error = 0; 2351f5f7ba03SJordan K. Hubbard 23523a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2353f5f7ba03SJordan K. Hubbard 23546182fdbdSPeter Wemm switch (cmd) { 2355f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2356f5f7ba03SJordan K. Hubbard bzero(buffer, sizeof (buffer)); 2357f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 23583a2f7427SDavid Greenman dl->d_secsize = fdblk; 23596182fdbdSPeter Wemm fdt = fd->ft; 2360f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 2361f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 2362f5f7ba03SJordan K. Hubbard 236349ff4debSPoul-Henning Kamp if (readdisklabel(dkmodpart(dev, RAW_PART), dl) 2364191e1a59SBruce Evans == NULL) 2365f5f7ba03SJordan K. Hubbard error = 0; 2366f5f7ba03SJordan K. Hubbard else 2367f5f7ba03SJordan K. Hubbard error = EINVAL; 2368f5f7ba03SJordan K. Hubbard 2369f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 2370f5f7ba03SJordan K. Hubbard break; 2371f5f7ba03SJordan K. Hubbard 2372f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2373f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2374f5f7ba03SJordan K. Hubbard error = EBADF; 2375f5f7ba03SJordan K. Hubbard break; 2376f5f7ba03SJordan K. Hubbard 2377f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2378f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2379f5f7ba03SJordan K. Hubbard error = EBADF; 2380f5f7ba03SJordan K. Hubbard break; 2381f5f7ba03SJordan K. Hubbard 2382f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 23836182fdbdSPeter Wemm if ((flag & FWRITE) == 0) { 2384f5f7ba03SJordan K. Hubbard error = EBADF; 2385f5f7ba03SJordan K. Hubbard break; 2386f5f7ba03SJordan K. Hubbard } 2387f5f7ba03SJordan K. Hubbard 2388f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 2389f5f7ba03SJordan K. Hubbard 2390191e1a59SBruce Evans if ((error = setdisklabel((struct disklabel *)buffer, dl, 2391191e1a59SBruce Evans (u_long)0)) != 0) 2392f5f7ba03SJordan K. Hubbard break; 2393f5f7ba03SJordan K. Hubbard 239449ff4debSPoul-Henning Kamp error = writedisklabel(dev, (struct disklabel *)buffer); 2395b39c878eSAndrey A. Chernov break; 2396b39c878eSAndrey A. Chernov case FD_FORM: 2397b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2398b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 2399b39c878eSAndrey A. Chernov else if (((struct fd_formb *)addr)->format_version != 2400b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2401b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 2402b39c878eSAndrey A. Chernov else 2403b39c878eSAndrey A. Chernov error = fdformat(dev, (struct fd_formb *)addr, p); 2404b39c878eSAndrey A. Chernov break; 2405b39c878eSAndrey A. Chernov 2406b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 24073e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2408f5f7ba03SJordan K. Hubbard break; 2409f5f7ba03SJordan K. Hubbard 24103a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 24113a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 2412f711d546SPoul-Henning Kamp if (suser(p) != 0) 24133a2f7427SDavid Greenman return EPERM; 24143e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 24153a2f7427SDavid Greenman break; 24163a2f7427SDavid Greenman 24173a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 24183e425b96SJulian Elischer *(int *)addr = fd->options; 24193a2f7427SDavid Greenman break; 24203a2f7427SDavid Greenman 24213a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 24223e425b96SJulian Elischer fd->options = *(int *)addr; 24233a2f7427SDavid Greenman break; 24243a2f7427SDavid Greenman 24252995d110SJoerg Wunsch case FD_CLRERR: 24262995d110SJoerg Wunsch if (suser(p) != 0) 24272995d110SJoerg Wunsch return EPERM; 24282995d110SJoerg Wunsch fd->fdc->fdc_errs = 0; 24292995d110SJoerg Wunsch break; 24302995d110SJoerg Wunsch 24312995d110SJoerg Wunsch case FD_GSTAT: 24322995d110SJoerg Wunsch fsp = (struct fdc_status *)addr; 24332995d110SJoerg Wunsch if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 24342995d110SJoerg Wunsch return EINVAL; 24352995d110SJoerg Wunsch memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 24362995d110SJoerg Wunsch break; 24372995d110SJoerg Wunsch 2438f5f7ba03SJordan K. Hubbard default: 24393a2f7427SDavid Greenman error = ENOTTY; 2440f5f7ba03SJordan K. Hubbard break; 2441f5f7ba03SJordan K. Hubbard } 2442f5f7ba03SJordan K. Hubbard return (error); 2443f5f7ba03SJordan K. Hubbard } 2444f5f7ba03SJordan K. Hubbard 24453a2f7427SDavid Greenman /* 24463a2f7427SDavid Greenman * Hello emacs, these are the 24473a2f7427SDavid Greenman * Local Variables: 24483a2f7427SDavid Greenman * c-indent-level: 8 24493a2f7427SDavid Greenman * c-continued-statement-offset: 8 24503a2f7427SDavid Greenman * c-continued-brace-offset: 0 24513a2f7427SDavid Greenman * c-brace-offset: -8 24523a2f7427SDavid Greenman * c-brace-imaginary-offset: 0 24533a2f7427SDavid Greenman * c-argdecl-indent: 8 24543a2f7427SDavid Greenman * c-label-offset: -8 24553a2f7427SDavid Greenman * c++-hanging-braces: 1 24563a2f7427SDavid Greenman * c++-access-specifier-offset: -8 24573a2f7427SDavid Greenman * c++-empty-arglist-indent: 8 24583a2f7427SDavid Greenman * c++-friend-offset: 0 24593a2f7427SDavid Greenman * End: 24603a2f7427SDavid Greenman */ 2461