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 * 215b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 225b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 235b81b6b3SRodney W. Grimes * are met: 245b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 255b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 265b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 275b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 285b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 295b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 305b81b6b3SRodney W. Grimes * must display the following acknowledgement: 315b81b6b3SRodney W. Grimes * This product includes software developed by the University of 325b81b6b3SRodney W. Grimes * California, Berkeley and its contributors. 335b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 345b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 355b81b6b3SRodney W. Grimes * without specific prior written permission. 365b81b6b3SRodney W. Grimes * 375b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 385b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 395b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 405b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 415b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 425b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 435b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 445b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 455b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 465b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 475b81b6b3SRodney W. Grimes * SUCH DAMAGE. 485b81b6b3SRodney W. Grimes * 49dc4ff321SRodney W. Grimes * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 50c3aac50fSPeter Wemm * $FreeBSD$ 515b81b6b3SRodney W. Grimes * 525b81b6b3SRodney W. Grimes */ 535b81b6b3SRodney W. Grimes 54d2fb4892SJoerg Wunsch #include "opt_fdc.h" 555f830ea2SJoerg Wunsch #include "card.h" 565b81b6b3SRodney W. Grimes 57b99f0a4aSAndrew Moore #include <sys/param.h> 58b99f0a4aSAndrew Moore #include <sys/systm.h> 59b99f0a4aSAndrew Moore #include <sys/kernel.h> 609626b608SPoul-Henning Kamp #include <sys/bio.h> 616182fdbdSPeter Wemm #include <sys/bus.h> 626182fdbdSPeter Wemm #include <sys/conf.h> 636182fdbdSPeter Wemm #include <sys/disklabel.h> 64b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h> 656182fdbdSPeter Wemm #include <sys/fcntl.h> 66b99f0a4aSAndrew Moore #include <sys/malloc.h> 676182fdbdSPeter Wemm #include <sys/module.h> 683a2f7427SDavid Greenman #include <sys/proc.h> 69b99f0a4aSAndrew Moore #include <sys/syslog.h> 706182fdbdSPeter Wemm 716182fdbdSPeter Wemm #include <sys/bus.h> 726182fdbdSPeter Wemm #include <machine/bus.h> 736182fdbdSPeter Wemm #include <sys/rman.h> 746182fdbdSPeter Wemm 756182fdbdSPeter Wemm #include <machine/clock.h> 766182fdbdSPeter Wemm #include <machine/ioctl_fd.h> 776182fdbdSPeter Wemm #include <machine/resource.h> 78dc5df763SJoerg Wunsch #include <machine/stdarg.h> 796182fdbdSPeter Wemm 806182fdbdSPeter Wemm #include <isa/isavar.h> 81a97c75b7SDoug Rabson #include <isa/isareg.h> 82a97c75b7SDoug Rabson #include <isa/fdreg.h> 83a97c75b7SDoug Rabson #include <isa/fdc.h> 84a97c75b7SDoug Rabson #include <isa/rtc.h> 856182fdbdSPeter Wemm 86b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */ 875b81b6b3SRodney W. Grimes 880722d6abSJoerg Wunsch /* configuration flags */ 890722d6abSJoerg Wunsch #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 90e34c71eaSJoerg Wunsch #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 910722d6abSJoerg Wunsch 920722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */ 930722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED 0x1000 940722d6abSJoerg Wunsch 95dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 96dc5df763SJoerg Wunsch #define FD_FAILED -1 97dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 98dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 99dc5df763SJoerg Wunsch 1006fb89845SKATO Takenori #define NUMTYPES 17 1016fb89845SKATO Takenori #define NUMDENS (NUMTYPES - 7) 1027ca0641bSAndrey A. Chernov 1033a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */ 104b99f0a4aSAndrew Moore #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 105b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 106b99f0a4aSAndrew Moore #define FD_1720 1 107b99f0a4aSAndrew Moore #define FD_1480 2 108b99f0a4aSAndrew Moore #define FD_1440 3 109b99f0a4aSAndrew Moore #define FD_1200 4 110b99f0a4aSAndrew Moore #define FD_820 5 111b99f0a4aSAndrew Moore #define FD_800 6 112b99f0a4aSAndrew Moore #define FD_720 7 113b99f0a4aSAndrew Moore #define FD_360 8 1146fb89845SKATO Takenori #define FD_640 9 1156fb89845SKATO Takenori #define FD_1232 10 116ed2fa05eSAndrey A. Chernov 1176fb89845SKATO Takenori #define FD_1480in5_25 11 1186fb89845SKATO Takenori #define FD_1440in5_25 12 1196fb89845SKATO Takenori #define FD_820in5_25 13 1206fb89845SKATO Takenori #define FD_800in5_25 14 1216fb89845SKATO Takenori #define FD_720in5_25 15 1226fb89845SKATO Takenori #define FD_360in5_25 16 1236fb89845SKATO Takenori #define FD_640in5_25 17 124b99f0a4aSAndrew Moore 1257ca0641bSAndrey A. Chernov 1266f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] = 1275b81b6b3SRodney W. Grimes { 128126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 129126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 130126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 131126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 132126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 133126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 134126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 135b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 1366fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */ 1376fb89845SKATO Takenori { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */ 138ed2fa05eSAndrey A. Chernov 139126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 140126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 141126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 142126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 143126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 144126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 1456fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */ 1465b81b6b3SRodney W. Grimes }; 1475b81b6b3SRodney W. Grimes 148b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 149dc16046fSJoerg Wunsch 1505b81b6b3SRodney W. Grimes /***********************************************************************\ 1515b81b6b3SRodney W. Grimes * Per controller structure. * 1525b81b6b3SRodney W. Grimes \***********************************************************************/ 1536182fdbdSPeter Wemm static devclass_t fdc_devclass; 1545b81b6b3SRodney W. Grimes 1555b81b6b3SRodney W. Grimes /***********************************************************************\ 1565b81b6b3SRodney W. Grimes * Per drive structure. * 157b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 1585b81b6b3SRodney W. Grimes \***********************************************************************/ 1596182fdbdSPeter Wemm struct fd_data { 160b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 1615b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 1623a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 1635b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 1645b81b6b3SRodney W. Grimes int flags; 1655b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 1665b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 1675b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 1685b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 1695b81b6b3SRodney W. Grimes int skip; 1705b81b6b3SRodney W. Grimes int hddrv; 171dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 1725b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 1733a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 17402a19910SJustin T. Gibbs struct callout_handle toffhandle; 17502a19910SJustin T. Gibbs struct callout_handle tohandle; 176b2dfb1f9SJustin T. Gibbs struct devstat device_stats; 1776182fdbdSPeter Wemm device_t dev; 1786182fdbdSPeter Wemm fdu_t fdu; 1796182fdbdSPeter Wemm }; 18037286586SPeter Wemm 18137286586SPeter Wemm struct fdc_ivars { 18237286586SPeter Wemm int fdunit; 18337286586SPeter Wemm }; 1846182fdbdSPeter Wemm static devclass_t fd_devclass; 1855b81b6b3SRodney W. Grimes 1865b81b6b3SRodney W. Grimes /***********************************************************************\ 1875b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 1885b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 1895b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 1905b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 1915b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 1925b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 1935b81b6b3SRodney W. Grimes \***********************************************************************/ 194b99f0a4aSAndrew Moore 1953a2f7427SDavid Greenman /* needed for ft driver, thus exported */ 1966182fdbdSPeter Wemm int in_fdc(struct fdc_data *); 1976182fdbdSPeter Wemm int out_fdc(struct fdc_data *, int); 1983a2f7427SDavid Greenman 1993a2f7427SDavid Greenman /* internal functions */ 2006182fdbdSPeter Wemm static void fdc_intr(void *); 2016182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 2023a2f7427SDavid Greenman # define TURNON 1 2033a2f7427SDavid Greenman # define TURNOFF 0 2043a2f7427SDavid Greenman static timeout_t fd_turnoff; 2053a2f7427SDavid Greenman static timeout_t fd_motor_on; 2066182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 2073a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2086182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 2096182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 2105c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 2113a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2126182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 2136182fdbdSPeter Wemm static int retrier(struct fdc_data *); 2143a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *); 2153a2f7427SDavid Greenman 216d66c374fSTor Egge static int enable_fifo(fdc_p fdc); 217d66c374fSTor Egge 218d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 219d66c374fSTor Egge 220aaf08d94SGarrett Wollman 2215b81b6b3SRodney W. Grimes #define DEVIDLE 0 2225b81b6b3SRodney W. Grimes #define FINDWORK 1 2235b81b6b3SRodney W. Grimes #define DOSEEK 2 2245b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 3 2255b81b6b3SRodney W. Grimes #define IOCOMPLETE 4 2265b81b6b3SRodney W. Grimes #define RECALCOMPLETE 5 2275b81b6b3SRodney W. Grimes #define STARTRECAL 6 2285b81b6b3SRodney W. Grimes #define RESETCTLR 7 2295b81b6b3SRodney W. Grimes #define SEEKWAIT 8 2305b81b6b3SRodney W. Grimes #define RECALWAIT 9 2315b81b6b3SRodney W. Grimes #define MOTORWAIT 10 2325b81b6b3SRodney W. Grimes #define IOTIMEDOUT 11 2335c1a1eaeSBruce Evans #define RESETCOMPLETE 12 23469acd21dSWarner Losh #define PIOREAD 13 2355b81b6b3SRodney W. Grimes 236d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 237cba2a7c6SBruce Evans static char const * const fdstates[] = 2385b81b6b3SRodney W. Grimes { 2395b81b6b3SRodney W. Grimes "DEVIDLE", 2405b81b6b3SRodney W. Grimes "FINDWORK", 2415b81b6b3SRodney W. Grimes "DOSEEK", 2425b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 2435b81b6b3SRodney W. Grimes "IOCOMPLETE", 2445b81b6b3SRodney W. Grimes "RECALCOMPLETE", 2455b81b6b3SRodney W. Grimes "STARTRECAL", 2465b81b6b3SRodney W. Grimes "RESETCTLR", 2475b81b6b3SRodney W. Grimes "SEEKWAIT", 2485b81b6b3SRodney W. Grimes "RECALWAIT", 2495b81b6b3SRodney W. Grimes "MOTORWAIT", 2505c1a1eaeSBruce Evans "IOTIMEDOUT", 2515c1a1eaeSBruce Evans "RESETCOMPLETE", 252ff9607b0SBruce Evans "PIOREAD", 2535b81b6b3SRodney W. Grimes }; 2545b81b6b3SRodney W. Grimes 2553a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 256cba2a7c6SBruce Evans static int volatile fd_debug = 0; 2575b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 2585b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 259d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 2605b81b6b3SRodney W. Grimes #define TRACE0(arg) 2615b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 262d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 2635b81b6b3SRodney W. Grimes 264427ccf00SDoug Rabson static void 265427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 266427ccf00SDoug Rabson { 267427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 268427ccf00SDoug Rabson } 269427ccf00SDoug Rabson 270427ccf00SDoug Rabson static u_int8_t 271427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 272427ccf00SDoug Rabson { 273427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 274427ccf00SDoug Rabson } 275427ccf00SDoug Rabson 276427ccf00SDoug Rabson static void 277427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 278427ccf00SDoug Rabson { 279427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 280427ccf00SDoug Rabson } 281427ccf00SDoug Rabson 282427ccf00SDoug Rabson static u_int8_t 283427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 284427ccf00SDoug Rabson { 285427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 286427ccf00SDoug Rabson } 287427ccf00SDoug Rabson 288427ccf00SDoug Rabson static void 2895f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 290427ccf00SDoug Rabson { 291427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 292427ccf00SDoug Rabson } 293427ccf00SDoug Rabson 29458c9d623SPeter Wemm #if NCARD > 0 2955f830ea2SJoerg Wunsch static void 2965f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 2975f830ea2SJoerg Wunsch { 2985f830ea2SJoerg Wunsch bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 2995f830ea2SJoerg Wunsch } 30058c9d623SPeter Wemm #endif 3015f830ea2SJoerg Wunsch 302427ccf00SDoug Rabson #if 0 303427ccf00SDoug Rabson 304427ccf00SDoug Rabson static u_int8_t 305427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 306427ccf00SDoug Rabson { 307427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 308427ccf00SDoug Rabson } 309427ccf00SDoug Rabson 310427ccf00SDoug Rabson #endif 311427ccf00SDoug Rabson 31287f6c662SJulian Elischer static d_open_t Fdopen; /* NOTE, not fdopen */ 31387f6c662SJulian Elischer static d_close_t fdclose; 31487f6c662SJulian Elischer static d_ioctl_t fdioctl; 31587f6c662SJulian Elischer static d_strategy_t fdstrategy; 31687f6c662SJulian Elischer 31787f6c662SJulian Elischer #define CDEV_MAJOR 9 31887f6c662SJulian Elischer #define BDEV_MAJOR 2 319f7ea2f55SJulian Elischer 3204e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 3214e2f199eSPoul-Henning Kamp /* open */ Fdopen, 3224e2f199eSPoul-Henning Kamp /* close */ fdclose, 3234e2f199eSPoul-Henning Kamp /* read */ physread, 3244e2f199eSPoul-Henning Kamp /* write */ physwrite, 3254e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 3264e2f199eSPoul-Henning Kamp /* poll */ nopoll, 3274e2f199eSPoul-Henning Kamp /* mmap */ nommap, 3284e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 3294e2f199eSPoul-Henning Kamp /* name */ "fd", 3304e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 3314e2f199eSPoul-Henning Kamp /* dump */ nodump, 3324e2f199eSPoul-Henning Kamp /* psize */ nopsize, 3334e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 3344e2f199eSPoul-Henning Kamp /* bmaj */ BDEV_MAJOR 3354e2f199eSPoul-Henning Kamp }; 3364e2f199eSPoul-Henning Kamp 337dc5df763SJoerg Wunsch static int 3386182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 339dc5df763SJoerg Wunsch { 3406182fdbdSPeter Wemm fdc->fdc_errs++; 34116b04b6aSJoerg Wunsch if (s) { 342b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 343b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 344b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 345b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 346b6e5f28eSPeter Wemm "logging any more\n"); 34716b04b6aSJoerg Wunsch } 348dc5df763SJoerg Wunsch 349dc5df763SJoerg Wunsch return FD_FAILED; 350dc5df763SJoerg Wunsch } 351dc5df763SJoerg Wunsch 352dc5df763SJoerg Wunsch /* 353dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 354dc5df763SJoerg Wunsch * Unit number, 355dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 356dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 357dc5df763SJoerg Wunsch */ 3586f4e0bebSPoul-Henning Kamp static int 3596182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 360dc5df763SJoerg Wunsch { 361dc5df763SJoerg Wunsch u_char cmd; 362dc5df763SJoerg Wunsch int n_in; 363dc5df763SJoerg Wunsch int n; 364dc5df763SJoerg Wunsch va_list ap; 365dc5df763SJoerg Wunsch 366dc5df763SJoerg Wunsch va_start(ap, n_out); 367dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 368dc5df763SJoerg Wunsch va_end(ap); 369dc5df763SJoerg Wunsch va_start(ap, n_out); 370dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 371dc5df763SJoerg Wunsch { 3726182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 373dc5df763SJoerg Wunsch { 374dc5df763SJoerg Wunsch char msg[50]; 3752127f260SArchie Cobbs snprintf(msg, sizeof(msg), 376dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 377dc5df763SJoerg Wunsch cmd, n + 1, n_out); 3786182fdbdSPeter Wemm return fdc_err(fdc, msg); 379dc5df763SJoerg Wunsch } 380dc5df763SJoerg Wunsch } 381dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 382dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 383dc5df763SJoerg Wunsch { 384dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 3856182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 386dc5df763SJoerg Wunsch { 387dc5df763SJoerg Wunsch char msg[50]; 3882127f260SArchie Cobbs snprintf(msg, sizeof(msg), 389dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 390dc5df763SJoerg Wunsch cmd, n + 1, n_in); 3916182fdbdSPeter Wemm return fdc_err(fdc, msg); 392dc5df763SJoerg Wunsch } 393dc5df763SJoerg Wunsch } 394dc5df763SJoerg Wunsch 395dc5df763SJoerg Wunsch return 0; 396dc5df763SJoerg Wunsch } 397dc5df763SJoerg Wunsch 3986f4e0bebSPoul-Henning Kamp static int 399d66c374fSTor Egge enable_fifo(fdc_p fdc) 400d66c374fSTor Egge { 401d66c374fSTor Egge int i, j; 402d66c374fSTor Egge 403d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 404d66c374fSTor Egge 405d66c374fSTor Egge /* 406d66c374fSTor Egge * XXX: 407d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 408d66c374fSTor Egge * this might be an invalid command. Thus we send the 409d66c374fSTor Egge * first byte, and check for an early turn of data directon. 410d66c374fSTor Egge */ 411d66c374fSTor Egge 4126182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 4136182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 414d66c374fSTor Egge 415d66c374fSTor Egge /* If command is invalid, return */ 416d66c374fSTor Egge j = 100000; 417427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 418d66c374fSTor Egge != NE7_RQM && j-- > 0) 419d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 420d66c374fSTor Egge fdc_reset(fdc); 421d66c374fSTor Egge return FD_FAILED; 422d66c374fSTor Egge } 423d66c374fSTor Egge if (j<0 || 4246182fdbdSPeter Wemm fd_cmd(fdc, 3, 425d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 426d66c374fSTor Egge fdc_reset(fdc); 4276182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 428d66c374fSTor Egge } 429d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 430d66c374fSTor Egge return 0; 431d66c374fSTor Egge } 4326182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 433d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 4346182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 435d66c374fSTor Egge return 0; 436d66c374fSTor Egge } 437d66c374fSTor Egge 438d66c374fSTor Egge static int 439dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 440dc5df763SJoerg Wunsch { 441dc5df763SJoerg Wunsch int st3; 442dc5df763SJoerg Wunsch 4436182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 444dc5df763SJoerg Wunsch { 4456182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 446dc5df763SJoerg Wunsch } 447dc5df763SJoerg Wunsch if (st3p) 448dc5df763SJoerg Wunsch *st3p = st3; 449dc5df763SJoerg Wunsch 450dc5df763SJoerg Wunsch return 0; 451dc5df763SJoerg Wunsch } 452dc5df763SJoerg Wunsch 4536f4e0bebSPoul-Henning Kamp static int 454dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 455dc5df763SJoerg Wunsch { 4566182fdbdSPeter Wemm int cyl, st0, ret; 457dc5df763SJoerg Wunsch 4586182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 4596182fdbdSPeter Wemm if (ret) { 4606182fdbdSPeter Wemm (void)fdc_err(fdc, 461dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 462dc5df763SJoerg Wunsch return ret; 463dc5df763SJoerg Wunsch } 464dc5df763SJoerg Wunsch 465dc5df763SJoerg Wunsch if (st0p) 466dc5df763SJoerg Wunsch *st0p = st0; 467dc5df763SJoerg Wunsch 4686182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 469dc5df763SJoerg Wunsch /* 470dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 471dc5df763SJoerg Wunsch */ 472dc5df763SJoerg Wunsch return FD_NOT_VALID; 473dc5df763SJoerg Wunsch } 474dc5df763SJoerg Wunsch 4756182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 4766182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 477dc5df763SJoerg Wunsch } 478dc5df763SJoerg Wunsch 479dc5df763SJoerg Wunsch if (cylp) 480dc5df763SJoerg Wunsch *cylp = cyl; 481dc5df763SJoerg Wunsch 482dc5df763SJoerg Wunsch return 0; 483dc5df763SJoerg Wunsch } 484dc5df763SJoerg Wunsch 485dc5df763SJoerg Wunsch 4866f4e0bebSPoul-Henning Kamp static int 487dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu) 488dc5df763SJoerg Wunsch { 489dc5df763SJoerg Wunsch int i, ret; 490b5e8ce9fSBruce Evans 4916182fdbdSPeter Wemm for (i = 0; i < 7; i++) { 492b5e8ce9fSBruce Evans /* 493b5e8ce9fSBruce Evans * XXX types are poorly chosen. Only bytes can by read 494a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 495b5e8ce9fSBruce Evans * fd_in() gives ints. 496b5e8ce9fSBruce Evans */ 497b5e8ce9fSBruce Evans int status; 498b5e8ce9fSBruce Evans 4996182fdbdSPeter Wemm ret = fd_in(fdc, &status); 500b5e8ce9fSBruce Evans fdc->status[i] = status; 501b5e8ce9fSBruce Evans if (ret != 0) 502dc5df763SJoerg Wunsch break; 503dc5df763SJoerg Wunsch } 504dc5df763SJoerg Wunsch 505dc5df763SJoerg Wunsch if (ret == 0) 506dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 507dc5df763SJoerg Wunsch else 508dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 509dc5df763SJoerg Wunsch 510dc5df763SJoerg Wunsch return ret; 511dc5df763SJoerg Wunsch } 512dc5df763SJoerg Wunsch 5135b81b6b3SRodney W. Grimes /****************************************************************************/ 5145b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5155b81b6b3SRodney W. Grimes /****************************************************************************/ 516dc5df763SJoerg Wunsch 5173a2f7427SDavid Greenman static int 51837286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 5195b81b6b3SRodney W. Grimes { 52037286586SPeter Wemm device_t dev; 5215f830ea2SJoerg Wunsch int ispnp, ispcmcia; 5225b81b6b3SRodney W. Grimes 52337286586SPeter Wemm dev = fdc->fdc_dev; 5245f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 5255f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 5266182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 5276182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 5286182fdbdSPeter Wemm 529b6e5f28eSPeter Wemm /* 5305f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 5315f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 5325f830ea2SJoerg Wunsch * register at 0x3f6. 5335f830ea2SJoerg Wunsch * 534b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 5355f830ea2SJoerg Wunsch * 5365f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 5375f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 5385f830ea2SJoerg Wunsch * one with offset 7 as control register. 539b6e5f28eSPeter Wemm */ 5406182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 5416182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 5425f830ea2SJoerg Wunsch ispcmcia ? 8 : (ispnp ? 1 : 6), 5435f830ea2SJoerg Wunsch RF_ACTIVE); 5446182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 545427ccf00SDoug Rabson device_printf(dev, "cannot reserve I/O port range\n"); 54637286586SPeter Wemm return ENXIO; 5476182fdbdSPeter Wemm } 548427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 549427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 550427ccf00SDoug Rabson 5515f830ea2SJoerg Wunsch if (!ispcmcia) { 552427ccf00SDoug Rabson /* 5535f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 5545f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 5555f830ea2SJoerg Wunsch * by checking the size and adjust the port address 5565f830ea2SJoerg Wunsch * accordingly. 557b6e5f28eSPeter Wemm */ 558b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 559b6e5f28eSPeter Wemm fdc->port_off = -2; 560b6e5f28eSPeter Wemm 561b6e5f28eSPeter Wemm /* 5625f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 5635f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 5645f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 565a2639a18SPeter Wemm * 5665f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 5675f830ea2SJoerg Wunsch * leave out the control register! It seems that some 5685f830ea2SJoerg Wunsch * non-antique controller chips have a different 5695f830ea2SJoerg Wunsch * method of programming the transfer speed which 5705f830ea2SJoerg Wunsch * doesn't require the control register, but it's 5715f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 5725f830ea2SJoerg Wunsch * address for the control register. 573427ccf00SDoug Rabson */ 574b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 575b9da888fSPeter Wemm u_long ctlstart; 576b9da888fSPeter Wemm 577b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 5785f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 5795f830ea2SJoerg Wunsch fdc->port_off + 7; 580b6e5f28eSPeter Wemm 581b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 582b9da888fSPeter Wemm } 583b6e5f28eSPeter Wemm 584b6e5f28eSPeter Wemm /* 585b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 586b6e5f28eSPeter Wemm */ 587427ccf00SDoug Rabson fdc->rid_ctl = 1; 5885f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 5895f830ea2SJoerg Wunsch &fdc->rid_ctl, 590b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 591427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 5925f830ea2SJoerg Wunsch device_printf(dev, 5935f830ea2SJoerg Wunsch "cannot reserve control I/O port range\n"); 59437286586SPeter Wemm return ENXIO; 595427ccf00SDoug Rabson } 596427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 597427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 5985f830ea2SJoerg Wunsch } 5996182fdbdSPeter Wemm 6006182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 6016182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 6026182fdbdSPeter Wemm RF_ACTIVE); 6036182fdbdSPeter Wemm if (fdc->res_irq == 0) { 604427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 60537286586SPeter Wemm return ENXIO; 6066182fdbdSPeter Wemm } 6075f830ea2SJoerg Wunsch 6085f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 6096182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 6106182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 6116182fdbdSPeter Wemm RF_ACTIVE); 6126182fdbdSPeter Wemm if (fdc->res_drq == 0) { 613427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 61437286586SPeter Wemm return ENXIO; 6156182fdbdSPeter Wemm } 6166182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 6175f830ea2SJoerg Wunsch } 61837286586SPeter Wemm 61937286586SPeter Wemm return 0; 62037286586SPeter Wemm } 62137286586SPeter Wemm 62237286586SPeter Wemm static void 62337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 62437286586SPeter Wemm { 62537286586SPeter Wemm device_t dev; 62637286586SPeter Wemm 62737286586SPeter Wemm dev = fdc->fdc_dev; 62837286586SPeter Wemm if (fdc->res_irq != 0) { 62937286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 63037286586SPeter Wemm fdc->res_irq); 63137286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 63237286586SPeter Wemm fdc->res_irq); 63337286586SPeter Wemm } 63437286586SPeter Wemm if (fdc->res_ctl != 0) { 63537286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63637286586SPeter Wemm fdc->res_ctl); 63737286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63837286586SPeter Wemm fdc->res_ctl); 63937286586SPeter Wemm } 64037286586SPeter Wemm if (fdc->res_ioport != 0) { 64137286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 64237286586SPeter Wemm fdc->res_ioport); 64337286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 64437286586SPeter Wemm fdc->res_ioport); 64537286586SPeter Wemm } 64637286586SPeter Wemm if (fdc->res_drq != 0) { 64737286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64837286586SPeter Wemm fdc->res_drq); 64937286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 65037286586SPeter Wemm fdc->res_drq); 65137286586SPeter Wemm } 65237286586SPeter Wemm } 65337286586SPeter Wemm 65437286586SPeter Wemm /****************************************************************************/ 65537286586SPeter Wemm /* autoconfiguration stuff */ 65637286586SPeter Wemm /****************************************************************************/ 65737286586SPeter Wemm 65837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 65937286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 66037286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 66137286586SPeter Wemm {0} 66237286586SPeter Wemm }; 66337286586SPeter Wemm 66437286586SPeter Wemm static int 6653aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 66637286586SPeter Wemm { 66737286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 66837286586SPeter Wemm 66937286586SPeter Wemm switch (which) { 67037286586SPeter Wemm case FDC_IVAR_FDUNIT: 67137286586SPeter Wemm *result = ivars->fdunit; 67237286586SPeter Wemm break; 67337286586SPeter Wemm default: 67437286586SPeter Wemm return ENOENT; 67537286586SPeter Wemm } 67637286586SPeter Wemm return 0; 67737286586SPeter Wemm } 67837286586SPeter Wemm 67937286586SPeter Wemm /* 68037286586SPeter Wemm * fdc controller section. 68137286586SPeter Wemm */ 68237286586SPeter Wemm static int 68337286586SPeter Wemm fdc_probe(device_t dev) 68437286586SPeter Wemm { 68537286586SPeter Wemm int error, ic_type; 68637286586SPeter Wemm struct fdc_data *fdc; 68737286586SPeter Wemm 68837286586SPeter Wemm fdc = device_get_softc(dev); 68937286586SPeter Wemm bzero(fdc, sizeof *fdc); 69037286586SPeter Wemm fdc->fdc_dev = dev; 6915f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 69237286586SPeter Wemm 69337286586SPeter Wemm /* Check pnp ids */ 69437286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 69537286586SPeter Wemm if (error == ENXIO) 69637286586SPeter Wemm return ENXIO; 6975f830ea2SJoerg Wunsch if (error == 0) 6985f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 69937286586SPeter Wemm 70037286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 70137286586SPeter Wemm error = fdc_alloc_resources(fdc); 70237286586SPeter Wemm if (error) 70337286586SPeter Wemm goto out; 7045b81b6b3SRodney W. Grimes 70516111cedSAndrew Moore /* First - lets reset the floppy controller */ 706427ccf00SDoug Rabson fdout_wr(fdc, 0); 70716111cedSAndrew Moore DELAY(100); 708427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 70916111cedSAndrew Moore 7105b81b6b3SRodney W. Grimes /* see if it can handle a command */ 7116182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7126182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 7136182fdbdSPeter Wemm error = ENXIO; 7146182fdbdSPeter Wemm goto out; 7155b81b6b3SRodney W. Grimes } 7166182fdbdSPeter Wemm 7176182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 7186182fdbdSPeter Wemm ic_type = (u_char)ic_type; 7196182fdbdSPeter Wemm switch (ic_type) { 7206182fdbdSPeter Wemm case 0x80: 7216182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 7226182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 7236182fdbdSPeter Wemm break; 7246182fdbdSPeter Wemm case 0x81: 7256182fdbdSPeter Wemm device_set_desc(dev, "Intel 82077 or clone"); 7266182fdbdSPeter Wemm fdc->fdct = FDC_I82077; 7276182fdbdSPeter Wemm break; 7286182fdbdSPeter Wemm case 0x90: 7296182fdbdSPeter Wemm device_set_desc(dev, "NEC 72065B or clone"); 7306182fdbdSPeter Wemm fdc->fdct = FDC_NE72065; 7316182fdbdSPeter Wemm break; 7326182fdbdSPeter Wemm default: 7336182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 7346182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 7356182fdbdSPeter Wemm break; 7366182fdbdSPeter Wemm } 7376182fdbdSPeter Wemm } 7386182fdbdSPeter Wemm 7396182fdbdSPeter Wemm out: 74037286586SPeter Wemm fdc_release_resources(fdc); 7416182fdbdSPeter Wemm return (error); 7425b81b6b3SRodney W. Grimes } 7435b81b6b3SRodney W. Grimes 7445f830ea2SJoerg Wunsch #if NCARD > 0 7455f830ea2SJoerg Wunsch 7465f830ea2SJoerg Wunsch static int 7475f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 7485f830ea2SJoerg Wunsch { 7495f830ea2SJoerg Wunsch int error; 7505f830ea2SJoerg Wunsch struct fdc_data *fdc; 7515f830ea2SJoerg Wunsch 7525f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7535f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 7545f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 7555f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 7565f830ea2SJoerg Wunsch 7575f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 7585f830ea2SJoerg Wunsch 7595f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 7605f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 7615f830ea2SJoerg Wunsch if (error) 7625f830ea2SJoerg Wunsch goto out; 7635f830ea2SJoerg Wunsch 7645f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 7655f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 7665f830ea2SJoerg Wunsch DELAY(100); 7675f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 7685f830ea2SJoerg Wunsch 7695f830ea2SJoerg Wunsch /* see if it can handle a command */ 7705f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7715f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 7725f830ea2SJoerg Wunsch error = ENXIO; 7735f830ea2SJoerg Wunsch goto out; 7745f830ea2SJoerg Wunsch } 7755f830ea2SJoerg Wunsch 7765f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 7775f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 7785f830ea2SJoerg Wunsch 7795f830ea2SJoerg Wunsch out: 7805f830ea2SJoerg Wunsch fdc_release_resources(fdc); 7815f830ea2SJoerg Wunsch return (error); 7825f830ea2SJoerg Wunsch } 7835f830ea2SJoerg Wunsch 7845f830ea2SJoerg Wunsch static int 7855f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev) 7865f830ea2SJoerg Wunsch { 7875f830ea2SJoerg Wunsch struct fdc_data *fdc; 7885f830ea2SJoerg Wunsch int error; 7895f830ea2SJoerg Wunsch 7905f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7915f830ea2SJoerg Wunsch 7925f830ea2SJoerg Wunsch /* have our children detached first */ 7935f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 7945f830ea2SJoerg Wunsch return (error); 7955f830ea2SJoerg Wunsch 7965f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 7975f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 7985f830ea2SJoerg Wunsch return (0); 7995f830ea2SJoerg Wunsch } 8005f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 8015f830ea2SJoerg Wunsch 8025f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 8035f830ea2SJoerg Wunsch fdc->fdc_intr); 8045f830ea2SJoerg Wunsch fdc_release_resources(fdc); 8055f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 8065f830ea2SJoerg Wunsch return (0); 8075f830ea2SJoerg Wunsch } 8085f830ea2SJoerg Wunsch 8095f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 8105f830ea2SJoerg Wunsch 8115b81b6b3SRodney W. Grimes /* 81237286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc. 8135b81b6b3SRodney W. Grimes */ 8146182fdbdSPeter Wemm static void 81537286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit) 8165b81b6b3SRodney W. Grimes { 81737286586SPeter Wemm int disabled; 81837286586SPeter Wemm struct fdc_ivars *ivar; 8196182fdbdSPeter Wemm device_t child; 82092200632SGarrett Wollman 8216182fdbdSPeter Wemm ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT); 82237286586SPeter Wemm if (ivar == NULL) 8236182fdbdSPeter Wemm return; 82437286586SPeter Wemm bzero(ivar, sizeof *ivar); 82537286586SPeter Wemm if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 82637286586SPeter Wemm ivar->fdunit = 0; 827fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit); 82837286586SPeter Wemm if (child == NULL) 8296182fdbdSPeter Wemm return; 83037286586SPeter Wemm device_set_ivars(child, ivar); 831a97c75b7SDoug Rabson if (resource_int_value(name, unit, "disabled", &disabled) == 0 832a97c75b7SDoug Rabson && disabled != 0) 8336182fdbdSPeter Wemm device_disable(child); 8346182fdbdSPeter Wemm } 8356182fdbdSPeter Wemm 8366182fdbdSPeter Wemm static int 8376182fdbdSPeter Wemm fdc_attach(device_t dev) 8386182fdbdSPeter Wemm { 83937286586SPeter Wemm struct fdc_data *fdc; 84037286586SPeter Wemm int i, error; 841427ccf00SDoug Rabson 84237286586SPeter Wemm fdc = device_get_softc(dev); 84337286586SPeter Wemm error = fdc_alloc_resources(fdc); 84437286586SPeter Wemm if (error) { 84537286586SPeter Wemm device_printf(dev, "cannot re-aquire resources\n"); 84637286586SPeter Wemm return error; 84737286586SPeter Wemm } 84837286586SPeter Wemm error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 84937286586SPeter Wemm INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr); 85037286586SPeter Wemm if (error) { 85137286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n"); 85237286586SPeter Wemm return error; 85337286586SPeter Wemm } 85437286586SPeter Wemm fdc->fdcu = device_get_unit(dev); 8555b81b6b3SRodney W. Grimes fdc->flags |= FDC_ATTACHED; 8566182fdbdSPeter Wemm 8575f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 858100f78bbSSujal Patel /* Acquire the DMA channel forever, The driver will do the rest */ 8596182fdbdSPeter Wemm /* XXX should integrate with rman */ 860100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 861dd87702aSBruce Evans isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 8625f830ea2SJoerg Wunsch } 8635b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 8646182fdbdSPeter Wemm 8653a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 866427ccf00SDoug Rabson fdout_wr(fdc, ((fdc->fdout = 0))); 8678177437dSPoul-Henning Kamp bioq_init(&fdc->head); 8685b81b6b3SRodney W. Grimes 8696182fdbdSPeter Wemm /* 87037286586SPeter Wemm * Probe and attach any children. We should probably detect 87137286586SPeter Wemm * devices from the BIOS unless overridden. 8726182fdbdSPeter Wemm */ 87337286586SPeter Wemm for (i = resource_query_string(-1, "at", device_get_nameunit(dev)); 87437286586SPeter Wemm i != -1; 87537286586SPeter Wemm i = resource_query_string(i, "at", device_get_nameunit(dev))) 87637286586SPeter Wemm fdc_add_child(dev, resource_query_name(i), 87737286586SPeter Wemm resource_query_unit(i)); 87837286586SPeter Wemm 8796182fdbdSPeter Wemm return (bus_generic_attach(dev)); 8806182fdbdSPeter Wemm } 8816182fdbdSPeter Wemm 88215317dd8SMatthew N. Dodd static int 8836182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 8846182fdbdSPeter Wemm { 88515317dd8SMatthew N. Dodd int retval = 0; 88615317dd8SMatthew N. Dodd 88715317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 88815317dd8SMatthew N. Dodd retval += printf(" on %s drive %d\n", device_get_nameunit(me), 88937286586SPeter Wemm fdc_get_fdunit(child)); 89015317dd8SMatthew N. Dodd 89115317dd8SMatthew N. Dodd return (retval); 8926182fdbdSPeter Wemm } 8936182fdbdSPeter Wemm 89416e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 89516e68fc6SPeter Wemm /* Device interface */ 89616e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 89716e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 89816e68fc6SPeter Wemm DEVMETHOD(device_detach, bus_generic_detach), 89916e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 90016e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 90116e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 90216e68fc6SPeter Wemm 90316e68fc6SPeter Wemm /* Bus interface */ 90416e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 90537286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 90616e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 90716e68fc6SPeter Wemm 90816e68fc6SPeter Wemm { 0, 0 } 90916e68fc6SPeter Wemm }; 91016e68fc6SPeter Wemm 91116e68fc6SPeter Wemm static driver_t fdc_driver = { 91216e68fc6SPeter Wemm "fdc", 91316e68fc6SPeter Wemm fdc_methods, 91416e68fc6SPeter Wemm sizeof(struct fdc_data) 91516e68fc6SPeter Wemm }; 91616e68fc6SPeter Wemm 91716e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 91816e68fc6SPeter Wemm 9195f830ea2SJoerg Wunsch #if NCARD > 0 9205f830ea2SJoerg Wunsch 9215f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 9225f830ea2SJoerg Wunsch /* Device interface */ 9235f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 9245f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 9255f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fdc_pccard_detach), 9265f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 9275f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 9285f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 9295f830ea2SJoerg Wunsch 9305f830ea2SJoerg Wunsch /* Bus interface */ 9315f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 9325f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 9335f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 9345f830ea2SJoerg Wunsch 9355f830ea2SJoerg Wunsch { 0, 0 } 9365f830ea2SJoerg Wunsch }; 9375f830ea2SJoerg Wunsch 9385f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 9395f830ea2SJoerg Wunsch "fdc", 9405f830ea2SJoerg Wunsch fdc_pccard_methods, 9415f830ea2SJoerg Wunsch sizeof(struct fdc_data) 9425f830ea2SJoerg Wunsch }; 9435f830ea2SJoerg Wunsch 9445f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 9455f830ea2SJoerg Wunsch 9465f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 9475f830ea2SJoerg Wunsch 9483f54a085SPoul-Henning Kamp static void fd_clone __P((void *arg, char *name, int namelen, dev_t *dev)); 9493f54a085SPoul-Henning Kamp 9503f54a085SPoul-Henning Kamp static struct { 9513f54a085SPoul-Henning Kamp char *match; 9523f54a085SPoul-Henning Kamp int minor; 9533f54a085SPoul-Henning Kamp int link; 9543f54a085SPoul-Henning Kamp } fd_suffix[] = { 9553f54a085SPoul-Henning Kamp { "a", 0, 1 }, 9563f54a085SPoul-Henning Kamp { "b", 0, 1 }, 9573f54a085SPoul-Henning Kamp { "c", 0, 1 }, 9583f54a085SPoul-Henning Kamp { "d", 0, 1 }, 9593f54a085SPoul-Henning Kamp { "e", 0, 1 }, 9603f54a085SPoul-Henning Kamp { "f", 0, 1 }, 9613f54a085SPoul-Henning Kamp { "g", 0, 1 }, 9623f54a085SPoul-Henning Kamp { "h", 0, 1 }, 9633f54a085SPoul-Henning Kamp { ".1720", 1, 0 }, 9643f54a085SPoul-Henning Kamp { ".1480", 2, 0 }, 9653f54a085SPoul-Henning Kamp { ".1440", 3, 0 }, 9663f54a085SPoul-Henning Kamp { ".1200", 4, 0 }, 9673f54a085SPoul-Henning Kamp { ".820", 5, 0 }, 9683f54a085SPoul-Henning Kamp { ".800", 6, 0 }, 9693f54a085SPoul-Henning Kamp { ".720", 7, 0 }, 9703f54a085SPoul-Henning Kamp { ".360", 8, 0 }, 9713f54a085SPoul-Henning Kamp { ".640", 9, 0 }, 9723f54a085SPoul-Henning Kamp { ".1232", 10, 0 }, 9733f54a085SPoul-Henning Kamp { 0, 0 } 9743f54a085SPoul-Henning Kamp }; 9753f54a085SPoul-Henning Kamp static void 9763f54a085SPoul-Henning Kamp fd_clone(arg, name, namelen, dev) 9773f54a085SPoul-Henning Kamp void *arg; 9783f54a085SPoul-Henning Kamp char *name; 9793f54a085SPoul-Henning Kamp int namelen; 9803f54a085SPoul-Henning Kamp dev_t *dev; 9813f54a085SPoul-Henning Kamp { 9823f54a085SPoul-Henning Kamp int u, d, i; 9833f54a085SPoul-Henning Kamp char *n; 9843f54a085SPoul-Henning Kamp dev_t pdev; 9853f54a085SPoul-Henning Kamp 9863f54a085SPoul-Henning Kamp if (*dev != NODEV) 9873f54a085SPoul-Henning Kamp return; 988db901281SPoul-Henning Kamp if (dev_stdclone(name, &n, "fd", &u) != 2) 9893f54a085SPoul-Henning Kamp return; 9903f54a085SPoul-Henning Kamp for (i = 0; ; i++) { 9913f54a085SPoul-Henning Kamp if (fd_suffix[i].match == NULL) 9923f54a085SPoul-Henning Kamp return; 9933f54a085SPoul-Henning Kamp if (strcmp(n, fd_suffix[i].match)) 9943f54a085SPoul-Henning Kamp continue; 9953f54a085SPoul-Henning Kamp d = fd_suffix[i].minor; 9963f54a085SPoul-Henning Kamp break; 9973f54a085SPoul-Henning Kamp } 9983f54a085SPoul-Henning Kamp if (fd_suffix[i].link == 0) { 9993f54a085SPoul-Henning Kamp *dev = make_dev(&fd_cdevsw, (u << 6) + d, 10003f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, name); 10013f54a085SPoul-Henning Kamp } else { 10023f54a085SPoul-Henning Kamp pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d); 10033f54a085SPoul-Henning Kamp *dev = make_dev_alias(pdev, name); 10043f54a085SPoul-Henning Kamp } 10053f54a085SPoul-Henning Kamp } 10063f54a085SPoul-Henning Kamp 100716e68fc6SPeter Wemm /******************************************************************/ 100816e68fc6SPeter Wemm /* 100916e68fc6SPeter Wemm * devices attached to the controller section. 101016e68fc6SPeter Wemm */ 10116182fdbdSPeter Wemm static int 10126182fdbdSPeter Wemm fd_probe(device_t dev) 10136182fdbdSPeter Wemm { 10146182fdbdSPeter Wemm int i; 10156182fdbdSPeter Wemm u_int fdt, st0, st3; 10166182fdbdSPeter Wemm struct fd_data *fd; 10176182fdbdSPeter Wemm struct fdc_data *fdc; 10186182fdbdSPeter Wemm fdsu_t fdsu; 10196182fdbdSPeter Wemm static int fd_fifo = 0; 10206182fdbdSPeter Wemm 10216182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 10226182fdbdSPeter Wemm fd = device_get_softc(dev); 10236182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 10246182fdbdSPeter Wemm 10256182fdbdSPeter Wemm bzero(fd, sizeof *fd); 10266182fdbdSPeter Wemm fd->dev = dev; 10276182fdbdSPeter Wemm fd->fdc = fdc; 10286182fdbdSPeter Wemm fd->fdsu = fdsu; 10296182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 10306182fdbdSPeter Wemm 1031a97c75b7SDoug Rabson #ifdef __i386__ 1032b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 10336182fdbdSPeter Wemm switch (fd->fdu) { 10346182fdbdSPeter Wemm case 0: 10355f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 10365f830ea2SJoerg Wunsch fdt = RTCFDT_144M; 10375f830ea2SJoerg Wunsch else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 10380722d6abSJoerg Wunsch fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 10390722d6abSJoerg Wunsch else 10400722d6abSJoerg Wunsch fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 1041b99f0a4aSAndrew Moore break; 10426182fdbdSPeter Wemm case 1: 10436182fdbdSPeter Wemm fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 1044dc5df763SJoerg Wunsch break; 1045dc5df763SJoerg Wunsch default: 10466182fdbdSPeter Wemm fdt = RTCFDT_NONE; 1047dc5df763SJoerg Wunsch break; 10486b7bd95bSJoerg Wunsch } 1049a97c75b7SDoug Rabson #else 1050a97c75b7SDoug Rabson fdt = RTCFDT_144M; /* XXX probably */ 1051a97c75b7SDoug Rabson #endif 10526182fdbdSPeter Wemm 10536182fdbdSPeter Wemm /* is there a unit? */ 10546182fdbdSPeter Wemm if (fdt == RTCFDT_NONE) 10556182fdbdSPeter Wemm return (ENXIO); 10566182fdbdSPeter Wemm 10576182fdbdSPeter Wemm /* select it */ 10586182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 10596182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 10606182fdbdSPeter Wemm 10618de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 10626182fdbdSPeter Wemm if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 1063e34c71eaSJoerg Wunsch && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 10646182fdbdSPeter Wemm && enable_fifo(fdc) == 0) { 1065b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1066b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1067d66c374fSTor Egge } 10686182fdbdSPeter Wemm fd_fifo = 1; 10696182fdbdSPeter Wemm 10706182fdbdSPeter Wemm if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 10716182fdbdSPeter Wemm && (st3 & NE7_ST3_T0)) { 1072dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 1073dc5df763SJoerg Wunsch /* seek some steps: */ 10746182fdbdSPeter Wemm fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 1075dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 10766182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1077dc5df763SJoerg Wunsch } 1078dc5df763SJoerg Wunsch 1079dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 10806182fdbdSPeter Wemm if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1081dc5df763SJoerg Wunsch /* Seek some steps... */ 10826182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1083dc5df763SJoerg Wunsch /* ...wait a moment... */ 1084dc5df763SJoerg Wunsch DELAY(300000); 1085dc5df763SJoerg Wunsch /* make ctrlr happy: */ 10866182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1087dc5df763SJoerg Wunsch } 1088dc5df763SJoerg Wunsch } 1089dc5df763SJoerg Wunsch 10906b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 10916b7bd95bSJoerg Wunsch /* 10926b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 10936b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 10946b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 10956b7bd95bSJoerg Wunsch * more than 77 steps 10966b7bd95bSJoerg Wunsch */ 1097dc5df763SJoerg Wunsch /* go back to 0: */ 10986182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 10996b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 11006b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 11015b81b6b3SRodney W. Grimes 11026b7bd95bSJoerg Wunsch /* anything responding? */ 1103dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1104dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 11056b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 11066b7bd95bSJoerg Wunsch } 1107dc5df763SJoerg Wunsch } 11086b7bd95bSJoerg Wunsch 11096182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 11103a2f7427SDavid Greenman 11113a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 11126182fdbdSPeter Wemm return (ENXIO); 11135b81b6b3SRodney W. Grimes 1114dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1115b99f0a4aSAndrew Moore fd->fdc = fdc; 1116b99f0a4aSAndrew Moore fd->fdsu = fdsu; 11173a2f7427SDavid Greenman fd->options = 0; 111802a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 111902a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 11205b81b6b3SRodney W. Grimes 1121b99f0a4aSAndrew Moore switch (fdt) { 11227ca0641bSAndrey A. Chernov case RTCFDT_12M: 11236182fdbdSPeter Wemm device_set_desc(dev, "1200-KB 5.25\" drive"); 1124b99f0a4aSAndrew Moore fd->type = FD_1200; 11257ca0641bSAndrey A. Chernov break; 11260722d6abSJoerg Wunsch case RTCFDT_144M | RTCFDT_144M_PRETENDED: 11276182fdbdSPeter Wemm device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 11280722d6abSJoerg Wunsch fdt = RTCFDT_144M; 11296182fdbdSPeter Wemm fd->type = FD_1440; 11307ca0641bSAndrey A. Chernov case RTCFDT_144M: 11316182fdbdSPeter Wemm device_set_desc(dev, "1440-KB 3.5\" drive"); 1132b99f0a4aSAndrew Moore fd->type = FD_1440; 11337ca0641bSAndrey A. Chernov break; 1134290dd077SJoerg Wunsch case RTCFDT_288M: 113586a727d9SJoerg Wunsch case RTCFDT_288M_1: 11366182fdbdSPeter Wemm device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1137290dd077SJoerg Wunsch fd->type = FD_1440; 1138290dd077SJoerg Wunsch break; 11397ca0641bSAndrey A. Chernov case RTCFDT_360K: 11406182fdbdSPeter Wemm device_set_desc(dev, "360-KB 5.25\" drive"); 1141b99f0a4aSAndrew Moore fd->type = FD_360; 11427ca0641bSAndrey A. Chernov break; 1143ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 11446182fdbdSPeter Wemm printf("720-KB 3.5\" drive"); 1145b99f0a4aSAndrew Moore fd->type = FD_720; 1146ed2fa05eSAndrey A. Chernov break; 11477ca0641bSAndrey A. Chernov default: 11486182fdbdSPeter Wemm return (ENXIO); 11495b81b6b3SRodney W. Grimes } 11506182fdbdSPeter Wemm return (0); 11516182fdbdSPeter Wemm } 11526182fdbdSPeter Wemm 11536182fdbdSPeter Wemm static int 11546182fdbdSPeter Wemm fd_attach(device_t dev) 11556182fdbdSPeter Wemm { 11566182fdbdSPeter Wemm struct fd_data *fd; 1157db901281SPoul-Henning Kamp static int cdevsw_add_done = 0; 11586182fdbdSPeter Wemm 11596182fdbdSPeter Wemm fd = device_get_softc(dev); 11606182fdbdSPeter Wemm 116102211baeSNick Hibma if (!cdevsw_add_done) { 1162475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 116302211baeSNick Hibma cdevsw_add_done++; 116402211baeSNick Hibma } 1165db901281SPoul-Henning Kamp EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000); 11663f54a085SPoul-Henning Kamp make_dev(&fd_cdevsw, (fd->fdu << 6), 11673f54a085SPoul-Henning Kamp UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 1168ada9bd8cSJulian Elischer 1169671e2ceeSBruce Evans /* 1170b2dfb1f9SJustin T. Gibbs * Export the drive to the devstat interface. 1171671e2ceeSBruce Evans */ 11726182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 11736182fdbdSPeter Wemm device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS, 11742a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 11752a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 11766182fdbdSPeter Wemm return (0); 11775b81b6b3SRodney W. Grimes } 11785b81b6b3SRodney W. Grimes 11795f830ea2SJoerg Wunsch static int 11805f830ea2SJoerg Wunsch fd_detach(device_t dev) 11815f830ea2SJoerg Wunsch { 11825f830ea2SJoerg Wunsch struct fd_data *fd; 11835f830ea2SJoerg Wunsch 11845f830ea2SJoerg Wunsch fd = device_get_softc(dev); 11855f830ea2SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 11865f830ea2SJoerg Wunsch 11875f830ea2SJoerg Wunsch return (0); 11885f830ea2SJoerg Wunsch } 11895f830ea2SJoerg Wunsch 119016e68fc6SPeter Wemm static device_method_t fd_methods[] = { 119116e68fc6SPeter Wemm /* Device interface */ 119216e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 119316e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 11945f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 119516e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 119616e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 119716e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 119816e68fc6SPeter Wemm 119916e68fc6SPeter Wemm { 0, 0 } 120016e68fc6SPeter Wemm }; 120116e68fc6SPeter Wemm 120216e68fc6SPeter Wemm static driver_t fd_driver = { 120316e68fc6SPeter Wemm "fd", 120416e68fc6SPeter Wemm fd_methods, 120516e68fc6SPeter Wemm sizeof(struct fd_data) 120616e68fc6SPeter Wemm }; 120716e68fc6SPeter Wemm 1208475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 120916e68fc6SPeter Wemm 12105b81b6b3SRodney W. Grimes /****************************************************************************/ 12115b81b6b3SRodney W. Grimes /* motor control stuff */ 12125e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 12135b81b6b3SRodney W. Grimes /****************************************************************************/ 12143a2f7427SDavid Greenman static void 12156182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 12165b81b6b3SRodney W. Grimes { 12176182fdbdSPeter Wemm int fdout = fdc->fdout; 12183a2f7427SDavid Greenman int needspecify = 0; 12193a2f7427SDavid Greenman 12203a2f7427SDavid Greenman if(turnon) { 12213a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 12223a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 12233a2f7427SDavid Greenman } else 12243a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 12253a2f7427SDavid Greenman 12265e235068SJordan K. Hubbard if(!turnon 12275e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 12285e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 12295e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 12305e235068SJordan K. Hubbard else { 12313a2f7427SDavid Greenman /* make sure controller is selected and specified */ 12323a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 12333a2f7427SDavid Greenman needspecify = 1; 12343a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 12355b81b6b3SRodney W. Grimes } 12365b81b6b3SRodney W. Grimes 1237427ccf00SDoug Rabson fdout_wr(fdc, fdout); 12386182fdbdSPeter Wemm fdc->fdout = fdout; 12393a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 12403a2f7427SDavid Greenman 12413a2f7427SDavid Greenman if (needspecify) { 1242dc8603e3SJoerg Wunsch /* 1243dc5df763SJoerg Wunsch * XXX 1244dc8603e3SJoerg Wunsch * special case: since we have just woken up the FDC 1245dc8603e3SJoerg Wunsch * from its sleep, we silently assume the command will 1246dc8603e3SJoerg Wunsch * be accepted, and do not test for a timeout 1247dc8603e3SJoerg Wunsch */ 12486182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1249dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1250dc5df763SJoerg Wunsch 0); 12516182fdbdSPeter Wemm if (fdc->flags & FDC_HAS_FIFO) 12526182fdbdSPeter Wemm (void) enable_fifo(fdc); 12533a2f7427SDavid Greenman } 12543a2f7427SDavid Greenman } 12553a2f7427SDavid Greenman 1256381fe1aaSGarrett Wollman static void 12576182fdbdSPeter Wemm fd_turnoff(void *xfd) 12585b81b6b3SRodney W. Grimes { 1259f5f7ba03SJordan K. Hubbard int s; 12606182fdbdSPeter Wemm fd_p fd = xfd; 1261dc16046fSJoerg Wunsch 12626182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 12638335c1b8SBruce Evans 12645f830ea2SJoerg Wunsch s = splbio(); 12658335c1b8SBruce Evans /* 12668335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 12675f830ea2SJoerg Wunsch * 12685f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 12695f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 12705f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 12715f830ea2SJoerg Wunsch * so the state machine gets re-entered. 12728335c1b8SBruce Evans */ 12736182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 12745f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 12755f830ea2SJoerg Wunsch splx(s); 12768335c1b8SBruce Evans return; 12778335c1b8SBruce Evans } 12788335c1b8SBruce Evans 12795b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 12806182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1281f5f7ba03SJordan K. Hubbard splx(s); 12825b81b6b3SRodney W. Grimes } 12835b81b6b3SRodney W. Grimes 12843a2f7427SDavid Greenman static void 12856182fdbdSPeter Wemm fd_motor_on(void *xfd) 12865b81b6b3SRodney W. Grimes { 1287f5f7ba03SJordan K. Hubbard int s; 12886182fdbdSPeter Wemm fd_p fd = xfd; 1289f5f7ba03SJordan K. Hubbard 1290f5f7ba03SJordan K. Hubbard s = splbio(); 12915b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 12925b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 12935b81b6b3SRodney W. Grimes { 12946182fdbdSPeter Wemm fdc_intr(fd->fdc); 12955b81b6b3SRodney W. Grimes } 1296f5f7ba03SJordan K. Hubbard splx(s); 12975b81b6b3SRodney W. Grimes } 12985b81b6b3SRodney W. Grimes 12993a2f7427SDavid Greenman static void 13006182fdbdSPeter Wemm fd_turnon(fd_p fd) 13015b81b6b3SRodney W. Grimes { 13025b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 13035b81b6b3SRodney W. Grimes { 13043a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 13056182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 13066182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 13075b81b6b3SRodney W. Grimes } 13085b81b6b3SRodney W. Grimes } 13095b81b6b3SRodney W. Grimes 1310381fe1aaSGarrett Wollman static void 1311dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 13125b81b6b3SRodney W. Grimes { 13133a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1314427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13153a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 13163a2f7427SDavid Greenman DELAY(100); 13173a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1318427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 13193a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 13203a2f7427SDavid Greenman DELAY(100); 1321427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 13223a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 13233a2f7427SDavid Greenman 1324dc5df763SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 13256182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1326dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1327dc5df763SJoerg Wunsch 0); 1328d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1329d66c374fSTor Egge (void) enable_fifo(fdc); 13305b81b6b3SRodney W. Grimes } 13315b81b6b3SRodney W. Grimes 13325b81b6b3SRodney W. Grimes /****************************************************************************/ 13335b81b6b3SRodney W. Grimes /* fdc in/out */ 13345b81b6b3SRodney W. Grimes /****************************************************************************/ 13355b81b6b3SRodney W. Grimes int 13366182fdbdSPeter Wemm in_fdc(struct fdc_data *fdc) 13375b81b6b3SRodney W. Grimes { 13385b81b6b3SRodney W. Grimes int i, j = 100000; 1339427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 13405b81b6b3SRodney W. Grimes != (NE7_DIO|NE7_RQM) && j-- > 0) 1341dc5df763SJoerg Wunsch if (i == NE7_RQM) 13426182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 13435b81b6b3SRodney W. Grimes if (j <= 0) 13446182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1345d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1346427ccf00SDoug Rabson i = fddata_rd(fdc); 13473a2f7427SDavid Greenman TRACE1("[FDDATA->0x%x]", (unsigned char)i); 13485b81b6b3SRodney W. Grimes return(i); 1349d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1350427ccf00SDoug Rabson return fddata_rd(fdc); 1351d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 13525b81b6b3SRodney W. Grimes } 13535b81b6b3SRodney W. Grimes 1354dc5df763SJoerg Wunsch /* 1355dc5df763SJoerg Wunsch * fd_in: Like in_fdc, but allows you to see if it worked. 1356dc5df763SJoerg Wunsch */ 1357b5e8ce9fSBruce Evans static int 13586182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1359dc5df763SJoerg Wunsch { 1360dc5df763SJoerg Wunsch int i, j = 100000; 1361427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1362dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 1363dc5df763SJoerg Wunsch if (i == NE7_RQM) 13646182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1365dc5df763SJoerg Wunsch if (j <= 0) 13666182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1367d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1368427ccf00SDoug Rabson i = fddata_rd(fdc); 1369dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1370dc5df763SJoerg Wunsch *ptr = i; 1371dc5df763SJoerg Wunsch return 0; 1372d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1373427ccf00SDoug Rabson i = fddata_rd(fdc); 1374dc5df763SJoerg Wunsch if (ptr) 1375dc5df763SJoerg Wunsch *ptr = i; 1376dc5df763SJoerg Wunsch return 0; 1377d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1378dc5df763SJoerg Wunsch } 1379dc5df763SJoerg Wunsch 1380dc5df763SJoerg Wunsch int 13816182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 13825b81b6b3SRodney W. Grimes { 13833b3837dbSRodney W. Grimes int i; 13845b81b6b3SRodney W. Grimes 13853b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 13863b3837dbSRodney W. Grimes i = 100000; 1387427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0); 13886182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 13893b3837dbSRodney W. Grimes 13903b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 13913b3837dbSRodney W. Grimes i = 100000; 1392427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0); 139316b04b6aSJoerg Wunsch if (i <= 0) 13946182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 13953b3837dbSRodney W. Grimes 13963b3837dbSRodney W. Grimes /* Send the command and return */ 1397427ccf00SDoug Rabson fddata_wr(fdc, x); 13983a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 13995b81b6b3SRodney W. Grimes return (0); 14005b81b6b3SRodney W. Grimes } 14015b81b6b3SRodney W. Grimes 14025b81b6b3SRodney W. Grimes /****************************************************************************/ 14035b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 14045b81b6b3SRodney W. Grimes /****************************************************************************/ 1405381fe1aaSGarrett Wollman int 1406671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 14075b81b6b3SRodney W. Grimes { 14085b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 140920a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 14106182fdbdSPeter Wemm fd_p fd; 1411b99f0a4aSAndrew Moore fdc_p fdc; 14125b81b6b3SRodney W. Grimes 14135b81b6b3SRodney W. Grimes /* check bounds */ 14146182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1415b99f0a4aSAndrew Moore return (ENXIO); 14166182fdbdSPeter Wemm fdc = fd->fdc; 14176182fdbdSPeter Wemm if ((fdc == NULL) || (fd->type == NO_TYPE)) 1418b99f0a4aSAndrew Moore return (ENXIO); 1419b99f0a4aSAndrew Moore if (type > NUMDENS) 1420b99f0a4aSAndrew Moore return (ENXIO); 14217ca0641bSAndrey A. Chernov if (type == 0) 14226182fdbdSPeter Wemm type = fd->type; 14237ca0641bSAndrey A. Chernov else { 14243e425b96SJulian Elischer /* 14253e425b96SJulian Elischer * For each type of basic drive, make sure we are trying 14263e425b96SJulian Elischer * to open a type it can do, 14273e425b96SJulian Elischer */ 14286182fdbdSPeter Wemm if (type != fd->type) { 14296182fdbdSPeter Wemm switch (fd->type) { 14307ca0641bSAndrey A. Chernov case FD_360: 14317ca0641bSAndrey A. Chernov return (ENXIO); 1432ed2fa05eSAndrey A. Chernov case FD_720: 1433b39c878eSAndrey A. Chernov if ( type != FD_820 1434b39c878eSAndrey A. Chernov && type != FD_800 14356fb89845SKATO Takenori && type != FD_640 1436ed2fa05eSAndrey A. Chernov ) 1437ed2fa05eSAndrey A. Chernov return (ENXIO); 1438ed2fa05eSAndrey A. Chernov break; 14397ca0641bSAndrey A. Chernov case FD_1200: 1440b39c878eSAndrey A. Chernov switch (type) { 1441b39c878eSAndrey A. Chernov case FD_1480: 1442b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1443fa4700b4SAndrey A. Chernov break; 14447ca0641bSAndrey A. Chernov case FD_1440: 1445b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1446b39c878eSAndrey A. Chernov break; 14476fb89845SKATO Takenori case FD_1232: 14486fb89845SKATO Takenori break; 1449b39c878eSAndrey A. Chernov case FD_820: 1450b39c878eSAndrey A. Chernov type = FD_820in5_25; 1451b39c878eSAndrey A. Chernov break; 1452b39c878eSAndrey A. Chernov case FD_800: 1453b39c878eSAndrey A. Chernov type = FD_800in5_25; 1454b39c878eSAndrey A. Chernov break; 1455b39c878eSAndrey A. Chernov case FD_720: 1456b39c878eSAndrey A. Chernov type = FD_720in5_25; 1457b39c878eSAndrey A. Chernov break; 14586fb89845SKATO Takenori case FD_640: 14596fb89845SKATO Takenori type = FD_640in5_25; 14606fb89845SKATO Takenori break; 1461b39c878eSAndrey A. Chernov case FD_360: 1462b39c878eSAndrey A. Chernov type = FD_360in5_25; 1463b39c878eSAndrey A. Chernov break; 1464b39c878eSAndrey A. Chernov default: 1465b39c878eSAndrey A. Chernov return(ENXIO); 1466b39c878eSAndrey A. Chernov } 1467b39c878eSAndrey A. Chernov break; 1468b39c878eSAndrey A. Chernov case FD_1440: 1469b39c878eSAndrey A. Chernov if ( type != FD_1720 1470b39c878eSAndrey A. Chernov && type != FD_1480 1471ed2fa05eSAndrey A. Chernov && type != FD_1200 1472b39c878eSAndrey A. Chernov && type != FD_820 1473b39c878eSAndrey A. Chernov && type != FD_800 1474b39c878eSAndrey A. Chernov && type != FD_720 14756fb89845SKATO Takenori && type != FD_640 14767ca0641bSAndrey A. Chernov ) 1477dffff499SAndrey A. Chernov return(ENXIO); 1478fa4700b4SAndrey A. Chernov break; 14797ca0641bSAndrey A. Chernov } 14807ca0641bSAndrey A. Chernov } 1481fa4700b4SAndrey A. Chernov } 14826182fdbdSPeter Wemm fd->ft = fd_types + type - 1; 14836182fdbdSPeter Wemm fd->flags |= FD_OPEN; 14845f830ea2SJoerg Wunsch 14855b81b6b3SRodney W. Grimes return 0; 14865b81b6b3SRodney W. Grimes } 14875b81b6b3SRodney W. Grimes 1488381fe1aaSGarrett Wollman int 1489671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 14905b81b6b3SRodney W. Grimes { 14915b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 14926182fdbdSPeter Wemm struct fd_data *fd; 1493b99f0a4aSAndrew Moore 14946182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14956182fdbdSPeter Wemm fd->flags &= ~FD_OPEN; 14966182fdbdSPeter Wemm fd->options &= ~FDOPT_NORETRY; 1497dc16046fSJoerg Wunsch 14985b81b6b3SRodney W. Grimes return (0); 14995b81b6b3SRodney W. Grimes } 15005b81b6b3SRodney W. Grimes 15013a2f7427SDavid Greenman /****************************************************************************/ 15023a2f7427SDavid Greenman /* fdstrategy */ 15033a2f7427SDavid Greenman /****************************************************************************/ 15043a2f7427SDavid Greenman void 15058177437dSPoul-Henning Kamp fdstrategy(struct bio *bp) 15063a2f7427SDavid Greenman { 1507bb6382faSJoerg Wunsch unsigned nblocks, blknum, cando; 15083a2f7427SDavid Greenman int s; 15093a2f7427SDavid Greenman fdu_t fdu; 15103a2f7427SDavid Greenman fdc_p fdc; 15113a2f7427SDavid Greenman fd_p fd; 15123a2f7427SDavid Greenman size_t fdblk; 15133a2f7427SDavid Greenman 15148177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 15156182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 15166182fdbdSPeter Wemm if (fd == 0) 15176182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 15188177437dSPoul-Henning Kamp (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 15193a2f7427SDavid Greenman fdc = fd->fdc; 152069acd21dSWarner Losh if (fd->type == NO_TYPE) { 15218177437dSPoul-Henning Kamp bp->bio_error = ENXIO; 15228177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15233b178206SWarner Losh goto bad; 152469acd21dSWarner Losh }; 15253a2f7427SDavid Greenman 1526d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 15278177437dSPoul-Henning Kamp if (!(bp->bio_cmd & BIO_FORMAT)) { 15288177437dSPoul-Henning Kamp if (bp->bio_blkno < 0) { 1529dc5df763SJoerg Wunsch printf( 15306a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 15318177437dSPoul-Henning Kamp fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 15328177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15338177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15343a2f7427SDavid Greenman goto bad; 15353a2f7427SDavid Greenman } 15368177437dSPoul-Henning Kamp if ((bp->bio_bcount % fdblk) != 0) { 15378177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15388177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15393a2f7427SDavid Greenman goto bad; 15403a2f7427SDavid Greenman } 15413a2f7427SDavid Greenman } 15423a2f7427SDavid Greenman 15433a2f7427SDavid Greenman /* 15443a2f7427SDavid Greenman * Set up block calculations. 15453a2f7427SDavid Greenman */ 15468177437dSPoul-Henning Kamp if (bp->bio_blkno > 20000000) { 1547bb6382faSJoerg Wunsch /* 1548bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1549bb6382faSJoerg Wunsch * multiplication below from overflowing. 1550bb6382faSJoerg Wunsch */ 15518177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15528177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 15533a2f7427SDavid Greenman goto bad; 15543a2f7427SDavid Greenman } 15558177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk; 1556bb6382faSJoerg Wunsch nblocks = fd->ft->size; 15578177437dSPoul-Henning Kamp bp->bio_resid = 0; 15588177437dSPoul-Henning Kamp if (blknum + (bp->bio_bcount / fdblk) > nblocks) { 1559bb6382faSJoerg Wunsch if (blknum <= nblocks) { 1560bb6382faSJoerg Wunsch cando = (nblocks - blknum) * fdblk; 15618177437dSPoul-Henning Kamp bp->bio_resid = bp->bio_bcount - cando; 1562bb6382faSJoerg Wunsch if (cando == 0) 1563bb6382faSJoerg Wunsch goto bad; /* not actually bad but EOF */ 1564bb6382faSJoerg Wunsch } else { 15658177437dSPoul-Henning Kamp bp->bio_error = EINVAL; 15668177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 1567bb6382faSJoerg Wunsch goto bad; 1568bb6382faSJoerg Wunsch } 1569bb6382faSJoerg Wunsch } 15708177437dSPoul-Henning Kamp bp->bio_pblkno = bp->bio_blkno; 15713a2f7427SDavid Greenman s = splbio(); 15728177437dSPoul-Henning Kamp bioqdisksort(&fdc->head, bp); 15736182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1574b2dfb1f9SJustin T. Gibbs 1575b2dfb1f9SJustin T. Gibbs /* Tell devstat we are starting on the transaction */ 1576b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 15775f830ea2SJoerg Wunsch device_busy(fd->dev); 1578b2dfb1f9SJustin T. Gibbs 15796182fdbdSPeter Wemm fdstart(fdc); 15803a2f7427SDavid Greenman splx(s); 15813a2f7427SDavid Greenman return; 15823a2f7427SDavid Greenman 15833a2f7427SDavid Greenman bad: 15843a2f7427SDavid Greenman biodone(bp); 15853a2f7427SDavid Greenman } 15863a2f7427SDavid Greenman 15875b81b6b3SRodney W. Grimes /***************************************************************\ 15885b81b6b3SRodney W. Grimes * fdstart * 15895b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 15905b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 15915b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 15925b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 15935b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 15945b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 15955b81b6b3SRodney W. Grimes \***************************************************************/ 1596381fe1aaSGarrett Wollman static void 15976182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 15985b81b6b3SRodney W. Grimes { 15995b81b6b3SRodney W. Grimes int s; 16005b81b6b3SRodney W. Grimes 16015b81b6b3SRodney W. Grimes s = splbio(); 16026182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 16035b81b6b3SRodney W. Grimes { 16046182fdbdSPeter Wemm fdc_intr(fdc); 16055b81b6b3SRodney W. Grimes } 16065b81b6b3SRodney W. Grimes splx(s); 16075b81b6b3SRodney W. Grimes } 16085b81b6b3SRodney W. Grimes 1609381fe1aaSGarrett Wollman static void 16106182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 16115b81b6b3SRodney W. Grimes { 16125c1a1eaeSBruce Evans fdc_p fdc; 1613f5f7ba03SJordan K. Hubbard int s; 16145b81b6b3SRodney W. Grimes 16156182fdbdSPeter Wemm fdc = xfdc; 16165c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 16175b81b6b3SRodney W. Grimes 16183a2f7427SDavid Greenman /* 16193a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 16203a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 16213a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 16223a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 16233a2f7427SDavid Greenman * Many thanks, Big Blue! 16245c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 16255c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 16265c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 16275c1a1eaeSBruce Evans * will reset the FDC and retry once. 16283a2f7427SDavid Greenman */ 16293a2f7427SDavid Greenman s = splbio(); 16305c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 16315c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 16325c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 16336182fdbdSPeter Wemm fdc_intr(fdc); 1634f5f7ba03SJordan K. Hubbard splx(s); 16355b81b6b3SRodney W. Grimes } 16365b81b6b3SRodney W. Grimes 16375b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1638381fe1aaSGarrett Wollman static void 16396182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 16405b81b6b3SRodney W. Grimes { 16415b81b6b3SRodney W. Grimes int s; 16423a2f7427SDavid Greenman 16435b81b6b3SRodney W. Grimes s = splbio(); 16446182fdbdSPeter Wemm fdc_intr(xfdc); 16455b81b6b3SRodney W. Grimes splx(s); 16465b81b6b3SRodney W. Grimes } 16475b81b6b3SRodney W. Grimes 16485b81b6b3SRodney W. Grimes /***********************************************************************\ 16495b81b6b3SRodney W. Grimes * fdintr * 16505b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 16515b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 16525b81b6b3SRodney W. Grimes \***********************************************************************/ 1653fe310de8SBruce Evans static void 16546182fdbdSPeter Wemm fdc_intr(void *xfdc) 16555b81b6b3SRodney W. Grimes { 16566182fdbdSPeter Wemm fdc_p fdc = xfdc; 16576182fdbdSPeter Wemm while(fdstate(fdc)) 1658381fe1aaSGarrett Wollman ; 16595b81b6b3SRodney W. Grimes } 16605b81b6b3SRodney W. Grimes 166169acd21dSWarner Losh /* 166269acd21dSWarner Losh * magic pseudo-DMA initialization for YE FDC. Sets count and 166369acd21dSWarner Losh * direction 166469acd21dSWarner Losh */ 16653b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 16663b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 16673b178206SWarner Losh ((cnt)-1) & 0xff); \ 16683b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 16693b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 167069acd21dSWarner Losh 167169acd21dSWarner Losh /* 167269acd21dSWarner Losh * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 167369acd21dSWarner Losh */ 16743b178206SWarner Losh static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 167569acd21dSWarner Losh { 167669acd21dSWarner Losh u_char *cptr = (u_char *)addr; 167769acd21dSWarner Losh 167821144e3bSPoul-Henning Kamp if (flags == BIO_READ) { 167969acd21dSWarner Losh if (fdc->state != PIOREAD) { 168069acd21dSWarner Losh fdc->state = PIOREAD; 168169acd21dSWarner Losh return(0); 168269acd21dSWarner Losh }; 16833b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 16843b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16853b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 168669acd21dSWarner Losh } else { 16873b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16883b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 16893b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 169069acd21dSWarner Losh }; 169169acd21dSWarner Losh return(1); 169269acd21dSWarner Losh } 169369acd21dSWarner Losh 16945b81b6b3SRodney W. Grimes /***********************************************************************\ 16955b81b6b3SRodney W. Grimes * The controller state machine. * 16965b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 16975b81b6b3SRodney W. Grimes \***********************************************************************/ 16983a2f7427SDavid Greenman static int 16996182fdbdSPeter Wemm fdstate(fdc_p fdc) 17005b81b6b3SRodney W. Grimes { 170156a23089SPoul-Henning Kamp int read, format, head, i, sec = 0, sectrac, st0, cyl, st3, idf; 1702bb6382faSJoerg Wunsch unsigned blknum = 0, b_cylinder = 0; 17035b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 17045b81b6b3SRodney W. Grimes fd_p fd; 17058177437dSPoul-Henning Kamp register struct bio *bp; 1706b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 17073a2f7427SDavid Greenman size_t fdblk; 17085b81b6b3SRodney W. Grimes 1709e93e63cbSBruce Evans bp = fdc->bp; 1710e93e63cbSBruce Evans if (bp == NULL) { 17118177437dSPoul-Henning Kamp bp = bioq_first(&fdc->head); 1712e93e63cbSBruce Evans if (bp != NULL) { 17138177437dSPoul-Henning Kamp bioq_remove(&fdc->head, bp); 1714e93e63cbSBruce Evans fdc->bp = bp; 1715e93e63cbSBruce Evans } 1716e93e63cbSBruce Evans } 1717e93e63cbSBruce Evans if (bp == NULL) { 17185b81b6b3SRodney W. Grimes /***********************************************\ 17195b81b6b3SRodney W. Grimes * nothing left for this controller to do * 17205b81b6b3SRodney W. Grimes * Force into the IDLE state, * 17215b81b6b3SRodney W. Grimes \***********************************************/ 17225b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 17236182fdbdSPeter Wemm if (fdc->fd) { 1724b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1725b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 17265b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 17275b81b6b3SRodney W. Grimes fdc->fdu = -1; 17285b81b6b3SRodney W. Grimes } 17296182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 17305b81b6b3SRodney W. Grimes return (0); 17315b81b6b3SRodney W. Grimes } 17328177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 17336182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 17343a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1735b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 1736b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 17378177437dSPoul-Henning Kamp read = bp->bio_cmd == BIO_READ; 173856a23089SPoul-Henning Kamp if (read) 173956a23089SPoul-Henning Kamp idf = ISADMA_READ; 174056a23089SPoul-Henning Kamp else 174156a23089SPoul-Henning Kamp idf = ISADMA_WRITE; 17428177437dSPoul-Henning Kamp format = bp->bio_cmd & BIO_FORMAT; 1743bb6382faSJoerg Wunsch if (format) { 17448177437dSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data; 1745bb6382faSJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1746bb6382faSJoerg Wunsch - (char *)finfo; 1747bb6382faSJoerg Wunsch } 1748bb6382faSJoerg Wunsch if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 17498177437dSPoul-Henning Kamp blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk + 1750bb6382faSJoerg Wunsch fd->skip/fdblk; 1751bb6382faSJoerg Wunsch b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1752bb6382faSJoerg Wunsch } 17535b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 17545b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 17555b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 17566182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 17576182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 17585b81b6b3SRodney W. Grimes switch (fdc->state) 17595b81b6b3SRodney W. Grimes { 17605b81b6b3SRodney W. Grimes case DEVIDLE: 17615b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 17625b81b6b3SRodney W. Grimes fdc->retry = 0; 17635b81b6b3SRodney W. Grimes fd->skip = 0; 17645b81b6b3SRodney W. Grimes fdc->fd = fd; 17655b81b6b3SRodney W. Grimes fdc->fdu = fdu; 17665f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 17673a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 17685b81b6b3SRodney W. Grimes /*******************************************************\ 17695b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 1770dc733423SDag-Erling Smørgrav * it will start up in its own good time * 17715b81b6b3SRodney W. Grimes \*******************************************************/ 17726182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 17735b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17745b81b6b3SRodney W. Grimes return (0); /* come back later */ 17755b81b6b3SRodney W. Grimes } 17765b81b6b3SRodney W. Grimes /*******************************************************\ 17775b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 17785b81b6b3SRodney W. Grimes \*******************************************************/ 17795b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 17805b81b6b3SRodney W. Grimes { 17815b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17826182fdbdSPeter Wemm fd_turnon(fd); 17835b81b6b3SRodney W. Grimes return (0); 17845b81b6b3SRodney W. Grimes } 17855b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 17865b81b6b3SRodney W. Grimes { 17876182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 17885b81b6b3SRodney W. Grimes } 17895c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 17905c1a1eaeSBruce Evans fdc->state = RESETCTLR; 17915c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 17925c1a1eaeSBruce Evans } else 17935e235068SJordan K. Hubbard fdc->state = DOSEEK; 17945b81b6b3SRodney W. Grimes break; 17955b81b6b3SRodney W. Grimes case DOSEEK: 1796bb6382faSJoerg Wunsch if (b_cylinder == (unsigned)fd->track) 17975b81b6b3SRodney W. Grimes { 17985b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17995b81b6b3SRodney W. Grimes break; 18005b81b6b3SRodney W. Grimes } 18016182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1802bb6382faSJoerg Wunsch fd->fdsu, b_cylinder * fd->ft->steptrac, 1803dc5df763SJoerg Wunsch 0)) 1804dc8603e3SJoerg Wunsch { 1805dc8603e3SJoerg Wunsch /* 1806dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1807dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1808dc8603e3SJoerg Wunsch */ 1809dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 18106182fdbdSPeter Wemm return(retrier(fdc)); 1811dc8603e3SJoerg Wunsch } 1812dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 18135b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 18145b81b6b3SRodney W. Grimes return(0); /* will return later */ 18155b81b6b3SRodney W. Grimes case SEEKWAIT: 18165b81b6b3SRodney W. Grimes /* allow heads to settle */ 18176182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 18185b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 18195b81b6b3SRodney W. Grimes return(0); /* will return later */ 18205b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 18215b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 18226182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 1823bb6382faSJoerg Wunsch int descyl = b_cylinder * fd->ft->steptrac; 18243a2f7427SDavid Greenman do { 18253a2f7427SDavid Greenman /* 1826dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1827dc5df763SJoerg Wunsch * which cannot really happen since the 1828dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1829dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1830dc5df763SJoerg Wunsch * line, so do one of the following: 1831dc5df763SJoerg Wunsch * 1832dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1833dc5df763SJoerg Wunsch * known to not go stuck after responding 1834dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1835dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1836dc5df763SJoerg Wunsch * real interrupt condition. 1837dc5df763SJoerg Wunsch * 1838dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1839dc5df763SJoerg Wunsch * up immediately. The controller will 1840dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1841dc5df763SJoerg Wunsch * conditions right after reset (for the 1842dc5df763SJoerg Wunsch * corresponding four drives), so this is 1843dc5df763SJoerg Wunsch * our only chance to get notice that it 1844dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 18453a2f7427SDavid Greenman */ 1846dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1847dc5df763SJoerg Wunsch == FD_NOT_VALID) 1848dc5df763SJoerg Wunsch return 0; 1849dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1850dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1851dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 18523a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1853dc5df763SJoerg Wunsch 18546182fdbdSPeter Wemm if (0 == descyl) { 1855dc5df763SJoerg Wunsch int failed = 0; 18563a2f7427SDavid Greenman /* 18573a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 18583a2f7427SDavid Greenman * really there 18593a2f7427SDavid Greenman */ 1860dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1861dc5df763SJoerg Wunsch failed = 1; 18623a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 18633a2f7427SDavid Greenman printf( 18643a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 18653a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1866dc5df763SJoerg Wunsch failed = 1; 1867dc5df763SJoerg Wunsch } 1868dc5df763SJoerg Wunsch 18696182fdbdSPeter Wemm if (failed) { 18703a2f7427SDavid Greenman if(fdc->retry < 3) 18713a2f7427SDavid Greenman fdc->retry = 3; 18726182fdbdSPeter Wemm return (retrier(fdc)); 18733a2f7427SDavid Greenman } 18743a2f7427SDavid Greenman } 1875dc5df763SJoerg Wunsch 18766182fdbdSPeter Wemm if (cyl != descyl) { 18773a2f7427SDavid Greenman printf( 18783a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 18792d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 1880e5d7d243SBruce Evans if (fdc->retry < 3) 1881e5d7d243SBruce Evans fdc->retry = 3; 18826182fdbdSPeter Wemm return (retrier(fdc)); 18835b81b6b3SRodney W. Grimes } 18845b81b6b3SRodney W. Grimes } 18855b81b6b3SRodney W. Grimes 1886bb6382faSJoerg Wunsch fd->track = b_cylinder; 18873b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 18888177437dSPoul-Henning Kamp isa_dmastart(idf, bp->bio_data+fd->skip, 18898177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 18905b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 18915b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 18925b81b6b3SRodney W. Grimes head = sec / sectrac; 18935b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 18943a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 18953a2f7427SDavid Greenman 18963a2f7427SDavid Greenman if(format || !read) 18973a2f7427SDavid Greenman { 18983a2f7427SDavid Greenman /* make sure the drive is writable */ 1899dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1900dc8603e3SJoerg Wunsch { 1901dc8603e3SJoerg Wunsch /* stuck controller? */ 19025f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 190356a23089SPoul-Henning Kamp isa_dmadone(idf, 19048177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19058177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19065c1a1eaeSBruce Evans fdc->dmachan); 1907dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 19086182fdbdSPeter Wemm return (retrier(fdc)); 1909dc8603e3SJoerg Wunsch } 19103a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 19113a2f7427SDavid Greenman { 19123a2f7427SDavid Greenman /* 19133a2f7427SDavid Greenman * XXX YES! this is ugly. 19143a2f7427SDavid Greenman * in order to force the current operation 19153a2f7427SDavid Greenman * to fail, we will have to fake an FDC 19163a2f7427SDavid Greenman * error - all error handling is done 19173a2f7427SDavid Greenman * by the retrier() 19183a2f7427SDavid Greenman */ 19193a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 19203a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 19213a2f7427SDavid Greenman fdc->status[2] = 0; 19223a2f7427SDavid Greenman fdc->status[3] = fd->track; 19233a2f7427SDavid Greenman fdc->status[4] = head; 19243a2f7427SDavid Greenman fdc->status[5] = sec; 19253a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 19263a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 19273a2f7427SDavid Greenman return (1); 19283a2f7427SDavid Greenman } 19293a2f7427SDavid Greenman } 19305b81b6b3SRodney W. Grimes 19316182fdbdSPeter Wemm if (format) { 19325f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 19335f830ea2SJoerg Wunsch /* 19345f830ea2SJoerg Wunsch * This seems to be necessary for 19355f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 19365f830ea2SJoerg Wunsch * it, we end up filling the sector ID 19375f830ea2SJoerg Wunsch * fields of the newly formatted track 19385f830ea2SJoerg Wunsch * entirely with garbage, causing 19395f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 19405f830ea2SJoerg Wunsch * the place when trying to read them 19415f830ea2SJoerg Wunsch * back. 19425f830ea2SJoerg Wunsch * 19435f830ea2SJoerg Wunsch * Umpf. 19445f830ea2SJoerg Wunsch */ 19458177437dSPoul-Henning Kamp SET_BCDR(fdc, 1, bp->bio_bcount, 0); 19465f830ea2SJoerg Wunsch 19478177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19488177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 19498177437dSPoul-Henning Kamp bp->bio_bcount); 19505f830ea2SJoerg Wunsch 19515f830ea2SJoerg Wunsch } 1952b39c878eSAndrey A. Chernov /* formatting */ 19536182fdbdSPeter Wemm if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1954dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1955dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1956dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 19576182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 1958dc8603e3SJoerg Wunsch /* controller fell over */ 19595f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 196056a23089SPoul-Henning Kamp isa_dmadone(idf, 19618177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19628177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 19635c1a1eaeSBruce Evans fdc->dmachan); 1964dc8603e3SJoerg Wunsch fdc->retry = 6; 19656182fdbdSPeter Wemm return (retrier(fdc)); 1966dc8603e3SJoerg Wunsch } 19676182fdbdSPeter Wemm } else { 19683b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 196969acd21dSWarner Losh /* 197069acd21dSWarner Losh * this seems to be necessary even when 197169acd21dSWarner Losh * reading data 197269acd21dSWarner Losh */ 19733b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 197469acd21dSWarner Losh 197569acd21dSWarner Losh /* 197669acd21dSWarner Losh * perform the write pseudo-DMA before 197769acd21dSWarner Losh * the WRITE command is sent 197869acd21dSWarner Losh */ 197969acd21dSWarner Losh if (!read) 19808177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd, 19818177437dSPoul-Henning Kamp bp->bio_data+fd->skip, 198269acd21dSWarner Losh fdblk); 198369acd21dSWarner Losh } 19846182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 1985dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1986dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1987dc5df763SJoerg Wunsch fd->track, /* track */ 1988dc5df763SJoerg Wunsch head, 1989dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1990dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1991dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1992dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1993dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 19946182fdbdSPeter Wemm 0)) { 1995dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 19965f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 199756a23089SPoul-Henning Kamp isa_dmadone(idf, 19988177437dSPoul-Henning Kamp bp->bio_data + fd->skip, 19998177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 20005c1a1eaeSBruce Evans fdc->dmachan); 2001dc8603e3SJoerg Wunsch fdc->retry = 6; 20026182fdbdSPeter Wemm return (retrier(fdc)); 20035b81b6b3SRodney W. Grimes } 2004b39c878eSAndrey A. Chernov } 20053b178206SWarner Losh if (fdc->flags & FDC_NODMA) 200669acd21dSWarner Losh /* 200769acd21dSWarner Losh * if this is a read, then simply await interrupt 200869acd21dSWarner Losh * before performing PIO 200969acd21dSWarner Losh */ 20108177437dSPoul-Henning Kamp if (read && !fdcpio(fdc,bp->bio_cmd, 20118177437dSPoul-Henning Kamp bp->bio_data+fd->skip,fdblk)) { 20123b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 201369acd21dSWarner Losh return(0); /* will return later */ 201469acd21dSWarner Losh }; 201569acd21dSWarner Losh 201669acd21dSWarner Losh /* 201769acd21dSWarner Losh * write (or format) operation will fall through and 201869acd21dSWarner Losh * await completion interrupt 201969acd21dSWarner Losh */ 20205b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 20216182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 20225b81b6b3SRodney W. Grimes return (0); /* will return later */ 202369acd21dSWarner Losh case PIOREAD: 202469acd21dSWarner Losh /* 202569acd21dSWarner Losh * actually perform the PIO read. The IOCOMPLETE case 202669acd21dSWarner Losh * removes the timeout for us. 202769acd21dSWarner Losh */ 20288177437dSPoul-Henning Kamp (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 202969acd21dSWarner Losh fdc->state = IOCOMPLETE; 203069acd21dSWarner Losh /* FALLTHROUGH */ 20315b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 20326182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 2033dc5df763SJoerg Wunsch 20346182fdbdSPeter Wemm if (fd_read_status(fdc, fd->fdsu)) { 20355f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 20368177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20378177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, 20385c1a1eaeSBruce Evans fdc->dmachan); 2039dc5df763SJoerg Wunsch if (fdc->retry < 6) 2040dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 20416182fdbdSPeter Wemm return (retrier(fdc)); 20425b81b6b3SRodney W. Grimes } 2043dc5df763SJoerg Wunsch 20443a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 2045dc5df763SJoerg Wunsch 20463a2f7427SDavid Greenman /* FALLTHROUGH */ 2047dc5df763SJoerg Wunsch 20483a2f7427SDavid Greenman case IOTIMEDOUT: 20493b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 20508177437dSPoul-Henning Kamp isa_dmadone(idf, bp->bio_data + fd->skip, 20518177437dSPoul-Henning Kamp format ? bp->bio_bcount : fdblk, fdc->dmachan); 20526182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 20533a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20543a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 2055b39c878eSAndrey A. Chernov /* 20563a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 20573a2f7427SDavid Greenman * and didn't release it in time for the 20583a2f7427SDavid Greenman * next FDC transfer. 20593a2f7427SDavid Greenman * Just restart it, don't increment retry 20603a2f7427SDavid Greenman * count. (vak) 2061b39c878eSAndrey A. Chernov */ 2062b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2063b39c878eSAndrey A. Chernov return (1); 2064b39c878eSAndrey A. Chernov } 20653a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 20663a2f7427SDavid Greenman && fdc->retry < 6) 20673a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 20683a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20693a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 20703a2f7427SDavid Greenman && fdc->retry < 3) 20713a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 20726182fdbdSPeter Wemm return (retrier(fdc)); 20735b81b6b3SRodney W. Grimes } 20745b81b6b3SRodney W. Grimes /* All OK */ 20753a2f7427SDavid Greenman fd->skip += fdblk; 20768177437dSPoul-Henning Kamp if (!format && fd->skip < bp->bio_bcount - bp->bio_resid) { 20775b81b6b3SRodney W. Grimes /* set up next transfer */ 20785b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20796182fdbdSPeter Wemm } else { 20805b81b6b3SRodney W. Grimes /* ALL DONE */ 20815b81b6b3SRodney W. Grimes fd->skip = 0; 2082e93e63cbSBruce Evans fdc->bp = NULL; 20835f830ea2SJoerg Wunsch device_unbusy(fd->dev); 20848177437dSPoul-Henning Kamp devstat_end_transaction_bio(&fd->device_stats, bp); 20855b81b6b3SRodney W. Grimes biodone(bp); 20865b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20875b81b6b3SRodney W. Grimes fdc->fdu = -1; 20885b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 20895b81b6b3SRodney W. Grimes } 20905b81b6b3SRodney W. Grimes return (1); 20915b81b6b3SRodney W. Grimes case RESETCTLR: 20923a2f7427SDavid Greenman fdc_reset(fdc); 20935b81b6b3SRodney W. Grimes fdc->retry++; 20945c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 20955c1a1eaeSBruce Evans return (0); 20965c1a1eaeSBruce Evans case RESETCOMPLETE: 20975c1a1eaeSBruce Evans /* 20985c1a1eaeSBruce Evans * Discard all the results from the reset so that they 20995c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 21005c1a1eaeSBruce Evans */ 21010e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 21020e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 21035c1a1eaeSBruce Evans fdc->state = STARTRECAL; 21045c1a1eaeSBruce Evans /* Fall through. */ 21055c1a1eaeSBruce Evans case STARTRECAL: 21066182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2107dc8603e3SJoerg Wunsch /* arrgl */ 2108dc8603e3SJoerg Wunsch fdc->retry = 6; 21096182fdbdSPeter Wemm return (retrier(fdc)); 2110dc8603e3SJoerg Wunsch } 21115b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 21125b81b6b3SRodney W. Grimes return (0); /* will return later */ 21135b81b6b3SRodney W. Grimes case RECALWAIT: 21145b81b6b3SRodney W. Grimes /* allow heads to settle */ 21156182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 21165b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 21175b81b6b3SRodney W. Grimes return (0); /* will return later */ 21185b81b6b3SRodney W. Grimes case RECALCOMPLETE: 21193a2f7427SDavid Greenman do { 21203a2f7427SDavid Greenman /* 2121dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 21223a2f7427SDavid Greenman */ 2123dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2124dc5df763SJoerg Wunsch return 0; 2125dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2126dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2127dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 21283a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 21293a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 21305b81b6b3SRodney W. Grimes { 2131dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2132dc8603e3SJoerg Wunsch /* 2133dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 2134dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2135dc8603e3SJoerg Wunsch * since people used to complain much about 2136dc8603e3SJoerg Wunsch * the failure message, try not logging 2137dc8603e3SJoerg Wunsch * this one if it seems to be the first 2138dc8603e3SJoerg Wunsch * time in a line 2139dc8603e3SJoerg Wunsch */ 2140dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2141dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 21423a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 21436182fdbdSPeter Wemm return (retrier(fdc)); 21445b81b6b3SRodney W. Grimes } 21455b81b6b3SRodney W. Grimes fd->track = 0; 21465b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 21475b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 21485b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21495b81b6b3SRodney W. Grimes case MOTORWAIT: 21505b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 21515b81b6b3SRodney W. Grimes { 21525b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 21535b81b6b3SRodney W. Grimes } 21545c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 21555c1a1eaeSBruce Evans fdc->state = RESETCTLR; 21565c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 21575c1a1eaeSBruce Evans } else { 21585e235068SJordan K. Hubbard /* 21595c1a1eaeSBruce Evans * If all motors were off, then the controller was 21605c1a1eaeSBruce Evans * reset, so it has lost track of the current 21615c1a1eaeSBruce Evans * cylinder. Recalibrate to handle this case. 2162f86e4077SBruce Evans * But first, discard the results of the reset. 21635e235068SJordan K. Hubbard */ 2164f86e4077SBruce Evans fdc->state = RESETCOMPLETE; 21655c1a1eaeSBruce Evans } 21665b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21675b81b6b3SRodney W. Grimes default: 2168b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 2169dc5df763SJoerg Wunsch if (fd_read_status(fdc, fd->fdsu) == 0) 2170a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 21715b81b6b3SRodney W. Grimes fdc->status[0], 21725b81b6b3SRodney W. Grimes fdc->status[1], 21735b81b6b3SRodney W. Grimes fdc->status[2], 21745b81b6b3SRodney W. Grimes fdc->status[3], 21755b81b6b3SRodney W. Grimes fdc->status[4], 21765b81b6b3SRodney W. Grimes fdc->status[5], 21775b81b6b3SRodney W. Grimes fdc->status[6] ); 21783a2f7427SDavid Greenman else 2179dac0f2dbSJoerg Wunsch printf("No status available "); 2180dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2181dac0f2dbSJoerg Wunsch { 2182dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 21835b81b6b3SRodney W. Grimes return (0); 21845b81b6b3SRodney W. Grimes } 2185dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2186dac0f2dbSJoerg Wunsch return (0); 2187dac0f2dbSJoerg Wunsch } 2188dac0f2dbSJoerg Wunsch /*XXX confusing: some branches return immediately, others end up here*/ 21895b81b6b3SRodney W. Grimes return (1); /* Come back immediatly to new state */ 21905b81b6b3SRodney W. Grimes } 21915b81b6b3SRodney W. Grimes 2192aaf08d94SGarrett Wollman static int 21936182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 21945b81b6b3SRodney W. Grimes { 21958177437dSPoul-Henning Kamp struct bio *bp; 21966182fdbdSPeter Wemm struct fd_data *fd; 21976182fdbdSPeter Wemm int fdu; 21985b81b6b3SRodney W. Grimes 2199e93e63cbSBruce Evans bp = fdc->bp; 22005b81b6b3SRodney W. Grimes 22016182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 22028177437dSPoul-Henning Kamp fdu = FDUNIT(minor(bp->bio_dev)); 22036182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22046182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 22053a2f7427SDavid Greenman goto fail; 22066182fdbdSPeter Wemm 22076182fdbdSPeter Wemm switch (fdc->retry) { 22085b81b6b3SRodney W. Grimes case 0: case 1: case 2: 22095b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 22105b81b6b3SRodney W. Grimes break; 22115b81b6b3SRodney W. Grimes case 3: case 4: case 5: 22125b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 22135b81b6b3SRodney W. Grimes break; 22145b81b6b3SRodney W. Grimes case 6: 22155b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 22165b81b6b3SRodney W. Grimes break; 22175b81b6b3SRodney W. Grimes case 7: 22185b81b6b3SRodney W. Grimes break; 22195b81b6b3SRodney W. Grimes default: 22203a2f7427SDavid Greenman fail: 22215b81b6b3SRodney W. Grimes { 22228177437dSPoul-Henning Kamp dev_t sav_bio_dev = bp->bio_dev; 22237ca0641bSAndrey A. Chernov /* Trick diskerr */ 22248177437dSPoul-Henning Kamp bp->bio_dev = makedev(major(bp->bio_dev), 22258177437dSPoul-Henning Kamp (FDUNIT(minor(bp->bio_dev))<<3)|RAW_PART); 2226887ba12fSBruce Evans diskerr(bp, "hard error", LOG_PRINTF, 22273a2f7427SDavid Greenman fdc->fd->skip / DEV_BSIZE, 22283a2f7427SDavid Greenman (struct disklabel *)NULL); 22298177437dSPoul-Henning Kamp bp->bio_dev = sav_bio_dev; 2230dc5df763SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) 2231dc5df763SJoerg Wunsch { 2232dc5df763SJoerg Wunsch printf( 2233a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2234dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2235dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2236dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2237dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2238dc5df763SJoerg Wunsch fdc->status[5]); 2239dc5df763SJoerg Wunsch } 2240dc5df763SJoerg Wunsch else 2241dc5df763SJoerg Wunsch printf(" (No status)\n"); 22425b81b6b3SRodney W. Grimes } 22438177437dSPoul-Henning Kamp bp->bio_flags |= BIO_ERROR; 22448177437dSPoul-Henning Kamp bp->bio_error = EIO; 22458177437dSPoul-Henning Kamp bp->bio_resid += bp->bio_bcount - fdc->fd->skip; 2246e93e63cbSBruce Evans fdc->bp = NULL; 22475b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 22485f830ea2SJoerg Wunsch device_unbusy(fd->dev); 22498177437dSPoul-Henning Kamp devstat_end_transaction_bio(&fdc->fd->device_stats, bp); 22505b81b6b3SRodney W. Grimes biodone(bp); 225192ed385aSRodney W. Grimes fdc->state = FINDWORK; 22525c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 22535b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 22545b81b6b3SRodney W. Grimes fdc->fdu = -1; 225592ed385aSRodney W. Grimes return (1); 22565b81b6b3SRodney W. Grimes } 22575b81b6b3SRodney W. Grimes fdc->retry++; 22585b81b6b3SRodney W. Grimes return (1); 22595b81b6b3SRodney W. Grimes } 22605b81b6b3SRodney W. Grimes 22611fdb6e6cSPoul-Henning Kamp static void 22621fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp) 22631fdb6e6cSPoul-Henning Kamp { 22641fdb6e6cSPoul-Henning Kamp wakeup(bp); 22651fdb6e6cSPoul-Henning Kamp } 22661fdb6e6cSPoul-Henning Kamp 2267b39c878eSAndrey A. Chernov static int 2268b39c878eSAndrey A. Chernov fdformat(dev, finfo, p) 2269b39c878eSAndrey A. Chernov dev_t dev; 2270b39c878eSAndrey A. Chernov struct fd_formb *finfo; 2271b39c878eSAndrey A. Chernov struct proc *p; 2272b39c878eSAndrey A. Chernov { 2273b39c878eSAndrey A. Chernov fdu_t fdu; 2274b39c878eSAndrey A. Chernov fd_p fd; 2275b39c878eSAndrey A. Chernov 22761fdb6e6cSPoul-Henning Kamp struct bio *bp; 2277b39c878eSAndrey A. Chernov int rv = 0, s; 22783a2f7427SDavid Greenman size_t fdblk; 2279b39c878eSAndrey A. Chernov 2280b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 22816182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22823a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2283b39c878eSAndrey A. Chernov 2284b39c878eSAndrey A. Chernov /* set up a buffer header for fdstrategy() */ 22851fdb6e6cSPoul-Henning Kamp bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT); 2286b39c878eSAndrey A. Chernov if(bp == 0) 22871fdb6e6cSPoul-Henning Kamp return ENOMEM; 228882f5379bSJoerg Wunsch /* 228982f5379bSJoerg Wunsch * keep the process from being swapped 229082f5379bSJoerg Wunsch */ 22912ada239cSPeter Wemm PHOLD(p); 22921fdb6e6cSPoul-Henning Kamp bzero((void *)bp, sizeof(*bp)); 22931fdb6e6cSPoul-Henning Kamp bp->bio_cmd = BIO_FORMAT; 2294b39c878eSAndrey A. Chernov 2295b39c878eSAndrey A. Chernov /* 2296b39c878eSAndrey A. Chernov * calculate a fake blkno, so fdstrategy() would initiate a 2297b39c878eSAndrey A. Chernov * seek to the requested cylinder 2298b39c878eSAndrey A. Chernov */ 22991fdb6e6cSPoul-Henning Kamp bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 23003a2f7427SDavid Greenman + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 2301b39c878eSAndrey A. Chernov 23021fdb6e6cSPoul-Henning Kamp bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 23031fdb6e6cSPoul-Henning Kamp bp->bio_data = (caddr_t)finfo; 2304b39c878eSAndrey A. Chernov 2305b39c878eSAndrey A. Chernov /* now do the format */ 23061fdb6e6cSPoul-Henning Kamp bp->bio_dev = dev; 23071fdb6e6cSPoul-Henning Kamp bp->bio_done = fdbiodone; 23081fdb6e6cSPoul-Henning Kamp fdstrategy(bp); 2309b39c878eSAndrey A. Chernov 2310b39c878eSAndrey A. Chernov /* ...and wait for it to complete */ 2311b39c878eSAndrey A. Chernov s = splbio(); 23121fdb6e6cSPoul-Henning Kamp while(!(bp->bio_flags & BIO_DONE)) { 23135e235068SJordan K. Hubbard rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 2314b39c878eSAndrey A. Chernov if (rv == EWOULDBLOCK) 2315b39c878eSAndrey A. Chernov break; 2316b39c878eSAndrey A. Chernov } 2317b39c878eSAndrey A. Chernov splx(s); 2318b39c878eSAndrey A. Chernov 231982f5379bSJoerg Wunsch if (rv == EWOULDBLOCK) { 2320b39c878eSAndrey A. Chernov /* timed out */ 2321b39c878eSAndrey A. Chernov rv = EIO; 23225f830ea2SJoerg Wunsch device_unbusy(fd->dev); 232382f5379bSJoerg Wunsch } 23241fdb6e6cSPoul-Henning Kamp if (bp->bio_flags & BIO_ERROR) 23251fdb6e6cSPoul-Henning Kamp rv = bp->bio_error; 232682f5379bSJoerg Wunsch /* 232782f5379bSJoerg Wunsch * allow the process to be swapped 232882f5379bSJoerg Wunsch */ 23292ada239cSPeter Wemm PRELE(p); 2330b39c878eSAndrey A. Chernov free(bp, M_TEMP); 2331b39c878eSAndrey A. Chernov return rv; 2332b39c878eSAndrey A. Chernov } 2333b39c878eSAndrey A. Chernov 2334f5f7ba03SJordan K. Hubbard /* 2335671e2ceeSBruce Evans * TODO: don't allocate buffer on stack. 2336f5f7ba03SJordan K. Hubbard */ 23375b81b6b3SRodney W. Grimes 23383e425b96SJulian Elischer static int 2339b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p) 2340f5f7ba03SJordan K. Hubbard dev_t dev; 2341ecbb00a2SDoug Rabson u_long cmd; 2342f5f7ba03SJordan K. Hubbard caddr_t addr; 2343f5f7ba03SJordan K. Hubbard int flag; 2344b39c878eSAndrey A. Chernov struct proc *p; 2345f5f7ba03SJordan K. Hubbard { 23463a2f7427SDavid Greenman fdu_t fdu = FDUNIT(minor(dev)); 23476182fdbdSPeter Wemm fd_p fd = devclass_get_softc(fd_devclass, fdu); 23483a2f7427SDavid Greenman size_t fdblk; 23493a2f7427SDavid Greenman 2350f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2351f5f7ba03SJordan K. Hubbard struct disklabel *dl; 2352f5f7ba03SJordan K. Hubbard char buffer[DEV_BSIZE]; 23533a2f7427SDavid Greenman int error = 0; 2354f5f7ba03SJordan K. Hubbard 23553a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2356f5f7ba03SJordan K. Hubbard 23576182fdbdSPeter Wemm switch (cmd) { 2358f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2359f5f7ba03SJordan K. Hubbard bzero(buffer, sizeof (buffer)); 2360f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 23613a2f7427SDavid Greenman dl->d_secsize = fdblk; 23626182fdbdSPeter Wemm fdt = fd->ft; 2363f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 2364f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 2365f5f7ba03SJordan K. Hubbard 236649ff4debSPoul-Henning Kamp if (readdisklabel(dkmodpart(dev, RAW_PART), dl) 2367191e1a59SBruce Evans == NULL) 2368f5f7ba03SJordan K. Hubbard error = 0; 2369f5f7ba03SJordan K. Hubbard else 2370f5f7ba03SJordan K. Hubbard error = EINVAL; 2371f5f7ba03SJordan K. Hubbard 2372f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 2373f5f7ba03SJordan K. Hubbard break; 2374f5f7ba03SJordan K. Hubbard 2375f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2376f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2377f5f7ba03SJordan K. Hubbard error = EBADF; 2378f5f7ba03SJordan K. Hubbard break; 2379f5f7ba03SJordan K. Hubbard 2380f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2381f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2382f5f7ba03SJordan K. Hubbard error = EBADF; 2383f5f7ba03SJordan K. Hubbard break; 2384f5f7ba03SJordan K. Hubbard 2385f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 23866182fdbdSPeter Wemm if ((flag & FWRITE) == 0) { 2387f5f7ba03SJordan K. Hubbard error = EBADF; 2388f5f7ba03SJordan K. Hubbard break; 2389f5f7ba03SJordan K. Hubbard } 2390f5f7ba03SJordan K. Hubbard 2391f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 2392f5f7ba03SJordan K. Hubbard 2393191e1a59SBruce Evans if ((error = setdisklabel((struct disklabel *)buffer, dl, 2394191e1a59SBruce Evans (u_long)0)) != 0) 2395f5f7ba03SJordan K. Hubbard break; 2396f5f7ba03SJordan K. Hubbard 239749ff4debSPoul-Henning Kamp error = writedisklabel(dev, (struct disklabel *)buffer); 2398b39c878eSAndrey A. Chernov break; 2399b39c878eSAndrey A. Chernov case FD_FORM: 2400b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2401b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 2402b39c878eSAndrey A. Chernov else if (((struct fd_formb *)addr)->format_version != 2403b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2404b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 2405b39c878eSAndrey A. Chernov else 2406b39c878eSAndrey A. Chernov error = fdformat(dev, (struct fd_formb *)addr, p); 2407b39c878eSAndrey A. Chernov break; 2408b39c878eSAndrey A. Chernov 2409b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 24103e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2411f5f7ba03SJordan K. Hubbard break; 2412f5f7ba03SJordan K. Hubbard 24133a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 24143a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 2415f711d546SPoul-Henning Kamp if (suser(p) != 0) 24163a2f7427SDavid Greenman return EPERM; 24173e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 24183a2f7427SDavid Greenman break; 24193a2f7427SDavid Greenman 24203a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 24213e425b96SJulian Elischer *(int *)addr = fd->options; 24223a2f7427SDavid Greenman break; 24233a2f7427SDavid Greenman 24243a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 24253e425b96SJulian Elischer fd->options = *(int *)addr; 24263a2f7427SDavid Greenman break; 24273a2f7427SDavid Greenman 2428f5f7ba03SJordan K. Hubbard default: 24293a2f7427SDavid Greenman error = ENOTTY; 2430f5f7ba03SJordan K. Hubbard break; 2431f5f7ba03SJordan K. Hubbard } 2432f5f7ba03SJordan K. Hubbard return (error); 2433f5f7ba03SJordan K. Hubbard } 2434f5f7ba03SJordan K. Hubbard 24353a2f7427SDavid Greenman /* 24363a2f7427SDavid Greenman * Hello emacs, these are the 24373a2f7427SDavid Greenman * Local Variables: 24383a2f7427SDavid Greenman * c-indent-level: 8 24393a2f7427SDavid Greenman * c-continued-statement-offset: 8 24403a2f7427SDavid Greenman * c-continued-brace-offset: 0 24413a2f7427SDavid Greenman * c-brace-offset: -8 24423a2f7427SDavid Greenman * c-brace-imaginary-offset: 0 24433a2f7427SDavid Greenman * c-argdecl-indent: 8 24443a2f7427SDavid Greenman * c-label-offset: -8 24453a2f7427SDavid Greenman * c++-hanging-braces: 1 24463a2f7427SDavid Greenman * c++-access-specifier-offset: -8 24473a2f7427SDavid Greenman * c++-empty-arglist-indent: 8 24483a2f7427SDavid Greenman * c++-friend-offset: 0 24493a2f7427SDavid Greenman * End: 24503a2f7427SDavid Greenman */ 2451