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> 60b99f0a4aSAndrew Moore #include <sys/buf.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 */ 87b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX 885b81b6b3SRodney W. Grimes 890722d6abSJoerg Wunsch /* configuration flags */ 900722d6abSJoerg Wunsch #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 91e34c71eaSJoerg Wunsch #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 920722d6abSJoerg Wunsch 930722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */ 940722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED 0x1000 950722d6abSJoerg Wunsch 96dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 97dc5df763SJoerg Wunsch #define FD_FAILED -1 98dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 99dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 100dc5df763SJoerg Wunsch 1016fb89845SKATO Takenori #define NUMTYPES 17 1026fb89845SKATO Takenori #define NUMDENS (NUMTYPES - 7) 1037ca0641bSAndrey A. Chernov 1043a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */ 105b99f0a4aSAndrew Moore #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 106b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 107b99f0a4aSAndrew Moore #define FD_1720 1 108b99f0a4aSAndrew Moore #define FD_1480 2 109b99f0a4aSAndrew Moore #define FD_1440 3 110b99f0a4aSAndrew Moore #define FD_1200 4 111b99f0a4aSAndrew Moore #define FD_820 5 112b99f0a4aSAndrew Moore #define FD_800 6 113b99f0a4aSAndrew Moore #define FD_720 7 114b99f0a4aSAndrew Moore #define FD_360 8 1156fb89845SKATO Takenori #define FD_640 9 1166fb89845SKATO Takenori #define FD_1232 10 117ed2fa05eSAndrey A. Chernov 1186fb89845SKATO Takenori #define FD_1480in5_25 11 1196fb89845SKATO Takenori #define FD_1440in5_25 12 1206fb89845SKATO Takenori #define FD_820in5_25 13 1216fb89845SKATO Takenori #define FD_800in5_25 14 1226fb89845SKATO Takenori #define FD_720in5_25 15 1236fb89845SKATO Takenori #define FD_360in5_25 16 1246fb89845SKATO Takenori #define FD_640in5_25 17 125b99f0a4aSAndrew Moore 1267ca0641bSAndrey A. Chernov 1276f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] = 1285b81b6b3SRodney W. Grimes { 129126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 130126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 131126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 132126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 133126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 134126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 135126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 136b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 1376fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /* 640K in DD 5.25in */ 1386fb89845SKATO Takenori { 8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */ 139ed2fa05eSAndrey A. Chernov 140126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 141126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 142126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 143126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 144126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 145126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 1466fb89845SKATO Takenori { 8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /* 640K in HD 5.25in */ 1475b81b6b3SRodney W. Grimes }; 1485b81b6b3SRodney W. Grimes 149b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 150dc16046fSJoerg Wunsch 1515b81b6b3SRodney W. Grimes /***********************************************************************\ 1525b81b6b3SRodney W. Grimes * Per controller structure. * 1535b81b6b3SRodney W. Grimes \***********************************************************************/ 1546182fdbdSPeter Wemm static devclass_t fdc_devclass; 1555b81b6b3SRodney W. Grimes 1565b81b6b3SRodney W. Grimes /***********************************************************************\ 1575b81b6b3SRodney W. Grimes * Per drive structure. * 158b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 1595b81b6b3SRodney W. Grimes \***********************************************************************/ 1606182fdbdSPeter Wemm struct fd_data { 161b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 1625b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 1633a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 1645b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 1655b81b6b3SRodney W. Grimes int flags; 1665b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 1675b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 1685b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 1695b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 1705b81b6b3SRodney W. Grimes int skip; 1715b81b6b3SRodney W. Grimes int hddrv; 172dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 1735b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 1743a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 17502a19910SJustin T. Gibbs struct callout_handle toffhandle; 17602a19910SJustin T. Gibbs struct callout_handle tohandle; 177b2dfb1f9SJustin T. Gibbs struct devstat device_stats; 1786182fdbdSPeter Wemm device_t dev; 1796182fdbdSPeter Wemm fdu_t fdu; 1806182fdbdSPeter Wemm }; 18137286586SPeter Wemm 18237286586SPeter Wemm struct fdc_ivars { 18337286586SPeter Wemm int fdunit; 18437286586SPeter Wemm }; 1856182fdbdSPeter Wemm static devclass_t fd_devclass; 1865b81b6b3SRodney W. Grimes 1875b81b6b3SRodney W. Grimes /***********************************************************************\ 1885b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 1895b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 1905b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 1915b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 1925b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 1935b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 1945b81b6b3SRodney W. Grimes \***********************************************************************/ 195b99f0a4aSAndrew Moore 1963a2f7427SDavid Greenman /* needed for ft driver, thus exported */ 1976182fdbdSPeter Wemm int in_fdc(struct fdc_data *); 1986182fdbdSPeter Wemm int out_fdc(struct fdc_data *, int); 1993a2f7427SDavid Greenman 2003a2f7427SDavid Greenman /* internal functions */ 2016182fdbdSPeter Wemm static void fdc_intr(void *); 2026182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int); 2033a2f7427SDavid Greenman # define TURNON 1 2043a2f7427SDavid Greenman # define TURNOFF 0 2053a2f7427SDavid Greenman static timeout_t fd_turnoff; 2063a2f7427SDavid Greenman static timeout_t fd_motor_on; 2076182fdbdSPeter Wemm static void fd_turnon(struct fd_data *); 2083a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2096182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *); 2106182fdbdSPeter Wemm static void fdstart(struct fdc_data *); 2115c1a1eaeSBruce Evans static timeout_t fd_iotimeout; 2123a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2136182fdbdSPeter Wemm static int fdstate(struct fdc_data *); 2146182fdbdSPeter Wemm static int retrier(struct fdc_data *); 2153a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *); 2163a2f7427SDavid Greenman 217d66c374fSTor Egge static int enable_fifo(fdc_p fdc); 218d66c374fSTor Egge 219d66c374fSTor Egge static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 220d66c374fSTor Egge 221aaf08d94SGarrett Wollman 2225b81b6b3SRodney W. Grimes #define DEVIDLE 0 2235b81b6b3SRodney W. Grimes #define FINDWORK 1 2245b81b6b3SRodney W. Grimes #define DOSEEK 2 2255b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 3 2265b81b6b3SRodney W. Grimes #define IOCOMPLETE 4 2275b81b6b3SRodney W. Grimes #define RECALCOMPLETE 5 2285b81b6b3SRodney W. Grimes #define STARTRECAL 6 2295b81b6b3SRodney W. Grimes #define RESETCTLR 7 2305b81b6b3SRodney W. Grimes #define SEEKWAIT 8 2315b81b6b3SRodney W. Grimes #define RECALWAIT 9 2325b81b6b3SRodney W. Grimes #define MOTORWAIT 10 2335b81b6b3SRodney W. Grimes #define IOTIMEDOUT 11 2345c1a1eaeSBruce Evans #define RESETCOMPLETE 12 23569acd21dSWarner Losh #define PIOREAD 13 2365b81b6b3SRodney W. Grimes 237d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 238cba2a7c6SBruce Evans static char const * const fdstates[] = 2395b81b6b3SRodney W. Grimes { 2405b81b6b3SRodney W. Grimes "DEVIDLE", 2415b81b6b3SRodney W. Grimes "FINDWORK", 2425b81b6b3SRodney W. Grimes "DOSEEK", 2435b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 2445b81b6b3SRodney W. Grimes "IOCOMPLETE", 2455b81b6b3SRodney W. Grimes "RECALCOMPLETE", 2465b81b6b3SRodney W. Grimes "STARTRECAL", 2475b81b6b3SRodney W. Grimes "RESETCTLR", 2485b81b6b3SRodney W. Grimes "SEEKWAIT", 2495b81b6b3SRodney W. Grimes "RECALWAIT", 2505b81b6b3SRodney W. Grimes "MOTORWAIT", 2515c1a1eaeSBruce Evans "IOTIMEDOUT", 2525c1a1eaeSBruce Evans "RESETCOMPLETE", 253ff9607b0SBruce Evans "PIOREAD", 2545b81b6b3SRodney W. Grimes }; 2555b81b6b3SRodney W. Grimes 2563a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 257cba2a7c6SBruce Evans static int volatile fd_debug = 0; 2585b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 2595b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 260d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */ 2615b81b6b3SRodney W. Grimes #define TRACE0(arg) 2625b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 263d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 2645b81b6b3SRodney W. Grimes 265427ccf00SDoug Rabson static void 266427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v) 267427ccf00SDoug Rabson { 268427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 269427ccf00SDoug Rabson } 270427ccf00SDoug Rabson 271427ccf00SDoug Rabson static u_int8_t 272427ccf00SDoug Rabson fdsts_rd(fdc_p fdc) 273427ccf00SDoug Rabson { 274427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 275427ccf00SDoug Rabson } 276427ccf00SDoug Rabson 277427ccf00SDoug Rabson static void 278427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v) 279427ccf00SDoug Rabson { 280427ccf00SDoug Rabson bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 281427ccf00SDoug Rabson } 282427ccf00SDoug Rabson 283427ccf00SDoug Rabson static u_int8_t 284427ccf00SDoug Rabson fddata_rd(fdc_p fdc) 285427ccf00SDoug Rabson { 286427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 287427ccf00SDoug Rabson } 288427ccf00SDoug Rabson 289427ccf00SDoug Rabson static void 2905f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v) 291427ccf00SDoug Rabson { 292427ccf00SDoug Rabson bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 293427ccf00SDoug Rabson } 294427ccf00SDoug Rabson 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 } 3005f830ea2SJoerg Wunsch 301427ccf00SDoug Rabson #if 0 302427ccf00SDoug Rabson 303427ccf00SDoug Rabson static u_int8_t 304427ccf00SDoug Rabson fdin_rd(fdc_p fdc) 305427ccf00SDoug Rabson { 306427ccf00SDoug Rabson return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 307427ccf00SDoug Rabson } 308427ccf00SDoug Rabson 309427ccf00SDoug Rabson #endif 310427ccf00SDoug Rabson 31187f6c662SJulian Elischer static d_open_t Fdopen; /* NOTE, not fdopen */ 31287f6c662SJulian Elischer static d_close_t fdclose; 31387f6c662SJulian Elischer static d_ioctl_t fdioctl; 31487f6c662SJulian Elischer static d_strategy_t fdstrategy; 31587f6c662SJulian Elischer 31687f6c662SJulian Elischer #define CDEV_MAJOR 9 31787f6c662SJulian Elischer #define BDEV_MAJOR 2 318f7ea2f55SJulian Elischer 3194e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = { 3204e2f199eSPoul-Henning Kamp /* open */ Fdopen, 3214e2f199eSPoul-Henning Kamp /* close */ fdclose, 3224e2f199eSPoul-Henning Kamp /* read */ physread, 3234e2f199eSPoul-Henning Kamp /* write */ physwrite, 3244e2f199eSPoul-Henning Kamp /* ioctl */ fdioctl, 3254e2f199eSPoul-Henning Kamp /* poll */ nopoll, 3264e2f199eSPoul-Henning Kamp /* mmap */ nommap, 3274e2f199eSPoul-Henning Kamp /* strategy */ fdstrategy, 3284e2f199eSPoul-Henning Kamp /* name */ "fd", 3294e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 3304e2f199eSPoul-Henning Kamp /* dump */ nodump, 3314e2f199eSPoul-Henning Kamp /* psize */ nopsize, 3324e2f199eSPoul-Henning Kamp /* flags */ D_DISK, 3334e2f199eSPoul-Henning Kamp /* bmaj */ BDEV_MAJOR 3344e2f199eSPoul-Henning Kamp }; 3354e2f199eSPoul-Henning Kamp 336dc5df763SJoerg Wunsch static int 3376182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s) 338dc5df763SJoerg Wunsch { 3396182fdbdSPeter Wemm fdc->fdc_errs++; 34016b04b6aSJoerg Wunsch if (s) { 341b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX) 342b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s); 343b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX) 344b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not " 345b6e5f28eSPeter Wemm "logging any more\n"); 34616b04b6aSJoerg Wunsch } 347dc5df763SJoerg Wunsch 348dc5df763SJoerg Wunsch return FD_FAILED; 349dc5df763SJoerg Wunsch } 350dc5df763SJoerg Wunsch 351dc5df763SJoerg Wunsch /* 352dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 353dc5df763SJoerg Wunsch * Unit number, 354dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 355dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 356dc5df763SJoerg Wunsch */ 3576f4e0bebSPoul-Henning Kamp static int 3586182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...) 359dc5df763SJoerg Wunsch { 360dc5df763SJoerg Wunsch u_char cmd; 361dc5df763SJoerg Wunsch int n_in; 362dc5df763SJoerg Wunsch int n; 363dc5df763SJoerg Wunsch va_list ap; 364dc5df763SJoerg Wunsch 365dc5df763SJoerg Wunsch va_start(ap, n_out); 366dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 367dc5df763SJoerg Wunsch va_end(ap); 368dc5df763SJoerg Wunsch va_start(ap, n_out); 369dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 370dc5df763SJoerg Wunsch { 3716182fdbdSPeter Wemm if (out_fdc(fdc, va_arg(ap, int)) < 0) 372dc5df763SJoerg Wunsch { 373dc5df763SJoerg Wunsch char msg[50]; 3742127f260SArchie Cobbs snprintf(msg, sizeof(msg), 375dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 376dc5df763SJoerg Wunsch cmd, n + 1, n_out); 3776182fdbdSPeter Wemm return fdc_err(fdc, msg); 378dc5df763SJoerg Wunsch } 379dc5df763SJoerg Wunsch } 380dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 381dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 382dc5df763SJoerg Wunsch { 383dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 3846182fdbdSPeter Wemm if (fd_in(fdc, ptr) < 0) 385dc5df763SJoerg Wunsch { 386dc5df763SJoerg Wunsch char msg[50]; 3872127f260SArchie Cobbs snprintf(msg, sizeof(msg), 388dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 389dc5df763SJoerg Wunsch cmd, n + 1, n_in); 3906182fdbdSPeter Wemm return fdc_err(fdc, msg); 391dc5df763SJoerg Wunsch } 392dc5df763SJoerg Wunsch } 393dc5df763SJoerg Wunsch 394dc5df763SJoerg Wunsch return 0; 395dc5df763SJoerg Wunsch } 396dc5df763SJoerg Wunsch 3976f4e0bebSPoul-Henning Kamp static int 398d66c374fSTor Egge enable_fifo(fdc_p fdc) 399d66c374fSTor Egge { 400d66c374fSTor Egge int i, j; 401d66c374fSTor Egge 402d66c374fSTor Egge if ((fdc->flags & FDC_HAS_FIFO) == 0) { 403d66c374fSTor Egge 404d66c374fSTor Egge /* 405d66c374fSTor Egge * XXX: 406d66c374fSTor Egge * Cannot use fd_cmd the normal way here, since 407d66c374fSTor Egge * this might be an invalid command. Thus we send the 408d66c374fSTor Egge * first byte, and check for an early turn of data directon. 409d66c374fSTor Egge */ 410d66c374fSTor Egge 4116182fdbdSPeter Wemm if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 4126182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 413d66c374fSTor Egge 414d66c374fSTor Egge /* If command is invalid, return */ 415d66c374fSTor Egge j = 100000; 416427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 417d66c374fSTor Egge != NE7_RQM && j-- > 0) 418d66c374fSTor Egge if (i == (NE7_DIO | NE7_RQM)) { 419d66c374fSTor Egge fdc_reset(fdc); 420d66c374fSTor Egge return FD_FAILED; 421d66c374fSTor Egge } 422d66c374fSTor Egge if (j<0 || 4236182fdbdSPeter Wemm fd_cmd(fdc, 3, 424d66c374fSTor Egge 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 425d66c374fSTor Egge fdc_reset(fdc); 4266182fdbdSPeter Wemm return fdc_err(fdc, "Enable FIFO failed\n"); 427d66c374fSTor Egge } 428d66c374fSTor Egge fdc->flags |= FDC_HAS_FIFO; 429d66c374fSTor Egge return 0; 430d66c374fSTor Egge } 4316182fdbdSPeter Wemm if (fd_cmd(fdc, 4, 432d66c374fSTor Egge I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 4336182fdbdSPeter Wemm return fdc_err(fdc, "Re-enable FIFO failed\n"); 434d66c374fSTor Egge return 0; 435d66c374fSTor Egge } 436d66c374fSTor Egge 437d66c374fSTor Egge static int 438dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 439dc5df763SJoerg Wunsch { 440dc5df763SJoerg Wunsch int st3; 441dc5df763SJoerg Wunsch 4426182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 443dc5df763SJoerg Wunsch { 4446182fdbdSPeter Wemm return fdc_err(fdc, "Sense Drive Status failed\n"); 445dc5df763SJoerg Wunsch } 446dc5df763SJoerg Wunsch if (st3p) 447dc5df763SJoerg Wunsch *st3p = st3; 448dc5df763SJoerg Wunsch 449dc5df763SJoerg Wunsch return 0; 450dc5df763SJoerg Wunsch } 451dc5df763SJoerg Wunsch 4526f4e0bebSPoul-Henning Kamp static int 453dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 454dc5df763SJoerg Wunsch { 4556182fdbdSPeter Wemm int cyl, st0, ret; 456dc5df763SJoerg Wunsch 4576182fdbdSPeter Wemm ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 4586182fdbdSPeter Wemm if (ret) { 4596182fdbdSPeter Wemm (void)fdc_err(fdc, 460dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 461dc5df763SJoerg Wunsch return ret; 462dc5df763SJoerg Wunsch } 463dc5df763SJoerg Wunsch 464dc5df763SJoerg Wunsch if (st0p) 465dc5df763SJoerg Wunsch *st0p = st0; 466dc5df763SJoerg Wunsch 4676182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 468dc5df763SJoerg Wunsch /* 469dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 470dc5df763SJoerg Wunsch */ 471dc5df763SJoerg Wunsch return FD_NOT_VALID; 472dc5df763SJoerg Wunsch } 473dc5df763SJoerg Wunsch 4746182fdbdSPeter Wemm if (fd_in(fdc, &cyl) < 0) { 4756182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n"); 476dc5df763SJoerg Wunsch } 477dc5df763SJoerg Wunsch 478dc5df763SJoerg Wunsch if (cylp) 479dc5df763SJoerg Wunsch *cylp = cyl; 480dc5df763SJoerg Wunsch 481dc5df763SJoerg Wunsch return 0; 482dc5df763SJoerg Wunsch } 483dc5df763SJoerg Wunsch 484dc5df763SJoerg Wunsch 4856f4e0bebSPoul-Henning Kamp static int 486dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu) 487dc5df763SJoerg Wunsch { 488dc5df763SJoerg Wunsch int i, ret; 489b5e8ce9fSBruce Evans 4906182fdbdSPeter Wemm for (i = 0; i < 7; i++) { 491b5e8ce9fSBruce Evans /* 492b5e8ce9fSBruce Evans * XXX types are poorly chosen. Only bytes can by read 493a838d83dSBruce Evans * from the hardware, but fdc->status[] wants u_ints and 494b5e8ce9fSBruce Evans * fd_in() gives ints. 495b5e8ce9fSBruce Evans */ 496b5e8ce9fSBruce Evans int status; 497b5e8ce9fSBruce Evans 4986182fdbdSPeter Wemm ret = fd_in(fdc, &status); 499b5e8ce9fSBruce Evans fdc->status[i] = status; 500b5e8ce9fSBruce Evans if (ret != 0) 501dc5df763SJoerg Wunsch break; 502dc5df763SJoerg Wunsch } 503dc5df763SJoerg Wunsch 504dc5df763SJoerg Wunsch if (ret == 0) 505dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 506dc5df763SJoerg Wunsch else 507dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 508dc5df763SJoerg Wunsch 509dc5df763SJoerg Wunsch return ret; 510dc5df763SJoerg Wunsch } 511dc5df763SJoerg Wunsch 5125b81b6b3SRodney W. Grimes /****************************************************************************/ 5135b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5145b81b6b3SRodney W. Grimes /****************************************************************************/ 515dc5df763SJoerg Wunsch 5163a2f7427SDavid Greenman static int 51737286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc) 5185b81b6b3SRodney W. Grimes { 51937286586SPeter Wemm device_t dev; 5205f830ea2SJoerg Wunsch int ispnp, ispcmcia; 5215b81b6b3SRodney W. Grimes 52237286586SPeter Wemm dev = fdc->fdc_dev; 5235f830ea2SJoerg Wunsch ispnp = (fdc->flags & FDC_ISPNP) != 0; 5245f830ea2SJoerg Wunsch ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 5256182fdbdSPeter Wemm fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 5266182fdbdSPeter Wemm fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 5276182fdbdSPeter Wemm 528b6e5f28eSPeter Wemm /* 5295f830ea2SJoerg Wunsch * On standard ISA, we don't just use an 8 port range 5305f830ea2SJoerg Wunsch * (e.g. 0x3f0-0x3f7) since that covers an IDE control 5315f830ea2SJoerg Wunsch * register at 0x3f6. 5325f830ea2SJoerg Wunsch * 533b6e5f28eSPeter Wemm * Isn't PC hardware wonderful. 5345f830ea2SJoerg Wunsch * 5355f830ea2SJoerg Wunsch * The Y-E Data PCMCIA FDC doesn't have this problem, it 5365f830ea2SJoerg Wunsch * uses the register with offset 6 for pseudo-DMA, and the 5375f830ea2SJoerg Wunsch * one with offset 7 as control register. 538b6e5f28eSPeter Wemm */ 5396182fdbdSPeter Wemm fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 5406182fdbdSPeter Wemm &fdc->rid_ioport, 0ul, ~0ul, 5415f830ea2SJoerg Wunsch ispcmcia ? 8 : (ispnp ? 1 : 6), 5425f830ea2SJoerg Wunsch RF_ACTIVE); 5436182fdbdSPeter Wemm if (fdc->res_ioport == 0) { 544427ccf00SDoug Rabson device_printf(dev, "cannot reserve I/O port range\n"); 54537286586SPeter Wemm return ENXIO; 5466182fdbdSPeter Wemm } 547427ccf00SDoug Rabson fdc->portt = rman_get_bustag(fdc->res_ioport); 548427ccf00SDoug Rabson fdc->porth = rman_get_bushandle(fdc->res_ioport); 549427ccf00SDoug Rabson 5505f830ea2SJoerg Wunsch if (!ispcmcia) { 551427ccf00SDoug Rabson /* 5525f830ea2SJoerg Wunsch * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 5535f830ea2SJoerg Wunsch * and some at 0x3f0-0x3f5,0x3f7. We detect the former 5545f830ea2SJoerg Wunsch * by checking the size and adjust the port address 5555f830ea2SJoerg Wunsch * accordingly. 556b6e5f28eSPeter Wemm */ 557b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 558b6e5f28eSPeter Wemm fdc->port_off = -2; 559b6e5f28eSPeter Wemm 560b6e5f28eSPeter Wemm /* 5615f830ea2SJoerg Wunsch * Register the control port range as rid 1 if it 5625f830ea2SJoerg Wunsch * isn't there already. Most PnP BIOSen will have 5635f830ea2SJoerg Wunsch * already done this but non-PnP configurations don't. 564a2639a18SPeter Wemm * 5655f830ea2SJoerg Wunsch * And some (!!) report 0x3f2-0x3f5 and completely 5665f830ea2SJoerg Wunsch * leave out the control register! It seems that some 5675f830ea2SJoerg Wunsch * non-antique controller chips have a different 5685f830ea2SJoerg Wunsch * method of programming the transfer speed which 5695f830ea2SJoerg Wunsch * doesn't require the control register, but it's 5705f830ea2SJoerg Wunsch * mighty bogus as the chip still responds to the 5715f830ea2SJoerg Wunsch * address for the control register. 572427ccf00SDoug Rabson */ 573b6e5f28eSPeter Wemm if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 574b9da888fSPeter Wemm u_long ctlstart; 575b9da888fSPeter Wemm 576b6e5f28eSPeter Wemm /* Find the control port, usually 0x3f7 */ 5775f830ea2SJoerg Wunsch ctlstart = rman_get_start(fdc->res_ioport) + 5785f830ea2SJoerg Wunsch fdc->port_off + 7; 579b6e5f28eSPeter Wemm 580b6e5f28eSPeter Wemm bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 581b9da888fSPeter Wemm } 582b6e5f28eSPeter Wemm 583b6e5f28eSPeter Wemm /* 584b6e5f28eSPeter Wemm * Now (finally!) allocate the control port. 585b6e5f28eSPeter Wemm */ 586427ccf00SDoug Rabson fdc->rid_ctl = 1; 5875f830ea2SJoerg Wunsch fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 5885f830ea2SJoerg Wunsch &fdc->rid_ctl, 589b6e5f28eSPeter Wemm 0ul, ~0ul, 1, RF_ACTIVE); 590427ccf00SDoug Rabson if (fdc->res_ctl == 0) { 5915f830ea2SJoerg Wunsch device_printf(dev, 5925f830ea2SJoerg Wunsch "cannot reserve control I/O port range\n"); 59337286586SPeter Wemm return ENXIO; 594427ccf00SDoug Rabson } 595427ccf00SDoug Rabson fdc->ctlt = rman_get_bustag(fdc->res_ctl); 596427ccf00SDoug Rabson fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 5975f830ea2SJoerg Wunsch } 5986182fdbdSPeter Wemm 5996182fdbdSPeter Wemm fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 6006182fdbdSPeter Wemm &fdc->rid_irq, 0ul, ~0ul, 1, 6016182fdbdSPeter Wemm RF_ACTIVE); 6026182fdbdSPeter Wemm if (fdc->res_irq == 0) { 603427ccf00SDoug Rabson device_printf(dev, "cannot reserve interrupt line\n"); 60437286586SPeter Wemm return ENXIO; 6056182fdbdSPeter Wemm } 6065f830ea2SJoerg Wunsch 6075f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 6086182fdbdSPeter Wemm fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 6096182fdbdSPeter Wemm &fdc->rid_drq, 0ul, ~0ul, 1, 6106182fdbdSPeter Wemm RF_ACTIVE); 6116182fdbdSPeter Wemm if (fdc->res_drq == 0) { 612427ccf00SDoug Rabson device_printf(dev, "cannot reserve DMA request line\n"); 61337286586SPeter Wemm return ENXIO; 6146182fdbdSPeter Wemm } 6156182fdbdSPeter Wemm fdc->dmachan = fdc->res_drq->r_start; 6165f830ea2SJoerg Wunsch } 61737286586SPeter Wemm 61837286586SPeter Wemm return 0; 61937286586SPeter Wemm } 62037286586SPeter Wemm 62137286586SPeter Wemm static void 62237286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc) 62337286586SPeter Wemm { 62437286586SPeter Wemm device_t dev; 62537286586SPeter Wemm 62637286586SPeter Wemm dev = fdc->fdc_dev; 62737286586SPeter Wemm if (fdc->res_irq != 0) { 62837286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 62937286586SPeter Wemm fdc->res_irq); 63037286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 63137286586SPeter Wemm fdc->res_irq); 63237286586SPeter Wemm } 63337286586SPeter Wemm if (fdc->res_ctl != 0) { 63437286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63537286586SPeter Wemm fdc->res_ctl); 63637286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 63737286586SPeter Wemm fdc->res_ctl); 63837286586SPeter Wemm } 63937286586SPeter Wemm if (fdc->res_ioport != 0) { 64037286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 64137286586SPeter Wemm fdc->res_ioport); 64237286586SPeter Wemm bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 64337286586SPeter Wemm fdc->res_ioport); 64437286586SPeter Wemm } 64537286586SPeter Wemm if (fdc->res_drq != 0) { 64637286586SPeter Wemm bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64737286586SPeter Wemm fdc->res_drq); 64837286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 64937286586SPeter Wemm fdc->res_drq); 65037286586SPeter Wemm } 65137286586SPeter Wemm } 65237286586SPeter Wemm 65337286586SPeter Wemm /****************************************************************************/ 65437286586SPeter Wemm /* autoconfiguration stuff */ 65537286586SPeter Wemm /****************************************************************************/ 65637286586SPeter Wemm 65737286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = { 65837286586SPeter Wemm {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 65937286586SPeter Wemm {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 66037286586SPeter Wemm {0} 66137286586SPeter Wemm }; 66237286586SPeter Wemm 66337286586SPeter Wemm static int 66437286586SPeter Wemm fdc_read_ivar(device_t dev, device_t child, int which, u_long *result) 66537286586SPeter Wemm { 66637286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child); 66737286586SPeter Wemm 66837286586SPeter Wemm switch (which) { 66937286586SPeter Wemm case FDC_IVAR_FDUNIT: 67037286586SPeter Wemm *result = ivars->fdunit; 67137286586SPeter Wemm break; 67237286586SPeter Wemm default: 67337286586SPeter Wemm return ENOENT; 67437286586SPeter Wemm } 67537286586SPeter Wemm return 0; 67637286586SPeter Wemm } 67737286586SPeter Wemm 67837286586SPeter Wemm /* 67937286586SPeter Wemm * fdc controller section. 68037286586SPeter Wemm */ 68137286586SPeter Wemm static int 68237286586SPeter Wemm fdc_probe(device_t dev) 68337286586SPeter Wemm { 68437286586SPeter Wemm int error, ic_type; 68537286586SPeter Wemm struct fdc_data *fdc; 68637286586SPeter Wemm 68737286586SPeter Wemm fdc = device_get_softc(dev); 68837286586SPeter Wemm bzero(fdc, sizeof *fdc); 68937286586SPeter Wemm fdc->fdc_dev = dev; 6905f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_isa; 69137286586SPeter Wemm 69237286586SPeter Wemm /* Check pnp ids */ 69337286586SPeter Wemm error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 69437286586SPeter Wemm if (error == ENXIO) 69537286586SPeter Wemm return ENXIO; 6965f830ea2SJoerg Wunsch if (error == 0) 6975f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPNP; 69837286586SPeter Wemm 69937286586SPeter Wemm /* Attempt to allocate our resources for the duration of the probe */ 70037286586SPeter Wemm error = fdc_alloc_resources(fdc); 70137286586SPeter Wemm if (error) 70237286586SPeter Wemm goto out; 7035b81b6b3SRodney W. Grimes 70416111cedSAndrew Moore /* First - lets reset the floppy controller */ 705427ccf00SDoug Rabson fdout_wr(fdc, 0); 70616111cedSAndrew Moore DELAY(100); 707427ccf00SDoug Rabson fdout_wr(fdc, FDO_FRST); 70816111cedSAndrew Moore 7095b81b6b3SRodney W. Grimes /* see if it can handle a command */ 7106182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7116182fdbdSPeter Wemm NE7_SPEC_2(2, 0), 0)) { 7126182fdbdSPeter Wemm error = ENXIO; 7136182fdbdSPeter Wemm goto out; 7145b81b6b3SRodney W. Grimes } 7156182fdbdSPeter Wemm 7166182fdbdSPeter Wemm if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 7176182fdbdSPeter Wemm ic_type = (u_char)ic_type; 7186182fdbdSPeter Wemm switch (ic_type) { 7196182fdbdSPeter Wemm case 0x80: 7206182fdbdSPeter Wemm device_set_desc(dev, "NEC 765 or clone"); 7216182fdbdSPeter Wemm fdc->fdct = FDC_NE765; 7226182fdbdSPeter Wemm break; 7236182fdbdSPeter Wemm case 0x81: 7246182fdbdSPeter Wemm device_set_desc(dev, "Intel 82077 or clone"); 7256182fdbdSPeter Wemm fdc->fdct = FDC_I82077; 7266182fdbdSPeter Wemm break; 7276182fdbdSPeter Wemm case 0x90: 7286182fdbdSPeter Wemm device_set_desc(dev, "NEC 72065B or clone"); 7296182fdbdSPeter Wemm fdc->fdct = FDC_NE72065; 7306182fdbdSPeter Wemm break; 7316182fdbdSPeter Wemm default: 7326182fdbdSPeter Wemm device_set_desc(dev, "generic floppy controller"); 7336182fdbdSPeter Wemm fdc->fdct = FDC_UNKNOWN; 7346182fdbdSPeter Wemm break; 7356182fdbdSPeter Wemm } 7366182fdbdSPeter Wemm } 7376182fdbdSPeter Wemm 7386182fdbdSPeter Wemm out: 73937286586SPeter Wemm fdc_release_resources(fdc); 7406182fdbdSPeter Wemm return (error); 7415b81b6b3SRodney W. Grimes } 7425b81b6b3SRodney W. Grimes 7435f830ea2SJoerg Wunsch #if NCARD > 0 7445f830ea2SJoerg Wunsch 7455f830ea2SJoerg Wunsch static int 7465f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev) 7475f830ea2SJoerg Wunsch { 7485f830ea2SJoerg Wunsch int error; 7495f830ea2SJoerg Wunsch struct fdc_data *fdc; 7505f830ea2SJoerg Wunsch 7515f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7525f830ea2SJoerg Wunsch bzero(fdc, sizeof *fdc); 7535f830ea2SJoerg Wunsch fdc->fdc_dev = dev; 7545f830ea2SJoerg Wunsch fdc->fdctl_wr = fdctl_wr_pcmcia; 7555f830ea2SJoerg Wunsch 7565f830ea2SJoerg Wunsch fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 7575f830ea2SJoerg Wunsch 7585f830ea2SJoerg Wunsch /* Attempt to allocate our resources for the duration of the probe */ 7595f830ea2SJoerg Wunsch error = fdc_alloc_resources(fdc); 7605f830ea2SJoerg Wunsch if (error) 7615f830ea2SJoerg Wunsch goto out; 7625f830ea2SJoerg Wunsch 7635f830ea2SJoerg Wunsch /* First - lets reset the floppy controller */ 7645f830ea2SJoerg Wunsch fdout_wr(fdc, 0); 7655f830ea2SJoerg Wunsch DELAY(100); 7665f830ea2SJoerg Wunsch fdout_wr(fdc, FDO_FRST); 7675f830ea2SJoerg Wunsch 7685f830ea2SJoerg Wunsch /* see if it can handle a command */ 7695f830ea2SJoerg Wunsch if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 7705f830ea2SJoerg Wunsch NE7_SPEC_2(2, 0), 0)) { 7715f830ea2SJoerg Wunsch error = ENXIO; 7725f830ea2SJoerg Wunsch goto out; 7735f830ea2SJoerg Wunsch } 7745f830ea2SJoerg Wunsch 7755f830ea2SJoerg Wunsch device_set_desc(dev, "Y-E Data PCMCIA floppy"); 7765f830ea2SJoerg Wunsch fdc->fdct = FDC_NE765; 7775f830ea2SJoerg Wunsch 7785f830ea2SJoerg Wunsch out: 7795f830ea2SJoerg Wunsch fdc_release_resources(fdc); 7805f830ea2SJoerg Wunsch return (error); 7815f830ea2SJoerg Wunsch } 7825f830ea2SJoerg Wunsch 7835f830ea2SJoerg Wunsch static int 7845f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev) 7855f830ea2SJoerg Wunsch { 7865f830ea2SJoerg Wunsch struct fdc_data *fdc; 7875f830ea2SJoerg Wunsch int error; 7885f830ea2SJoerg Wunsch 7895f830ea2SJoerg Wunsch fdc = device_get_softc(dev); 7905f830ea2SJoerg Wunsch 7915f830ea2SJoerg Wunsch /* have our children detached first */ 7925f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev))) 7935f830ea2SJoerg Wunsch return (error); 7945f830ea2SJoerg Wunsch 7955f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ATTACHED) == 0) { 7965f830ea2SJoerg Wunsch device_printf(dev, "already unloaded\n"); 7975f830ea2SJoerg Wunsch return (0); 7985f830ea2SJoerg Wunsch } 7995f830ea2SJoerg Wunsch fdc->flags &= ~FDC_ATTACHED; 8005f830ea2SJoerg Wunsch 8015f830ea2SJoerg Wunsch BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 8025f830ea2SJoerg Wunsch fdc->fdc_intr); 8035f830ea2SJoerg Wunsch fdc_release_resources(fdc); 8045f830ea2SJoerg Wunsch device_printf(dev, "unload\n"); 8055f830ea2SJoerg Wunsch return (0); 8065f830ea2SJoerg Wunsch } 8075f830ea2SJoerg Wunsch 8085f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 8095f830ea2SJoerg Wunsch 8105b81b6b3SRodney W. Grimes /* 81137286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc. 8125b81b6b3SRodney W. Grimes */ 8136182fdbdSPeter Wemm static void 81437286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit) 8155b81b6b3SRodney W. Grimes { 81637286586SPeter Wemm int disabled; 81737286586SPeter Wemm struct fdc_ivars *ivar; 8186182fdbdSPeter Wemm device_t child; 81992200632SGarrett Wollman 8206182fdbdSPeter Wemm ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT); 82137286586SPeter Wemm if (ivar == NULL) 8226182fdbdSPeter Wemm return; 82337286586SPeter Wemm bzero(ivar, sizeof *ivar); 82437286586SPeter Wemm if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 82537286586SPeter Wemm ivar->fdunit = 0; 826fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit); 82737286586SPeter Wemm if (child == NULL) 8286182fdbdSPeter Wemm return; 82937286586SPeter Wemm device_set_ivars(child, ivar); 830a97c75b7SDoug Rabson if (resource_int_value(name, unit, "disabled", &disabled) == 0 831a97c75b7SDoug Rabson && disabled != 0) 8326182fdbdSPeter Wemm device_disable(child); 8336182fdbdSPeter Wemm } 8346182fdbdSPeter Wemm 8356182fdbdSPeter Wemm static int 8366182fdbdSPeter Wemm fdc_attach(device_t dev) 8376182fdbdSPeter Wemm { 83837286586SPeter Wemm struct fdc_data *fdc; 83937286586SPeter Wemm int i, error; 840427ccf00SDoug Rabson 84137286586SPeter Wemm fdc = device_get_softc(dev); 84237286586SPeter Wemm error = fdc_alloc_resources(fdc); 84337286586SPeter Wemm if (error) { 84437286586SPeter Wemm device_printf(dev, "cannot re-aquire resources\n"); 84537286586SPeter Wemm return error; 84637286586SPeter Wemm } 84737286586SPeter Wemm error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 84837286586SPeter Wemm INTR_TYPE_BIO, fdc_intr, fdc, &fdc->fdc_intr); 84937286586SPeter Wemm if (error) { 85037286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n"); 85137286586SPeter Wemm return error; 85237286586SPeter Wemm } 85337286586SPeter Wemm fdc->fdcu = device_get_unit(dev); 8545b81b6b3SRodney W. Grimes fdc->flags |= FDC_ATTACHED; 8556182fdbdSPeter Wemm 8565f830ea2SJoerg Wunsch if ((fdc->flags & FDC_NODMA) == 0) { 857100f78bbSSujal Patel /* Acquire the DMA channel forever, The driver will do the rest */ 8586182fdbdSPeter Wemm /* XXX should integrate with rman */ 859100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 860dd87702aSBruce Evans isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 8615f830ea2SJoerg Wunsch } 8625b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 8636182fdbdSPeter Wemm 8643a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 865427ccf00SDoug Rabson fdout_wr(fdc, ((fdc->fdout = 0))); 86602a19910SJustin T. Gibbs bufq_init(&fdc->head); 8675b81b6b3SRodney W. Grimes 8686182fdbdSPeter Wemm /* 86937286586SPeter Wemm * Probe and attach any children. We should probably detect 87037286586SPeter Wemm * devices from the BIOS unless overridden. 8716182fdbdSPeter Wemm */ 87237286586SPeter Wemm for (i = resource_query_string(-1, "at", device_get_nameunit(dev)); 87337286586SPeter Wemm i != -1; 87437286586SPeter Wemm i = resource_query_string(i, "at", device_get_nameunit(dev))) 87537286586SPeter Wemm fdc_add_child(dev, resource_query_name(i), 87637286586SPeter Wemm resource_query_unit(i)); 87737286586SPeter Wemm 8786182fdbdSPeter Wemm return (bus_generic_attach(dev)); 8796182fdbdSPeter Wemm } 8806182fdbdSPeter Wemm 88115317dd8SMatthew N. Dodd static int 8826182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child) 8836182fdbdSPeter Wemm { 88415317dd8SMatthew N. Dodd int retval = 0; 88515317dd8SMatthew N. Dodd 88615317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child); 88715317dd8SMatthew N. Dodd retval += printf(" on %s drive %d\n", device_get_nameunit(me), 88837286586SPeter Wemm fdc_get_fdunit(child)); 88915317dd8SMatthew N. Dodd 89015317dd8SMatthew N. Dodd return (retval); 8916182fdbdSPeter Wemm } 8926182fdbdSPeter Wemm 89316e68fc6SPeter Wemm static device_method_t fdc_methods[] = { 89416e68fc6SPeter Wemm /* Device interface */ 89516e68fc6SPeter Wemm DEVMETHOD(device_probe, fdc_probe), 89616e68fc6SPeter Wemm DEVMETHOD(device_attach, fdc_attach), 89716e68fc6SPeter Wemm DEVMETHOD(device_detach, bus_generic_detach), 89816e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 89916e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), 90016e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), 90116e68fc6SPeter Wemm 90216e68fc6SPeter Wemm /* Bus interface */ 90316e68fc6SPeter Wemm DEVMETHOD(bus_print_child, fdc_print_child), 90437286586SPeter Wemm DEVMETHOD(bus_read_ivar, fdc_read_ivar), 90516e68fc6SPeter Wemm /* Our children never use any other bus interface methods. */ 90616e68fc6SPeter Wemm 90716e68fc6SPeter Wemm { 0, 0 } 90816e68fc6SPeter Wemm }; 90916e68fc6SPeter Wemm 91016e68fc6SPeter Wemm static driver_t fdc_driver = { 91116e68fc6SPeter Wemm "fdc", 91216e68fc6SPeter Wemm fdc_methods, 91316e68fc6SPeter Wemm sizeof(struct fdc_data) 91416e68fc6SPeter Wemm }; 91516e68fc6SPeter Wemm 91616e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 91716e68fc6SPeter Wemm 9185f830ea2SJoerg Wunsch #if NCARD > 0 9195f830ea2SJoerg Wunsch 9205f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = { 9215f830ea2SJoerg Wunsch /* Device interface */ 9225f830ea2SJoerg Wunsch DEVMETHOD(device_probe, fdc_pccard_probe), 9235f830ea2SJoerg Wunsch DEVMETHOD(device_attach, fdc_attach), 9245f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fdc_pccard_detach), 9255f830ea2SJoerg Wunsch DEVMETHOD(device_shutdown, bus_generic_shutdown), 9265f830ea2SJoerg Wunsch DEVMETHOD(device_suspend, bus_generic_suspend), 9275f830ea2SJoerg Wunsch DEVMETHOD(device_resume, bus_generic_resume), 9285f830ea2SJoerg Wunsch 9295f830ea2SJoerg Wunsch /* Bus interface */ 9305f830ea2SJoerg Wunsch DEVMETHOD(bus_print_child, fdc_print_child), 9315f830ea2SJoerg Wunsch DEVMETHOD(bus_read_ivar, fdc_read_ivar), 9325f830ea2SJoerg Wunsch /* Our children never use any other bus interface methods. */ 9335f830ea2SJoerg Wunsch 9345f830ea2SJoerg Wunsch { 0, 0 } 9355f830ea2SJoerg Wunsch }; 9365f830ea2SJoerg Wunsch 9375f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = { 9385f830ea2SJoerg Wunsch "fdc", 9395f830ea2SJoerg Wunsch fdc_pccard_methods, 9405f830ea2SJoerg Wunsch sizeof(struct fdc_data) 9415f830ea2SJoerg Wunsch }; 9425f830ea2SJoerg Wunsch 9435f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 9445f830ea2SJoerg Wunsch 9455f830ea2SJoerg Wunsch #endif /* NCARD > 0 */ 9465f830ea2SJoerg Wunsch 94716e68fc6SPeter Wemm /******************************************************************/ 94816e68fc6SPeter Wemm /* 94916e68fc6SPeter Wemm * devices attached to the controller section. 95016e68fc6SPeter Wemm */ 9516182fdbdSPeter Wemm static int 9526182fdbdSPeter Wemm fd_probe(device_t dev) 9536182fdbdSPeter Wemm { 9546182fdbdSPeter Wemm int i; 9556182fdbdSPeter Wemm u_int fdt, st0, st3; 9566182fdbdSPeter Wemm struct fd_data *fd; 9576182fdbdSPeter Wemm struct fdc_data *fdc; 9586182fdbdSPeter Wemm fdsu_t fdsu; 9596182fdbdSPeter Wemm static int fd_fifo = 0; 9606182fdbdSPeter Wemm 9616182fdbdSPeter Wemm fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 9626182fdbdSPeter Wemm fd = device_get_softc(dev); 9636182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev)); 9646182fdbdSPeter Wemm 9656182fdbdSPeter Wemm bzero(fd, sizeof *fd); 9666182fdbdSPeter Wemm fd->dev = dev; 9676182fdbdSPeter Wemm fd->fdc = fdc; 9686182fdbdSPeter Wemm fd->fdsu = fdsu; 9696182fdbdSPeter Wemm fd->fdu = device_get_unit(dev); 9706182fdbdSPeter Wemm 971a97c75b7SDoug Rabson #ifdef __i386__ 972b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 9736182fdbdSPeter Wemm switch (fd->fdu) { 9746182fdbdSPeter Wemm case 0: 9755f830ea2SJoerg Wunsch if ((fdc->flags & FDC_ISPCMCIA)) 9765f830ea2SJoerg Wunsch fdt = RTCFDT_144M; 9775f830ea2SJoerg Wunsch else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0) 9780722d6abSJoerg Wunsch fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 9790722d6abSJoerg Wunsch else 9800722d6abSJoerg Wunsch fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 981b99f0a4aSAndrew Moore break; 9826182fdbdSPeter Wemm case 1: 9836182fdbdSPeter Wemm fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 984dc5df763SJoerg Wunsch break; 985dc5df763SJoerg Wunsch default: 9866182fdbdSPeter Wemm fdt = RTCFDT_NONE; 987dc5df763SJoerg Wunsch break; 9886b7bd95bSJoerg Wunsch } 989a97c75b7SDoug Rabson #else 990a97c75b7SDoug Rabson fdt = RTCFDT_144M; /* XXX probably */ 991a97c75b7SDoug Rabson #endif 9926182fdbdSPeter Wemm 9936182fdbdSPeter Wemm /* is there a unit? */ 9946182fdbdSPeter Wemm if (fdt == RTCFDT_NONE) 9956182fdbdSPeter Wemm return (ENXIO); 9966182fdbdSPeter Wemm 9976182fdbdSPeter Wemm /* select it */ 9986182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNON); 9996182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */ 10006182fdbdSPeter Wemm 10018de0675cSPeter Wemm /* XXX This doesn't work before the first set_motor() */ 10026182fdbdSPeter Wemm if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN 1003e34c71eaSJoerg Wunsch && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 10046182fdbdSPeter Wemm && enable_fifo(fdc) == 0) { 1005b6e5f28eSPeter Wemm device_printf(device_get_parent(dev), 1006b6e5f28eSPeter Wemm "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1007d66c374fSTor Egge } 10086182fdbdSPeter Wemm fd_fifo = 1; 10096182fdbdSPeter Wemm 10106182fdbdSPeter Wemm if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) 10116182fdbdSPeter Wemm && (st3 & NE7_ST3_T0)) { 1012dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 1013dc5df763SJoerg Wunsch /* seek some steps: */ 10146182fdbdSPeter Wemm fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0); 1015dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 10166182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 1017dc5df763SJoerg Wunsch } 1018dc5df763SJoerg Wunsch 1019dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 10206182fdbdSPeter Wemm if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 1021dc5df763SJoerg Wunsch /* Seek some steps... */ 10226182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1023dc5df763SJoerg Wunsch /* ...wait a moment... */ 1024dc5df763SJoerg Wunsch DELAY(300000); 1025dc5df763SJoerg Wunsch /* make ctrlr happy: */ 10266182fdbdSPeter Wemm fd_sense_int(fdc, 0, 0); 1027dc5df763SJoerg Wunsch } 1028dc5df763SJoerg Wunsch } 1029dc5df763SJoerg Wunsch 10306b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) { 10316b7bd95bSJoerg Wunsch /* 10326b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 10336b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 10346b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 10356b7bd95bSJoerg Wunsch * more than 77 steps 10366b7bd95bSJoerg Wunsch */ 1037dc5df763SJoerg Wunsch /* go back to 0: */ 10386182fdbdSPeter Wemm if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 10396b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 10406b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000); 10415b81b6b3SRodney W. Grimes 10426b7bd95bSJoerg Wunsch /* anything responding? */ 1043dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 1044dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 10456b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 10466b7bd95bSJoerg Wunsch } 1047dc5df763SJoerg Wunsch } 10486b7bd95bSJoerg Wunsch 10496182fdbdSPeter Wemm set_motor(fdc, fdsu, TURNOFF); 10503a2f7427SDavid Greenman 10513a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 10526182fdbdSPeter Wemm return (ENXIO); 10535b81b6b3SRodney W. Grimes 1054dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 1055b99f0a4aSAndrew Moore fd->fdc = fdc; 1056b99f0a4aSAndrew Moore fd->fdsu = fdsu; 10573a2f7427SDavid Greenman fd->options = 0; 105802a19910SJustin T. Gibbs callout_handle_init(&fd->toffhandle); 105902a19910SJustin T. Gibbs callout_handle_init(&fd->tohandle); 10605b81b6b3SRodney W. Grimes 1061b99f0a4aSAndrew Moore switch (fdt) { 10627ca0641bSAndrey A. Chernov case RTCFDT_12M: 10636182fdbdSPeter Wemm device_set_desc(dev, "1200-KB 5.25\" drive"); 1064b99f0a4aSAndrew Moore fd->type = FD_1200; 10657ca0641bSAndrey A. Chernov break; 10660722d6abSJoerg Wunsch case RTCFDT_144M | RTCFDT_144M_PRETENDED: 10676182fdbdSPeter Wemm device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive"); 10680722d6abSJoerg Wunsch fdt = RTCFDT_144M; 10696182fdbdSPeter Wemm fd->type = FD_1440; 10707ca0641bSAndrey A. Chernov case RTCFDT_144M: 10716182fdbdSPeter Wemm device_set_desc(dev, "1440-KB 3.5\" drive"); 1072b99f0a4aSAndrew Moore fd->type = FD_1440; 10737ca0641bSAndrey A. Chernov break; 1074290dd077SJoerg Wunsch case RTCFDT_288M: 107586a727d9SJoerg Wunsch case RTCFDT_288M_1: 10766182fdbdSPeter Wemm device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1077290dd077SJoerg Wunsch fd->type = FD_1440; 1078290dd077SJoerg Wunsch break; 10797ca0641bSAndrey A. Chernov case RTCFDT_360K: 10806182fdbdSPeter Wemm device_set_desc(dev, "360-KB 5.25\" drive"); 1081b99f0a4aSAndrew Moore fd->type = FD_360; 10827ca0641bSAndrey A. Chernov break; 1083ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 10846182fdbdSPeter Wemm printf("720-KB 3.5\" drive"); 1085b99f0a4aSAndrew Moore fd->type = FD_720; 1086ed2fa05eSAndrey A. Chernov break; 10877ca0641bSAndrey A. Chernov default: 10886182fdbdSPeter Wemm return (ENXIO); 10895b81b6b3SRodney W. Grimes } 10906182fdbdSPeter Wemm return (0); 10916182fdbdSPeter Wemm } 10926182fdbdSPeter Wemm 10936182fdbdSPeter Wemm static int 10946182fdbdSPeter Wemm fd_attach(device_t dev) 10956182fdbdSPeter Wemm { 10966182fdbdSPeter Wemm struct fd_data *fd; 1097ada9bd8cSJulian Elischer #if 0 1098ada9bd8cSJulian Elischer int i; 1099ada9bd8cSJulian Elischer int mynor; 1100ada9bd8cSJulian Elischer int typemynor; 1101ada9bd8cSJulian Elischer int typesize; 1102ada9bd8cSJulian Elischer #endif 11036182fdbdSPeter Wemm 11046182fdbdSPeter Wemm fd = device_get_softc(dev); 11056182fdbdSPeter Wemm 1106475ad603SPeter Wemm cdevsw_add(&fd_cdevsw); /* XXX */ 1107ada9bd8cSJulian Elischer make_dev(&fd_cdevsw, (fd->fdu << 6), 1108ada9bd8cSJulian Elischer UID_ROOT, GID_OPERATOR, 0640, "rfd%d", fd->fdu); 1109ada9bd8cSJulian Elischer 1110ada9bd8cSJulian Elischer #if 0 1111ada9bd8cSJulian Elischer /* Other make_dev() go here. */ 1112ada9bd8cSJulian Elischer #endif 1113ada9bd8cSJulian Elischer 1114671e2ceeSBruce Evans /* 1115b2dfb1f9SJustin T. Gibbs * Export the drive to the devstat interface. 1116671e2ceeSBruce Evans */ 11176182fdbdSPeter Wemm devstat_add_entry(&fd->device_stats, device_get_name(dev), 11186182fdbdSPeter Wemm device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS, 11192a888f93SKenneth D. Merry DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 11202a888f93SKenneth D. Merry DEVSTAT_PRIORITY_FD); 11216182fdbdSPeter Wemm return (0); 11225b81b6b3SRodney W. Grimes } 11235b81b6b3SRodney W. Grimes 11245f830ea2SJoerg Wunsch static int 11255f830ea2SJoerg Wunsch fd_detach(device_t dev) 11265f830ea2SJoerg Wunsch { 11275f830ea2SJoerg Wunsch struct fd_data *fd; 11285f830ea2SJoerg Wunsch 11295f830ea2SJoerg Wunsch fd = device_get_softc(dev); 11305f830ea2SJoerg Wunsch untimeout(fd_turnoff, fd, fd->toffhandle); 11315f830ea2SJoerg Wunsch 11325f830ea2SJoerg Wunsch return (0); 11335f830ea2SJoerg Wunsch } 11345f830ea2SJoerg Wunsch 113516e68fc6SPeter Wemm static device_method_t fd_methods[] = { 113616e68fc6SPeter Wemm /* Device interface */ 113716e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe), 113816e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach), 11395f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach), 114016e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown), 114116e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 114216e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 114316e68fc6SPeter Wemm 114416e68fc6SPeter Wemm { 0, 0 } 114516e68fc6SPeter Wemm }; 114616e68fc6SPeter Wemm 114716e68fc6SPeter Wemm static driver_t fd_driver = { 114816e68fc6SPeter Wemm "fd", 114916e68fc6SPeter Wemm fd_methods, 115016e68fc6SPeter Wemm sizeof(struct fd_data) 115116e68fc6SPeter Wemm }; 115216e68fc6SPeter Wemm 1153475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 115416e68fc6SPeter Wemm 11555b81b6b3SRodney W. Grimes /****************************************************************************/ 11565b81b6b3SRodney W. Grimes /* motor control stuff */ 11575e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 11585b81b6b3SRodney W. Grimes /****************************************************************************/ 11593a2f7427SDavid Greenman static void 11606182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon) 11615b81b6b3SRodney W. Grimes { 11626182fdbdSPeter Wemm int fdout = fdc->fdout; 11633a2f7427SDavid Greenman int needspecify = 0; 11643a2f7427SDavid Greenman 11653a2f7427SDavid Greenman if(turnon) { 11663a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 11673a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 11683a2f7427SDavid Greenman } else 11693a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 11703a2f7427SDavid Greenman 11715e235068SJordan K. Hubbard if(!turnon 11725e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 11735e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 11745e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 11755e235068SJordan K. Hubbard else { 11763a2f7427SDavid Greenman /* make sure controller is selected and specified */ 11773a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 11783a2f7427SDavid Greenman needspecify = 1; 11793a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 11805b81b6b3SRodney W. Grimes } 11815b81b6b3SRodney W. Grimes 1182427ccf00SDoug Rabson fdout_wr(fdc, fdout); 11836182fdbdSPeter Wemm fdc->fdout = fdout; 11843a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 11853a2f7427SDavid Greenman 11863a2f7427SDavid Greenman if (needspecify) { 1187dc8603e3SJoerg Wunsch /* 1188dc5df763SJoerg Wunsch * XXX 1189dc8603e3SJoerg Wunsch * special case: since we have just woken up the FDC 1190dc8603e3SJoerg Wunsch * from its sleep, we silently assume the command will 1191dc8603e3SJoerg Wunsch * be accepted, and do not test for a timeout 1192dc8603e3SJoerg Wunsch */ 11936182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1194dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1195dc5df763SJoerg Wunsch 0); 11966182fdbdSPeter Wemm if (fdc->flags & FDC_HAS_FIFO) 11976182fdbdSPeter Wemm (void) enable_fifo(fdc); 11983a2f7427SDavid Greenman } 11993a2f7427SDavid Greenman } 12003a2f7427SDavid Greenman 1201381fe1aaSGarrett Wollman static void 12026182fdbdSPeter Wemm fd_turnoff(void *xfd) 12035b81b6b3SRodney W. Grimes { 1204f5f7ba03SJordan K. Hubbard int s; 12056182fdbdSPeter Wemm fd_p fd = xfd; 1206dc16046fSJoerg Wunsch 12076182fdbdSPeter Wemm TRACE1("[fd%d: turnoff]", fd->fdu); 12088335c1b8SBruce Evans 12095f830ea2SJoerg Wunsch s = splbio(); 12108335c1b8SBruce Evans /* 12118335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 12125f830ea2SJoerg Wunsch * 12135f830ea2SJoerg Wunsch * If we got here, this could only mean we missed an interrupt. 12145f830ea2SJoerg Wunsch * This can e. g. happen on the Y-E Date PCMCIA floppy controller 12155f830ea2SJoerg Wunsch * after a controller reset. Just schedule a pseudo-interrupt 12165f830ea2SJoerg Wunsch * so the state machine gets re-entered. 12178335c1b8SBruce Evans */ 12186182fdbdSPeter Wemm if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 12195f830ea2SJoerg Wunsch fdc_intr(fd->fdc); 12205f830ea2SJoerg Wunsch splx(s); 12218335c1b8SBruce Evans return; 12228335c1b8SBruce Evans } 12238335c1b8SBruce Evans 12245b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 12256182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNOFF); 1226f5f7ba03SJordan K. Hubbard splx(s); 12275b81b6b3SRodney W. Grimes } 12285b81b6b3SRodney W. Grimes 12293a2f7427SDavid Greenman static void 12306182fdbdSPeter Wemm fd_motor_on(void *xfd) 12315b81b6b3SRodney W. Grimes { 1232f5f7ba03SJordan K. Hubbard int s; 12336182fdbdSPeter Wemm fd_p fd = xfd; 1234f5f7ba03SJordan K. Hubbard 1235f5f7ba03SJordan K. Hubbard s = splbio(); 12365b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 12375b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 12385b81b6b3SRodney W. Grimes { 12396182fdbdSPeter Wemm fdc_intr(fd->fdc); 12405b81b6b3SRodney W. Grimes } 1241f5f7ba03SJordan K. Hubbard splx(s); 12425b81b6b3SRodney W. Grimes } 12435b81b6b3SRodney W. Grimes 12443a2f7427SDavid Greenman static void 12456182fdbdSPeter Wemm fd_turnon(fd_p fd) 12465b81b6b3SRodney W. Grimes { 12475b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 12485b81b6b3SRodney W. Grimes { 12493a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 12506182fdbdSPeter Wemm set_motor(fd->fdc, fd->fdsu, TURNON); 12516182fdbdSPeter Wemm timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 12525b81b6b3SRodney W. Grimes } 12535b81b6b3SRodney W. Grimes } 12545b81b6b3SRodney W. Grimes 1255381fe1aaSGarrett Wollman static void 1256dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 12575b81b6b3SRodney W. Grimes { 12583a2f7427SDavid Greenman /* Try a reset, keep motor on */ 1259427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 12603a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 12613a2f7427SDavid Greenman DELAY(100); 12623a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 1263427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 12643a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 12653a2f7427SDavid Greenman DELAY(100); 1266427ccf00SDoug Rabson fdout_wr(fdc, fdc->fdout); 12673a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 12683a2f7427SDavid Greenman 1269dc5df763SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 12706182fdbdSPeter Wemm (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1271dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1272dc5df763SJoerg Wunsch 0); 1273d66c374fSTor Egge if (fdc->flags & FDC_HAS_FIFO) 1274d66c374fSTor Egge (void) enable_fifo(fdc); 12755b81b6b3SRodney W. Grimes } 12765b81b6b3SRodney W. Grimes 12775b81b6b3SRodney W. Grimes /****************************************************************************/ 12785b81b6b3SRodney W. Grimes /* fdc in/out */ 12795b81b6b3SRodney W. Grimes /****************************************************************************/ 12805b81b6b3SRodney W. Grimes int 12816182fdbdSPeter Wemm in_fdc(struct fdc_data *fdc) 12825b81b6b3SRodney W. Grimes { 12835b81b6b3SRodney W. Grimes int i, j = 100000; 1284427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 12855b81b6b3SRodney W. Grimes != (NE7_DIO|NE7_RQM) && j-- > 0) 1286dc5df763SJoerg Wunsch if (i == NE7_RQM) 12876182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 12885b81b6b3SRodney W. Grimes if (j <= 0) 12896182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1290d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1291427ccf00SDoug Rabson i = fddata_rd(fdc); 12923a2f7427SDavid Greenman TRACE1("[FDDATA->0x%x]", (unsigned char)i); 12935b81b6b3SRodney W. Grimes return(i); 1294d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1295427ccf00SDoug Rabson return fddata_rd(fdc); 1296d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 12975b81b6b3SRodney W. Grimes } 12985b81b6b3SRodney W. Grimes 1299dc5df763SJoerg Wunsch /* 1300dc5df763SJoerg Wunsch * fd_in: Like in_fdc, but allows you to see if it worked. 1301dc5df763SJoerg Wunsch */ 1302b5e8ce9fSBruce Evans static int 13036182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr) 1304dc5df763SJoerg Wunsch { 1305dc5df763SJoerg Wunsch int i, j = 100000; 1306427ccf00SDoug Rabson while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) 1307dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 1308dc5df763SJoerg Wunsch if (i == NE7_RQM) 13096182fdbdSPeter Wemm return fdc_err(fdc, "ready for output in input\n"); 1310dc5df763SJoerg Wunsch if (j <= 0) 13116182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "input ready timeout\n": 0); 1312d2fb4892SJoerg Wunsch #ifdef FDC_DEBUG 1313427ccf00SDoug Rabson i = fddata_rd(fdc); 1314dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1315dc5df763SJoerg Wunsch *ptr = i; 1316dc5df763SJoerg Wunsch return 0; 1317d2fb4892SJoerg Wunsch #else /* !FDC_DEBUG */ 1318427ccf00SDoug Rabson i = fddata_rd(fdc); 1319dc5df763SJoerg Wunsch if (ptr) 1320dc5df763SJoerg Wunsch *ptr = i; 1321dc5df763SJoerg Wunsch return 0; 1322d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */ 1323dc5df763SJoerg Wunsch } 1324dc5df763SJoerg Wunsch 1325dc5df763SJoerg Wunsch int 13266182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x) 13275b81b6b3SRodney W. Grimes { 13283b3837dbSRodney W. Grimes int i; 13295b81b6b3SRodney W. Grimes 13303b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 13313b3837dbSRodney W. Grimes i = 100000; 1332427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0); 13336182fdbdSPeter Wemm if (i <= 0) return fdc_err(fdc, "direction bit not set\n"); 13343b3837dbSRodney W. Grimes 13353b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 13363b3837dbSRodney W. Grimes i = 100000; 1337427ccf00SDoug Rabson while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0); 133816b04b6aSJoerg Wunsch if (i <= 0) 13396182fdbdSPeter Wemm return fdc_err(fdc, bootverbose? "output ready timeout\n": 0); 13403b3837dbSRodney W. Grimes 13413b3837dbSRodney W. Grimes /* Send the command and return */ 1342427ccf00SDoug Rabson fddata_wr(fdc, x); 13433a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 13445b81b6b3SRodney W. Grimes return (0); 13455b81b6b3SRodney W. Grimes } 13465b81b6b3SRodney W. Grimes 13475b81b6b3SRodney W. Grimes /****************************************************************************/ 13485b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 13495b81b6b3SRodney W. Grimes /****************************************************************************/ 1350381fe1aaSGarrett Wollman int 1351671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 13525b81b6b3SRodney W. Grimes { 13535b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 135420a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 13556182fdbdSPeter Wemm fd_p fd; 1356b99f0a4aSAndrew Moore fdc_p fdc; 13575b81b6b3SRodney W. Grimes 13585b81b6b3SRodney W. Grimes /* check bounds */ 13596182fdbdSPeter Wemm if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1360b99f0a4aSAndrew Moore return (ENXIO); 13616182fdbdSPeter Wemm fdc = fd->fdc; 13626182fdbdSPeter Wemm if ((fdc == NULL) || (fd->type == NO_TYPE)) 1363b99f0a4aSAndrew Moore return (ENXIO); 1364b99f0a4aSAndrew Moore if (type > NUMDENS) 1365b99f0a4aSAndrew Moore return (ENXIO); 13667ca0641bSAndrey A. Chernov if (type == 0) 13676182fdbdSPeter Wemm type = fd->type; 13687ca0641bSAndrey A. Chernov else { 13693e425b96SJulian Elischer /* 13703e425b96SJulian Elischer * For each type of basic drive, make sure we are trying 13713e425b96SJulian Elischer * to open a type it can do, 13723e425b96SJulian Elischer */ 13736182fdbdSPeter Wemm if (type != fd->type) { 13746182fdbdSPeter Wemm switch (fd->type) { 13757ca0641bSAndrey A. Chernov case FD_360: 13767ca0641bSAndrey A. Chernov return (ENXIO); 1377ed2fa05eSAndrey A. Chernov case FD_720: 1378b39c878eSAndrey A. Chernov if ( type != FD_820 1379b39c878eSAndrey A. Chernov && type != FD_800 13806fb89845SKATO Takenori && type != FD_640 1381ed2fa05eSAndrey A. Chernov ) 1382ed2fa05eSAndrey A. Chernov return (ENXIO); 1383ed2fa05eSAndrey A. Chernov break; 13847ca0641bSAndrey A. Chernov case FD_1200: 1385b39c878eSAndrey A. Chernov switch (type) { 1386b39c878eSAndrey A. Chernov case FD_1480: 1387b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1388fa4700b4SAndrey A. Chernov break; 13897ca0641bSAndrey A. Chernov case FD_1440: 1390b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1391b39c878eSAndrey A. Chernov break; 13926fb89845SKATO Takenori case FD_1232: 13936fb89845SKATO Takenori break; 1394b39c878eSAndrey A. Chernov case FD_820: 1395b39c878eSAndrey A. Chernov type = FD_820in5_25; 1396b39c878eSAndrey A. Chernov break; 1397b39c878eSAndrey A. Chernov case FD_800: 1398b39c878eSAndrey A. Chernov type = FD_800in5_25; 1399b39c878eSAndrey A. Chernov break; 1400b39c878eSAndrey A. Chernov case FD_720: 1401b39c878eSAndrey A. Chernov type = FD_720in5_25; 1402b39c878eSAndrey A. Chernov break; 14036fb89845SKATO Takenori case FD_640: 14046fb89845SKATO Takenori type = FD_640in5_25; 14056fb89845SKATO Takenori break; 1406b39c878eSAndrey A. Chernov case FD_360: 1407b39c878eSAndrey A. Chernov type = FD_360in5_25; 1408b39c878eSAndrey A. Chernov break; 1409b39c878eSAndrey A. Chernov default: 1410b39c878eSAndrey A. Chernov return(ENXIO); 1411b39c878eSAndrey A. Chernov } 1412b39c878eSAndrey A. Chernov break; 1413b39c878eSAndrey A. Chernov case FD_1440: 1414b39c878eSAndrey A. Chernov if ( type != FD_1720 1415b39c878eSAndrey A. Chernov && type != FD_1480 1416ed2fa05eSAndrey A. Chernov && type != FD_1200 1417b39c878eSAndrey A. Chernov && type != FD_820 1418b39c878eSAndrey A. Chernov && type != FD_800 1419b39c878eSAndrey A. Chernov && type != FD_720 14206fb89845SKATO Takenori && type != FD_640 14217ca0641bSAndrey A. Chernov ) 1422dffff499SAndrey A. Chernov return(ENXIO); 1423fa4700b4SAndrey A. Chernov break; 14247ca0641bSAndrey A. Chernov } 14257ca0641bSAndrey A. Chernov } 1426fa4700b4SAndrey A. Chernov } 14276182fdbdSPeter Wemm fd->ft = fd_types + type - 1; 14286182fdbdSPeter Wemm fd->flags |= FD_OPEN; 14295f830ea2SJoerg Wunsch 14305b81b6b3SRodney W. Grimes return 0; 14315b81b6b3SRodney W. Grimes } 14325b81b6b3SRodney W. Grimes 1433381fe1aaSGarrett Wollman int 1434671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 14355b81b6b3SRodney W. Grimes { 14365b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 14376182fdbdSPeter Wemm struct fd_data *fd; 1438b99f0a4aSAndrew Moore 14396182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14406182fdbdSPeter Wemm fd->flags &= ~FD_OPEN; 14416182fdbdSPeter Wemm fd->options &= ~FDOPT_NORETRY; 1442dc16046fSJoerg Wunsch 14435b81b6b3SRodney W. Grimes return (0); 14445b81b6b3SRodney W. Grimes } 14455b81b6b3SRodney W. Grimes 14463a2f7427SDavid Greenman /****************************************************************************/ 14473a2f7427SDavid Greenman /* fdstrategy */ 14483a2f7427SDavid Greenman /****************************************************************************/ 14493a2f7427SDavid Greenman void 14503a2f7427SDavid Greenman fdstrategy(struct buf *bp) 14513a2f7427SDavid Greenman { 1452bb6382faSJoerg Wunsch unsigned nblocks, blknum, cando; 14533a2f7427SDavid Greenman int s; 14543a2f7427SDavid Greenman fdu_t fdu; 14553a2f7427SDavid Greenman fdc_p fdc; 14563a2f7427SDavid Greenman fd_p fd; 14573a2f7427SDavid Greenman size_t fdblk; 14583a2f7427SDavid Greenman 14593a2f7427SDavid Greenman fdu = FDUNIT(minor(bp->b_dev)); 14606182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 14616182fdbdSPeter Wemm if (fd == 0) 14626182fdbdSPeter Wemm panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 14636182fdbdSPeter Wemm (u_long)major(bp->b_dev), (u_long)minor(bp->b_dev)); 14643a2f7427SDavid Greenman fdc = fd->fdc; 146569acd21dSWarner Losh if (fd->type == NO_TYPE) { 146669acd21dSWarner Losh bp->b_error = ENXIO; 146769acd21dSWarner Losh bp->b_flags |= B_ERROR; 14683b178206SWarner Losh goto bad; 146969acd21dSWarner Losh }; 14703a2f7427SDavid Greenman 1471d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 14723a2f7427SDavid Greenman if (!(bp->b_flags & B_FORMAT)) { 14736182fdbdSPeter Wemm if (bp->b_blkno < 0) { 1474dc5df763SJoerg Wunsch printf( 14756a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 1476702c623aSPoul-Henning Kamp fdu, (u_long)bp->b_blkno, bp->b_bcount); 14773a2f7427SDavid Greenman bp->b_error = EINVAL; 14783a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 14793a2f7427SDavid Greenman goto bad; 14803a2f7427SDavid Greenman } 14813a2f7427SDavid Greenman if ((bp->b_bcount % fdblk) != 0) { 14823a2f7427SDavid Greenman bp->b_error = EINVAL; 14833a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 14843a2f7427SDavid Greenman goto bad; 14853a2f7427SDavid Greenman } 14863a2f7427SDavid Greenman } 14873a2f7427SDavid Greenman 14883a2f7427SDavid Greenman /* 14893a2f7427SDavid Greenman * Set up block calculations. 14903a2f7427SDavid Greenman */ 1491bb6382faSJoerg Wunsch if (bp->b_blkno > 20000000) { 1492bb6382faSJoerg Wunsch /* 1493bb6382faSJoerg Wunsch * Reject unreasonably high block number, prevent the 1494bb6382faSJoerg Wunsch * multiplication below from overflowing. 1495bb6382faSJoerg Wunsch */ 1496bb6382faSJoerg Wunsch bp->b_error = EINVAL; 14973a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 14983a2f7427SDavid Greenman goto bad; 14993a2f7427SDavid Greenman } 1500bb6382faSJoerg Wunsch blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk; 1501bb6382faSJoerg Wunsch nblocks = fd->ft->size; 1502bb6382faSJoerg Wunsch bp->b_resid = 0; 1503bb6382faSJoerg Wunsch if (blknum + (bp->b_bcount / fdblk) > nblocks) { 1504bb6382faSJoerg Wunsch if (blknum <= nblocks) { 1505bb6382faSJoerg Wunsch cando = (nblocks - blknum) * fdblk; 1506bb6382faSJoerg Wunsch bp->b_resid = bp->b_bcount - cando; 1507bb6382faSJoerg Wunsch if (cando == 0) 1508bb6382faSJoerg Wunsch goto bad; /* not actually bad but EOF */ 1509bb6382faSJoerg Wunsch } else { 1510bb6382faSJoerg Wunsch bp->b_error = EINVAL; 1511bb6382faSJoerg Wunsch bp->b_flags |= B_ERROR; 1512bb6382faSJoerg Wunsch goto bad; 1513bb6382faSJoerg Wunsch } 1514bb6382faSJoerg Wunsch } 151527513ca7SBruce Evans bp->b_pblkno = bp->b_blkno; 15163a2f7427SDavid Greenman s = splbio(); 151702a19910SJustin T. Gibbs bufqdisksort(&fdc->head, bp); 15186182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1519b2dfb1f9SJustin T. Gibbs 1520b2dfb1f9SJustin T. Gibbs /* Tell devstat we are starting on the transaction */ 1521b2dfb1f9SJustin T. Gibbs devstat_start_transaction(&fd->device_stats); 15225f830ea2SJoerg Wunsch device_busy(fd->dev); 1523b2dfb1f9SJustin T. Gibbs 15246182fdbdSPeter Wemm fdstart(fdc); 15253a2f7427SDavid Greenman splx(s); 15263a2f7427SDavid Greenman return; 15273a2f7427SDavid Greenman 15283a2f7427SDavid Greenman bad: 15293a2f7427SDavid Greenman biodone(bp); 15303a2f7427SDavid Greenman } 15313a2f7427SDavid Greenman 15325b81b6b3SRodney W. Grimes /***************************************************************\ 15335b81b6b3SRodney W. Grimes * fdstart * 15345b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 15355b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 15365b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 15375b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 15385b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 15395b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 15405b81b6b3SRodney W. Grimes \***************************************************************/ 1541381fe1aaSGarrett Wollman static void 15426182fdbdSPeter Wemm fdstart(struct fdc_data *fdc) 15435b81b6b3SRodney W. Grimes { 15445b81b6b3SRodney W. Grimes int s; 15455b81b6b3SRodney W. Grimes 15465b81b6b3SRodney W. Grimes s = splbio(); 15476182fdbdSPeter Wemm if(fdc->state == DEVIDLE) 15485b81b6b3SRodney W. Grimes { 15496182fdbdSPeter Wemm fdc_intr(fdc); 15505b81b6b3SRodney W. Grimes } 15515b81b6b3SRodney W. Grimes splx(s); 15525b81b6b3SRodney W. Grimes } 15535b81b6b3SRodney W. Grimes 1554381fe1aaSGarrett Wollman static void 15556182fdbdSPeter Wemm fd_iotimeout(void *xfdc) 15565b81b6b3SRodney W. Grimes { 15575c1a1eaeSBruce Evans fdc_p fdc; 1558f5f7ba03SJordan K. Hubbard int s; 15595b81b6b3SRodney W. Grimes 15606182fdbdSPeter Wemm fdc = xfdc; 15615c1a1eaeSBruce Evans TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 15625b81b6b3SRodney W. Grimes 15633a2f7427SDavid Greenman /* 15643a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 15653a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 15663a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 15673a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 15683a2f7427SDavid Greenman * Many thanks, Big Blue! 15695c1a1eaeSBruce Evans * The FDC must not be reset directly, since that would 15705c1a1eaeSBruce Evans * interfere with the state machine. Instead, pretend that 15715c1a1eaeSBruce Evans * the command completed but was invalid. The state machine 15725c1a1eaeSBruce Evans * will reset the FDC and retry once. 15733a2f7427SDavid Greenman */ 15743a2f7427SDavid Greenman s = splbio(); 15755c1a1eaeSBruce Evans fdc->status[0] = NE7_ST0_IC_IV; 15765c1a1eaeSBruce Evans fdc->flags &= ~FDC_STAT_VALID; 15775c1a1eaeSBruce Evans fdc->state = IOTIMEDOUT; 15786182fdbdSPeter Wemm fdc_intr(fdc); 1579f5f7ba03SJordan K. Hubbard splx(s); 15805b81b6b3SRodney W. Grimes } 15815b81b6b3SRodney W. Grimes 15825b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1583381fe1aaSGarrett Wollman static void 15846182fdbdSPeter Wemm fd_pseudointr(void *xfdc) 15855b81b6b3SRodney W. Grimes { 15865b81b6b3SRodney W. Grimes int s; 15873a2f7427SDavid Greenman 15885b81b6b3SRodney W. Grimes s = splbio(); 15896182fdbdSPeter Wemm fdc_intr(xfdc); 15905b81b6b3SRodney W. Grimes splx(s); 15915b81b6b3SRodney W. Grimes } 15925b81b6b3SRodney W. Grimes 15935b81b6b3SRodney W. Grimes /***********************************************************************\ 15945b81b6b3SRodney W. Grimes * fdintr * 15955b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 15965b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 15975b81b6b3SRodney W. Grimes \***********************************************************************/ 1598fe310de8SBruce Evans static void 15996182fdbdSPeter Wemm fdc_intr(void *xfdc) 16005b81b6b3SRodney W. Grimes { 16016182fdbdSPeter Wemm fdc_p fdc = xfdc; 16026182fdbdSPeter Wemm while(fdstate(fdc)) 1603381fe1aaSGarrett Wollman ; 16045b81b6b3SRodney W. Grimes } 16055b81b6b3SRodney W. Grimes 160669acd21dSWarner Losh /* 160769acd21dSWarner Losh * magic pseudo-DMA initialization for YE FDC. Sets count and 160869acd21dSWarner Losh * direction 160969acd21dSWarner Losh */ 16103b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \ 16113b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 16123b178206SWarner Losh ((cnt)-1) & 0xff); \ 16133b178206SWarner Losh bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 16143b178206SWarner Losh ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 161569acd21dSWarner Losh 161669acd21dSWarner Losh /* 161769acd21dSWarner Losh * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 161869acd21dSWarner Losh */ 16193b178206SWarner Losh static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 162069acd21dSWarner Losh { 162169acd21dSWarner Losh u_char *cptr = (u_char *)addr; 162269acd21dSWarner Losh 162369acd21dSWarner Losh if (flags & B_READ) { 162469acd21dSWarner Losh if (fdc->state != PIOREAD) { 162569acd21dSWarner Losh fdc->state = PIOREAD; 162669acd21dSWarner Losh return(0); 162769acd21dSWarner Losh }; 16283b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 16293b178206SWarner Losh bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16303b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 163169acd21dSWarner Losh } else { 16323b178206SWarner Losh bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 16333b178206SWarner Losh FDC_YE_DATAPORT, cptr, count); 16343b178206SWarner Losh SET_BCDR(fdc, 0, count, 0); 163569acd21dSWarner Losh }; 163669acd21dSWarner Losh return(1); 163769acd21dSWarner Losh } 163869acd21dSWarner Losh 16395b81b6b3SRodney W. Grimes /***********************************************************************\ 16405b81b6b3SRodney W. Grimes * The controller state machine. * 16415b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 16425b81b6b3SRodney W. Grimes \***********************************************************************/ 16433a2f7427SDavid Greenman static int 16446182fdbdSPeter Wemm fdstate(fdc_p fdc) 16455b81b6b3SRodney W. Grimes { 16465c1a1eaeSBruce Evans int read, format, head, i, sec = 0, sectrac, st0, cyl, st3; 1647bb6382faSJoerg Wunsch unsigned blknum = 0, b_cylinder = 0; 16485b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 16495b81b6b3SRodney W. Grimes fd_p fd; 165017542807SPoul-Henning Kamp register struct buf *bp; 1651b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 16523a2f7427SDavid Greenman size_t fdblk; 16535b81b6b3SRodney W. Grimes 1654e93e63cbSBruce Evans bp = fdc->bp; 1655e93e63cbSBruce Evans if (bp == NULL) { 165602a19910SJustin T. Gibbs bp = bufq_first(&fdc->head); 1657e93e63cbSBruce Evans if (bp != NULL) { 1658e93e63cbSBruce Evans bufq_remove(&fdc->head, bp); 1659e93e63cbSBruce Evans fdc->bp = bp; 1660e93e63cbSBruce Evans } 1661e93e63cbSBruce Evans } 1662e93e63cbSBruce Evans if (bp == NULL) { 16635b81b6b3SRodney W. Grimes /***********************************************\ 16645b81b6b3SRodney W. Grimes * nothing left for this controller to do * 16655b81b6b3SRodney W. Grimes * Force into the IDLE state, * 16665b81b6b3SRodney W. Grimes \***********************************************/ 16675b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 16686182fdbdSPeter Wemm if (fdc->fd) { 1669b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, 1670b6e5f28eSPeter Wemm "unexpected valid fd pointer\n"); 16715b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 16725b81b6b3SRodney W. Grimes fdc->fdu = -1; 16735b81b6b3SRodney W. Grimes } 16746182fdbdSPeter Wemm TRACE1("[fdc%d IDLE]", fdc->fdcu); 16755b81b6b3SRodney W. Grimes return (0); 16765b81b6b3SRodney W. Grimes } 16775b81b6b3SRodney W. Grimes fdu = FDUNIT(minor(bp->b_dev)); 16786182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 16793a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1680b6e5f28eSPeter Wemm if (fdc->fd && (fd != fdc->fd)) 1681b6e5f28eSPeter Wemm device_printf(fd->dev, "confused fd pointers\n"); 16825b81b6b3SRodney W. Grimes read = bp->b_flags & B_READ; 1683b39c878eSAndrey A. Chernov format = bp->b_flags & B_FORMAT; 1684bb6382faSJoerg Wunsch if (format) { 1685ab3f7469SPoul-Henning Kamp finfo = (struct fd_formb *)bp->b_data; 1686bb6382faSJoerg Wunsch fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1687bb6382faSJoerg Wunsch - (char *)finfo; 1688bb6382faSJoerg Wunsch } 1689bb6382faSJoerg Wunsch if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 16903e425b96SJulian Elischer blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk + 1691bb6382faSJoerg Wunsch fd->skip/fdblk; 1692bb6382faSJoerg Wunsch b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1693bb6382faSJoerg Wunsch } 16945b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 16955b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 16965b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 16976182fdbdSPeter Wemm untimeout(fd_turnoff, fd, fd->toffhandle); 16986182fdbdSPeter Wemm fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 16995b81b6b3SRodney W. Grimes switch (fdc->state) 17005b81b6b3SRodney W. Grimes { 17015b81b6b3SRodney W. Grimes case DEVIDLE: 17025b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 17035b81b6b3SRodney W. Grimes fdc->retry = 0; 17045b81b6b3SRodney W. Grimes fd->skip = 0; 17055b81b6b3SRodney W. Grimes fdc->fd = fd; 17065b81b6b3SRodney W. Grimes fdc->fdu = fdu; 17075f830ea2SJoerg Wunsch fdc->fdctl_wr(fdc, fd->ft->trans); 17083a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 17095b81b6b3SRodney W. Grimes /*******************************************************\ 17105b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 1711dc733423SDag-Erling Smørgrav * it will start up in its own good time * 17125b81b6b3SRodney W. Grimes \*******************************************************/ 17136182fdbdSPeter Wemm if(fd->flags & FD_MOTOR_WAIT) { 17145b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17155b81b6b3SRodney W. Grimes return (0); /* come back later */ 17165b81b6b3SRodney W. Grimes } 17175b81b6b3SRodney W. Grimes /*******************************************************\ 17185b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 17195b81b6b3SRodney W. Grimes \*******************************************************/ 17205b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 17215b81b6b3SRodney W. Grimes { 17225b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 17236182fdbdSPeter Wemm fd_turnon(fd); 17245b81b6b3SRodney W. Grimes return (0); 17255b81b6b3SRodney W. Grimes } 17265b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 17275b81b6b3SRodney W. Grimes { 17286182fdbdSPeter Wemm set_motor(fdc, fd->fdsu, TURNON); 17295b81b6b3SRodney W. Grimes } 17305c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 17315c1a1eaeSBruce Evans fdc->state = RESETCTLR; 17325c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 17335c1a1eaeSBruce Evans } else 17345e235068SJordan K. Hubbard fdc->state = DOSEEK; 17355b81b6b3SRodney W. Grimes break; 17365b81b6b3SRodney W. Grimes case DOSEEK: 1737bb6382faSJoerg Wunsch if (b_cylinder == (unsigned)fd->track) 17385b81b6b3SRodney W. Grimes { 17395b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17405b81b6b3SRodney W. Grimes break; 17415b81b6b3SRodney W. Grimes } 17426182fdbdSPeter Wemm if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1743bb6382faSJoerg Wunsch fd->fdsu, b_cylinder * fd->ft->steptrac, 1744dc5df763SJoerg Wunsch 0)) 1745dc8603e3SJoerg Wunsch { 1746dc8603e3SJoerg Wunsch /* 1747dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1748dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1749dc8603e3SJoerg Wunsch */ 1750dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 17516182fdbdSPeter Wemm return(retrier(fdc)); 1752dc8603e3SJoerg Wunsch } 1753dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 17545b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 17555b81b6b3SRodney W. Grimes return(0); /* will return later */ 17565b81b6b3SRodney W. Grimes case SEEKWAIT: 17575b81b6b3SRodney W. Grimes /* allow heads to settle */ 17586182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 16); 17595b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17605b81b6b3SRodney W. Grimes return(0); /* will return later */ 17615b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 17625b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 17636182fdbdSPeter Wemm if(fd->track == FD_NO_TRACK) { 1764bb6382faSJoerg Wunsch int descyl = b_cylinder * fd->ft->steptrac; 17653a2f7427SDavid Greenman do { 17663a2f7427SDavid Greenman /* 1767dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1768dc5df763SJoerg Wunsch * which cannot really happen since the 1769dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1770dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1771dc5df763SJoerg Wunsch * line, so do one of the following: 1772dc5df763SJoerg Wunsch * 1773dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1774dc5df763SJoerg Wunsch * known to not go stuck after responding 1775dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1776dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1777dc5df763SJoerg Wunsch * real interrupt condition. 1778dc5df763SJoerg Wunsch * 1779dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1780dc5df763SJoerg Wunsch * up immediately. The controller will 1781dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1782dc5df763SJoerg Wunsch * conditions right after reset (for the 1783dc5df763SJoerg Wunsch * corresponding four drives), so this is 1784dc5df763SJoerg Wunsch * our only chance to get notice that it 1785dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 17863a2f7427SDavid Greenman */ 1787dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1788dc5df763SJoerg Wunsch == FD_NOT_VALID) 1789dc5df763SJoerg Wunsch return 0; 1790dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1791dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1792dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 17933a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1794dc5df763SJoerg Wunsch 17956182fdbdSPeter Wemm if (0 == descyl) { 1796dc5df763SJoerg Wunsch int failed = 0; 17973a2f7427SDavid Greenman /* 17983a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 17993a2f7427SDavid Greenman * really there 18003a2f7427SDavid Greenman */ 1801dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1802dc5df763SJoerg Wunsch failed = 1; 18033a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 18043a2f7427SDavid Greenman printf( 18053a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 18063a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1807dc5df763SJoerg Wunsch failed = 1; 1808dc5df763SJoerg Wunsch } 1809dc5df763SJoerg Wunsch 18106182fdbdSPeter Wemm if (failed) { 18113a2f7427SDavid Greenman if(fdc->retry < 3) 18123a2f7427SDavid Greenman fdc->retry = 3; 18136182fdbdSPeter Wemm return (retrier(fdc)); 18143a2f7427SDavid Greenman } 18153a2f7427SDavid Greenman } 1816dc5df763SJoerg Wunsch 18176182fdbdSPeter Wemm if (cyl != descyl) { 18183a2f7427SDavid Greenman printf( 18193a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 18202d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 1821e5d7d243SBruce Evans if (fdc->retry < 3) 1822e5d7d243SBruce Evans fdc->retry = 3; 18236182fdbdSPeter Wemm return (retrier(fdc)); 18245b81b6b3SRodney W. Grimes } 18255b81b6b3SRodney W. Grimes } 18265b81b6b3SRodney W. Grimes 1827bb6382faSJoerg Wunsch fd->track = b_cylinder; 18283b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 1829ab3f7469SPoul-Henning Kamp isa_dmastart(bp->b_flags, bp->b_data+fd->skip, 18303a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 18315b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 18325b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 18335b81b6b3SRodney W. Grimes head = sec / sectrac; 18345b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 18353a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 18363a2f7427SDavid Greenman 18373a2f7427SDavid Greenman if(format || !read) 18383a2f7427SDavid Greenman { 18393a2f7427SDavid Greenman /* make sure the drive is writable */ 1840dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1841dc8603e3SJoerg Wunsch { 1842dc8603e3SJoerg Wunsch /* stuck controller? */ 18435f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 18445f830ea2SJoerg Wunsch isa_dmadone(bp->b_flags, 18455f830ea2SJoerg Wunsch bp->b_data + fd->skip, 18465c1a1eaeSBruce Evans format ? bp->b_bcount : fdblk, 18475c1a1eaeSBruce Evans fdc->dmachan); 1848dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 18496182fdbdSPeter Wemm return (retrier(fdc)); 1850dc8603e3SJoerg Wunsch } 18513a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 18523a2f7427SDavid Greenman { 18533a2f7427SDavid Greenman /* 18543a2f7427SDavid Greenman * XXX YES! this is ugly. 18553a2f7427SDavid Greenman * in order to force the current operation 18563a2f7427SDavid Greenman * to fail, we will have to fake an FDC 18573a2f7427SDavid Greenman * error - all error handling is done 18583a2f7427SDavid Greenman * by the retrier() 18593a2f7427SDavid Greenman */ 18603a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 18613a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 18623a2f7427SDavid Greenman fdc->status[2] = 0; 18633a2f7427SDavid Greenman fdc->status[3] = fd->track; 18643a2f7427SDavid Greenman fdc->status[4] = head; 18653a2f7427SDavid Greenman fdc->status[5] = sec; 18663a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 18673a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 18683a2f7427SDavid Greenman return (1); 18693a2f7427SDavid Greenman } 18703a2f7427SDavid Greenman } 18715b81b6b3SRodney W. Grimes 18726182fdbdSPeter Wemm if (format) { 18735f830ea2SJoerg Wunsch if (fdc->flags & FDC_NODMA) { 18745f830ea2SJoerg Wunsch /* 18755f830ea2SJoerg Wunsch * This seems to be necessary for 18765f830ea2SJoerg Wunsch * whatever obscure reason; if we omit 18775f830ea2SJoerg Wunsch * it, we end up filling the sector ID 18785f830ea2SJoerg Wunsch * fields of the newly formatted track 18795f830ea2SJoerg Wunsch * entirely with garbage, causing 18805f830ea2SJoerg Wunsch * `wrong cylinder' errors all over 18815f830ea2SJoerg Wunsch * the place when trying to read them 18825f830ea2SJoerg Wunsch * back. 18835f830ea2SJoerg Wunsch * 18845f830ea2SJoerg Wunsch * Umpf. 18855f830ea2SJoerg Wunsch */ 18865f830ea2SJoerg Wunsch SET_BCDR(fdc, 1, bp->b_bcount, 0); 18875f830ea2SJoerg Wunsch 18883b178206SWarner Losh (void)fdcpio(fdc,bp->b_flags, 188969acd21dSWarner Losh bp->b_data+fd->skip, 189069acd21dSWarner Losh bp->b_bcount); 18915f830ea2SJoerg Wunsch 18925f830ea2SJoerg Wunsch } 1893b39c878eSAndrey A. Chernov /* formatting */ 18946182fdbdSPeter Wemm if(fd_cmd(fdc, 6, NE7CMD_FORMAT, head << 2 | fdu, 1895dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1896dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1897dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 18986182fdbdSPeter Wemm finfo->fd_formb_fillbyte, 0)) { 1899dc8603e3SJoerg Wunsch /* controller fell over */ 19005f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 19015f830ea2SJoerg Wunsch isa_dmadone(bp->b_flags, 19025f830ea2SJoerg Wunsch bp->b_data + fd->skip, 19035c1a1eaeSBruce Evans format ? bp->b_bcount : fdblk, 19045c1a1eaeSBruce Evans fdc->dmachan); 1905dc8603e3SJoerg Wunsch fdc->retry = 6; 19066182fdbdSPeter Wemm return (retrier(fdc)); 1907dc8603e3SJoerg Wunsch } 19086182fdbdSPeter Wemm } else { 19093b178206SWarner Losh if (fdc->flags & FDC_NODMA) { 191069acd21dSWarner Losh /* 191169acd21dSWarner Losh * this seems to be necessary even when 191269acd21dSWarner Losh * reading data 191369acd21dSWarner Losh */ 19143b178206SWarner Losh SET_BCDR(fdc, 1, fdblk, 0); 191569acd21dSWarner Losh 191669acd21dSWarner Losh /* 191769acd21dSWarner Losh * perform the write pseudo-DMA before 191869acd21dSWarner Losh * the WRITE command is sent 191969acd21dSWarner Losh */ 192069acd21dSWarner Losh if (!read) 19213b178206SWarner Losh (void)fdcpio(fdc,bp->b_flags, 192269acd21dSWarner Losh bp->b_data+fd->skip, 192369acd21dSWarner Losh fdblk); 192469acd21dSWarner Losh } 19256182fdbdSPeter Wemm if (fd_cmd(fdc, 9, 1926dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1927dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1928dc5df763SJoerg Wunsch fd->track, /* track */ 1929dc5df763SJoerg Wunsch head, 1930dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1931dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1932dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1933dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1934dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 19356182fdbdSPeter Wemm 0)) { 1936dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 19375f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 19385f830ea2SJoerg Wunsch isa_dmadone(bp->b_flags, 19395f830ea2SJoerg Wunsch bp->b_data + fd->skip, 19405c1a1eaeSBruce Evans format ? bp->b_bcount : fdblk, 19415c1a1eaeSBruce Evans fdc->dmachan); 1942dc8603e3SJoerg Wunsch fdc->retry = 6; 19436182fdbdSPeter Wemm return (retrier(fdc)); 19445b81b6b3SRodney W. Grimes } 1945b39c878eSAndrey A. Chernov } 19463b178206SWarner Losh if (fdc->flags & FDC_NODMA) 194769acd21dSWarner Losh /* 194869acd21dSWarner Losh * if this is a read, then simply await interrupt 194969acd21dSWarner Losh * before performing PIO 195069acd21dSWarner Losh */ 19513b178206SWarner Losh if (read && !fdcpio(fdc,bp->b_flags, 195269acd21dSWarner Losh bp->b_data+fd->skip,fdblk)) { 19533b178206SWarner Losh fd->tohandle = timeout(fd_iotimeout, fdc, hz); 195469acd21dSWarner Losh return(0); /* will return later */ 195569acd21dSWarner Losh }; 195669acd21dSWarner Losh 195769acd21dSWarner Losh /* 195869acd21dSWarner Losh * write (or format) operation will fall through and 195969acd21dSWarner Losh * await completion interrupt 196069acd21dSWarner Losh */ 19615b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 19626182fdbdSPeter Wemm fd->tohandle = timeout(fd_iotimeout, fdc, hz); 19635b81b6b3SRodney W. Grimes return (0); /* will return later */ 196469acd21dSWarner Losh case PIOREAD: 196569acd21dSWarner Losh /* 196669acd21dSWarner Losh * actually perform the PIO read. The IOCOMPLETE case 196769acd21dSWarner Losh * removes the timeout for us. 196869acd21dSWarner Losh */ 19693b178206SWarner Losh (void)fdcpio(fdc,bp->b_flags,bp->b_data+fd->skip,fdblk); 197069acd21dSWarner Losh fdc->state = IOCOMPLETE; 197169acd21dSWarner Losh /* FALLTHROUGH */ 19725b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 19736182fdbdSPeter Wemm untimeout(fd_iotimeout, fdc, fd->tohandle); 1974dc5df763SJoerg Wunsch 19756182fdbdSPeter Wemm if (fd_read_status(fdc, fd->fdsu)) { 19765f830ea2SJoerg Wunsch if (!(fdc->flags & FDC_NODMA)) 19775c1a1eaeSBruce Evans isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 19785c1a1eaeSBruce Evans format ? bp->b_bcount : fdblk, 19795c1a1eaeSBruce Evans fdc->dmachan); 1980dc5df763SJoerg Wunsch if (fdc->retry < 6) 1981dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 19826182fdbdSPeter Wemm return (retrier(fdc)); 19835b81b6b3SRodney W. Grimes } 1984dc5df763SJoerg Wunsch 19853a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 1986dc5df763SJoerg Wunsch 19873a2f7427SDavid Greenman /* FALLTHROUGH */ 1988dc5df763SJoerg Wunsch 19893a2f7427SDavid Greenman case IOTIMEDOUT: 19903b178206SWarner Losh if (!(fdc->flags & FDC_NODMA)) 1991ab3f7469SPoul-Henning Kamp isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 19923a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 19936182fdbdSPeter Wemm if (fdc->status[0] & NE7_ST0_IC) { 19943a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 19953a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 1996b39c878eSAndrey A. Chernov /* 19973a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 19983a2f7427SDavid Greenman * and didn't release it in time for the 19993a2f7427SDavid Greenman * next FDC transfer. 20003a2f7427SDavid Greenman * Just restart it, don't increment retry 20013a2f7427SDavid Greenman * count. (vak) 2002b39c878eSAndrey A. Chernov */ 2003b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 2004b39c878eSAndrey A. Chernov return (1); 2005b39c878eSAndrey A. Chernov } 20063a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 20073a2f7427SDavid Greenman && fdc->retry < 6) 20083a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 20093a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 20103a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 20113a2f7427SDavid Greenman && fdc->retry < 3) 20123a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 20136182fdbdSPeter Wemm return (retrier(fdc)); 20145b81b6b3SRodney W. Grimes } 20155b81b6b3SRodney W. Grimes /* All OK */ 20163a2f7427SDavid Greenman fd->skip += fdblk; 20176182fdbdSPeter Wemm if (!format && fd->skip < bp->b_bcount - bp->b_resid) { 20185b81b6b3SRodney W. Grimes /* set up next transfer */ 20195b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20206182fdbdSPeter Wemm } else { 20215b81b6b3SRodney W. Grimes /* ALL DONE */ 20225b81b6b3SRodney W. Grimes fd->skip = 0; 2023e93e63cbSBruce Evans fdc->bp = NULL; 20245f830ea2SJoerg Wunsch device_unbusy(fd->dev); 20252186cd9eSPoul-Henning Kamp devstat_end_transaction_buf(&fd->device_stats, bp); 20265b81b6b3SRodney W. Grimes biodone(bp); 20275b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 20285b81b6b3SRodney W. Grimes fdc->fdu = -1; 20295b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 20305b81b6b3SRodney W. Grimes } 20315b81b6b3SRodney W. Grimes return (1); 20325b81b6b3SRodney W. Grimes case RESETCTLR: 20333a2f7427SDavid Greenman fdc_reset(fdc); 20345b81b6b3SRodney W. Grimes fdc->retry++; 20355c1a1eaeSBruce Evans fdc->state = RESETCOMPLETE; 20365c1a1eaeSBruce Evans return (0); 20375c1a1eaeSBruce Evans case RESETCOMPLETE: 20385c1a1eaeSBruce Evans /* 20395c1a1eaeSBruce Evans * Discard all the results from the reset so that they 20405c1a1eaeSBruce Evans * can't cause an unexpected interrupt later. 20415c1a1eaeSBruce Evans */ 20420e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 20430e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 20445c1a1eaeSBruce Evans fdc->state = STARTRECAL; 20455c1a1eaeSBruce Evans /* Fall through. */ 20465c1a1eaeSBruce Evans case STARTRECAL: 20476182fdbdSPeter Wemm if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2048dc8603e3SJoerg Wunsch /* arrgl */ 2049dc8603e3SJoerg Wunsch fdc->retry = 6; 20506182fdbdSPeter Wemm return (retrier(fdc)); 2051dc8603e3SJoerg Wunsch } 20525b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 20535b81b6b3SRodney W. Grimes return (0); /* will return later */ 20545b81b6b3SRodney W. Grimes case RECALWAIT: 20555b81b6b3SRodney W. Grimes /* allow heads to settle */ 20566182fdbdSPeter Wemm timeout(fd_pseudointr, fdc, hz / 8); 20575b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 20585b81b6b3SRodney W. Grimes return (0); /* will return later */ 20595b81b6b3SRodney W. Grimes case RECALCOMPLETE: 20603a2f7427SDavid Greenman do { 20613a2f7427SDavid Greenman /* 2062dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 20633a2f7427SDavid Greenman */ 2064dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2065dc5df763SJoerg Wunsch return 0; 2066dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 2067dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2068dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 20693a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 20703a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 20715b81b6b3SRodney W. Grimes { 2072dc8603e3SJoerg Wunsch if(fdc->retry > 3) 2073dc8603e3SJoerg Wunsch /* 2074dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 2075dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 2076dc8603e3SJoerg Wunsch * since people used to complain much about 2077dc8603e3SJoerg Wunsch * the failure message, try not logging 2078dc8603e3SJoerg Wunsch * this one if it seems to be the first 2079dc8603e3SJoerg Wunsch * time in a line 2080dc8603e3SJoerg Wunsch */ 2081dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 2082dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 20833a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 20846182fdbdSPeter Wemm return (retrier(fdc)); 20855b81b6b3SRodney W. Grimes } 20865b81b6b3SRodney W. Grimes fd->track = 0; 20875b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 20885b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 20895b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 20905b81b6b3SRodney W. Grimes case MOTORWAIT: 20915b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 20925b81b6b3SRodney W. Grimes { 20935b81b6b3SRodney W. Grimes return (0); /* time's not up yet */ 20945b81b6b3SRodney W. Grimes } 20955c1a1eaeSBruce Evans if (fdc->flags & FDC_NEEDS_RESET) { 20965c1a1eaeSBruce Evans fdc->state = RESETCTLR; 20975c1a1eaeSBruce Evans fdc->flags &= ~FDC_NEEDS_RESET; 20985c1a1eaeSBruce Evans } else { 20995e235068SJordan K. Hubbard /* 21005c1a1eaeSBruce Evans * If all motors were off, then the controller was 21015c1a1eaeSBruce Evans * reset, so it has lost track of the current 21025c1a1eaeSBruce Evans * cylinder. Recalibrate to handle this case. 2103f86e4077SBruce Evans * But first, discard the results of the reset. 21045e235068SJordan K. Hubbard */ 2105f86e4077SBruce Evans fdc->state = RESETCOMPLETE; 21065c1a1eaeSBruce Evans } 21075b81b6b3SRodney W. Grimes return (1); /* will return immediatly */ 21085b81b6b3SRodney W. Grimes default: 2109b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "unexpected FD int->"); 2110dc5df763SJoerg Wunsch if (fd_read_status(fdc, fd->fdsu) == 0) 2111a838d83dSBruce Evans printf("FDC status :%x %x %x %x %x %x %x ", 21125b81b6b3SRodney W. Grimes fdc->status[0], 21135b81b6b3SRodney W. Grimes fdc->status[1], 21145b81b6b3SRodney W. Grimes fdc->status[2], 21155b81b6b3SRodney W. Grimes fdc->status[3], 21165b81b6b3SRodney W. Grimes fdc->status[4], 21175b81b6b3SRodney W. Grimes fdc->status[5], 21185b81b6b3SRodney W. Grimes fdc->status[6] ); 21193a2f7427SDavid Greenman else 2120dac0f2dbSJoerg Wunsch printf("No status available "); 2121dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 2122dac0f2dbSJoerg Wunsch { 2123dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 21245b81b6b3SRodney W. Grimes return (0); 21255b81b6b3SRodney W. Grimes } 2126dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 2127dac0f2dbSJoerg Wunsch return (0); 2128dac0f2dbSJoerg Wunsch } 2129dac0f2dbSJoerg Wunsch /*XXX confusing: some branches return immediately, others end up here*/ 21305b81b6b3SRodney W. Grimes return (1); /* Come back immediatly to new state */ 21315b81b6b3SRodney W. Grimes } 21325b81b6b3SRodney W. Grimes 2133aaf08d94SGarrett Wollman static int 21346182fdbdSPeter Wemm retrier(struct fdc_data *fdc) 21355b81b6b3SRodney W. Grimes { 213617542807SPoul-Henning Kamp register struct buf *bp; 21376182fdbdSPeter Wemm struct fd_data *fd; 21386182fdbdSPeter Wemm int fdu; 21395b81b6b3SRodney W. Grimes 2140e93e63cbSBruce Evans bp = fdc->bp; 21415b81b6b3SRodney W. Grimes 21426182fdbdSPeter Wemm /* XXX shouldn't this be cached somewhere? */ 21436182fdbdSPeter Wemm fdu = FDUNIT(minor(bp->b_dev)); 21446182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 21456182fdbdSPeter Wemm if (fd->options & FDOPT_NORETRY) 21463a2f7427SDavid Greenman goto fail; 21476182fdbdSPeter Wemm 21486182fdbdSPeter Wemm switch (fdc->retry) { 21495b81b6b3SRodney W. Grimes case 0: case 1: case 2: 21505b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 21515b81b6b3SRodney W. Grimes break; 21525b81b6b3SRodney W. Grimes case 3: case 4: case 5: 21535b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 21545b81b6b3SRodney W. Grimes break; 21555b81b6b3SRodney W. Grimes case 6: 21565b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 21575b81b6b3SRodney W. Grimes break; 21585b81b6b3SRodney W. Grimes case 7: 21595b81b6b3SRodney W. Grimes break; 21605b81b6b3SRodney W. Grimes default: 21613a2f7427SDavid Greenman fail: 21625b81b6b3SRodney W. Grimes { 21637ca0641bSAndrey A. Chernov dev_t sav_b_dev = bp->b_dev; 21647ca0641bSAndrey A. Chernov /* Trick diskerr */ 21653a2f7427SDavid Greenman bp->b_dev = makedev(major(bp->b_dev), 21663a2f7427SDavid Greenman (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); 2167887ba12fSBruce Evans diskerr(bp, "hard error", LOG_PRINTF, 21683a2f7427SDavid Greenman fdc->fd->skip / DEV_BSIZE, 21693a2f7427SDavid Greenman (struct disklabel *)NULL); 21707ca0641bSAndrey A. Chernov bp->b_dev = sav_b_dev; 2171dc5df763SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) 2172dc5df763SJoerg Wunsch { 2173dc5df763SJoerg Wunsch printf( 2174a838d83dSBruce Evans " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2175dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 2176dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 2177dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 2178dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 2179dc5df763SJoerg Wunsch fdc->status[5]); 2180dc5df763SJoerg Wunsch } 2181dc5df763SJoerg Wunsch else 2182dc5df763SJoerg Wunsch printf(" (No status)\n"); 21835b81b6b3SRodney W. Grimes } 21845b81b6b3SRodney W. Grimes bp->b_flags |= B_ERROR; 21855b81b6b3SRodney W. Grimes bp->b_error = EIO; 2186bb6382faSJoerg Wunsch bp->b_resid += bp->b_bcount - fdc->fd->skip; 2187e93e63cbSBruce Evans fdc->bp = NULL; 21885b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 21895f830ea2SJoerg Wunsch device_unbusy(fd->dev); 219011a0be87SPoul-Henning Kamp devstat_end_transaction_buf(&fdc->fd->device_stats, bp); 21915b81b6b3SRodney W. Grimes biodone(bp); 219292ed385aSRodney W. Grimes fdc->state = FINDWORK; 21935c1a1eaeSBruce Evans fdc->flags |= FDC_NEEDS_RESET; 21945b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 21955b81b6b3SRodney W. Grimes fdc->fdu = -1; 219692ed385aSRodney W. Grimes return (1); 21975b81b6b3SRodney W. Grimes } 21985b81b6b3SRodney W. Grimes fdc->retry++; 21995b81b6b3SRodney W. Grimes return (1); 22005b81b6b3SRodney W. Grimes } 22015b81b6b3SRodney W. Grimes 2202b39c878eSAndrey A. Chernov static int 2203b39c878eSAndrey A. Chernov fdformat(dev, finfo, p) 2204b39c878eSAndrey A. Chernov dev_t dev; 2205b39c878eSAndrey A. Chernov struct fd_formb *finfo; 2206b39c878eSAndrey A. Chernov struct proc *p; 2207b39c878eSAndrey A. Chernov { 2208b39c878eSAndrey A. Chernov fdu_t fdu; 2209b39c878eSAndrey A. Chernov fd_p fd; 2210b39c878eSAndrey A. Chernov 2211b39c878eSAndrey A. Chernov struct buf *bp; 2212b39c878eSAndrey A. Chernov int rv = 0, s; 22133a2f7427SDavid Greenman size_t fdblk; 2214b39c878eSAndrey A. Chernov 2215b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 22166182fdbdSPeter Wemm fd = devclass_get_softc(fd_devclass, fdu); 22173a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2218b39c878eSAndrey A. Chernov 2219b39c878eSAndrey A. Chernov /* set up a buffer header for fdstrategy() */ 2220b39c878eSAndrey A. Chernov bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 2221b39c878eSAndrey A. Chernov if(bp == 0) 2222b39c878eSAndrey A. Chernov return ENOBUFS; 222382f5379bSJoerg Wunsch /* 222482f5379bSJoerg Wunsch * keep the process from being swapped 222582f5379bSJoerg Wunsch */ 22262ada239cSPeter Wemm PHOLD(p); 2227b39c878eSAndrey A. Chernov bzero((void *)bp, sizeof(struct buf)); 222867812eacSKirk McKusick BUF_LOCKINIT(bp); 222967812eacSKirk McKusick BUF_LOCK(bp, LK_EXCLUSIVE); 223067812eacSKirk McKusick bp->b_flags = B_PHYS | B_FORMAT; 2231b39c878eSAndrey A. Chernov 2232b39c878eSAndrey A. Chernov /* 2233b39c878eSAndrey A. Chernov * calculate a fake blkno, so fdstrategy() would initiate a 2234b39c878eSAndrey A. Chernov * seek to the requested cylinder 2235b39c878eSAndrey A. Chernov */ 2236b39c878eSAndrey A. Chernov bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 22373a2f7427SDavid Greenman + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 2238b39c878eSAndrey A. Chernov 2239b39c878eSAndrey A. Chernov bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 2240ab3f7469SPoul-Henning Kamp bp->b_data = (caddr_t)finfo; 2241b39c878eSAndrey A. Chernov 2242b39c878eSAndrey A. Chernov /* now do the format */ 22433e425b96SJulian Elischer bp->b_dev = dev; 224449ff4debSPoul-Henning Kamp BUF_STRATEGY(bp, 0); 2245b39c878eSAndrey A. Chernov 2246b39c878eSAndrey A. Chernov /* ...and wait for it to complete */ 2247b39c878eSAndrey A. Chernov s = splbio(); 22486182fdbdSPeter Wemm while(!(bp->b_flags & B_DONE)) { 22495e235068SJordan K. Hubbard rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 2250b39c878eSAndrey A. Chernov if (rv == EWOULDBLOCK) 2251b39c878eSAndrey A. Chernov break; 2252b39c878eSAndrey A. Chernov } 2253b39c878eSAndrey A. Chernov splx(s); 2254b39c878eSAndrey A. Chernov 225582f5379bSJoerg Wunsch if (rv == EWOULDBLOCK) { 2256b39c878eSAndrey A. Chernov /* timed out */ 2257b39c878eSAndrey A. Chernov rv = EIO; 22585f830ea2SJoerg Wunsch device_unbusy(fd->dev); 225982f5379bSJoerg Wunsch biodone(bp); 226082f5379bSJoerg Wunsch } 22613a2f7427SDavid Greenman if (bp->b_flags & B_ERROR) 22623a2f7427SDavid Greenman rv = bp->b_error; 226382f5379bSJoerg Wunsch /* 226482f5379bSJoerg Wunsch * allow the process to be swapped 226582f5379bSJoerg Wunsch */ 22662ada239cSPeter Wemm PRELE(p); 226767812eacSKirk McKusick BUF_UNLOCK(bp); 226867812eacSKirk McKusick BUF_LOCKFREE(bp); 2269b39c878eSAndrey A. Chernov free(bp, M_TEMP); 2270b39c878eSAndrey A. Chernov return rv; 2271b39c878eSAndrey A. Chernov } 2272b39c878eSAndrey A. Chernov 2273f5f7ba03SJordan K. Hubbard /* 2274671e2ceeSBruce Evans * TODO: don't allocate buffer on stack. 2275f5f7ba03SJordan K. Hubbard */ 22765b81b6b3SRodney W. Grimes 22773e425b96SJulian Elischer static int 2278b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p) 2279f5f7ba03SJordan K. Hubbard dev_t dev; 2280ecbb00a2SDoug Rabson u_long cmd; 2281f5f7ba03SJordan K. Hubbard caddr_t addr; 2282f5f7ba03SJordan K. Hubbard int flag; 2283b39c878eSAndrey A. Chernov struct proc *p; 2284f5f7ba03SJordan K. Hubbard { 22853a2f7427SDavid Greenman fdu_t fdu = FDUNIT(minor(dev)); 22866182fdbdSPeter Wemm fd_p fd = devclass_get_softc(fd_devclass, fdu); 22873a2f7427SDavid Greenman size_t fdblk; 22883a2f7427SDavid Greenman 2289f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 2290f5f7ba03SJordan K. Hubbard struct disklabel *dl; 2291f5f7ba03SJordan K. Hubbard char buffer[DEV_BSIZE]; 22923a2f7427SDavid Greenman int error = 0; 2293f5f7ba03SJordan K. Hubbard 22943a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 2295f5f7ba03SJordan K. Hubbard 22966182fdbdSPeter Wemm switch (cmd) { 2297f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 2298f5f7ba03SJordan K. Hubbard bzero(buffer, sizeof (buffer)); 2299f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 23003a2f7427SDavid Greenman dl->d_secsize = fdblk; 23016182fdbdSPeter Wemm fdt = fd->ft; 2302f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 2303f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 2304f5f7ba03SJordan K. Hubbard 230549ff4debSPoul-Henning Kamp if (readdisklabel(dkmodpart(dev, RAW_PART), dl) 2306191e1a59SBruce Evans == NULL) 2307f5f7ba03SJordan K. Hubbard error = 0; 2308f5f7ba03SJordan K. Hubbard else 2309f5f7ba03SJordan K. Hubbard error = EINVAL; 2310f5f7ba03SJordan K. Hubbard 2311f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 2312f5f7ba03SJordan K. Hubbard break; 2313f5f7ba03SJordan K. Hubbard 2314f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 2315f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2316f5f7ba03SJordan K. Hubbard error = EBADF; 2317f5f7ba03SJordan K. Hubbard break; 2318f5f7ba03SJordan K. Hubbard 2319f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 2320f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 2321f5f7ba03SJordan K. Hubbard error = EBADF; 2322f5f7ba03SJordan K. Hubbard break; 2323f5f7ba03SJordan K. Hubbard 2324f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 23256182fdbdSPeter Wemm if ((flag & FWRITE) == 0) { 2326f5f7ba03SJordan K. Hubbard error = EBADF; 2327f5f7ba03SJordan K. Hubbard break; 2328f5f7ba03SJordan K. Hubbard } 2329f5f7ba03SJordan K. Hubbard 2330f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 2331f5f7ba03SJordan K. Hubbard 2332191e1a59SBruce Evans if ((error = setdisklabel((struct disklabel *)buffer, dl, 2333191e1a59SBruce Evans (u_long)0)) != 0) 2334f5f7ba03SJordan K. Hubbard break; 2335f5f7ba03SJordan K. Hubbard 233649ff4debSPoul-Henning Kamp error = writedisklabel(dev, (struct disklabel *)buffer); 2337b39c878eSAndrey A. Chernov break; 2338b39c878eSAndrey A. Chernov case FD_FORM: 2339b39c878eSAndrey A. Chernov if ((flag & FWRITE) == 0) 2340b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 2341b39c878eSAndrey A. Chernov else if (((struct fd_formb *)addr)->format_version != 2342b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 2343b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 2344b39c878eSAndrey A. Chernov else 2345b39c878eSAndrey A. Chernov error = fdformat(dev, (struct fd_formb *)addr, p); 2346b39c878eSAndrey A. Chernov break; 2347b39c878eSAndrey A. Chernov 2348b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 23493e425b96SJulian Elischer *(struct fd_type *)addr = *fd->ft; 2350f5f7ba03SJordan K. Hubbard break; 2351f5f7ba03SJordan K. Hubbard 23523a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 23533a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 2354f711d546SPoul-Henning Kamp if (suser(p) != 0) 23553a2f7427SDavid Greenman return EPERM; 23563e425b96SJulian Elischer *fd->ft = *(struct fd_type *)addr; 23573a2f7427SDavid Greenman break; 23583a2f7427SDavid Greenman 23593a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 23603e425b96SJulian Elischer *(int *)addr = fd->options; 23613a2f7427SDavid Greenman break; 23623a2f7427SDavid Greenman 23633a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 23643e425b96SJulian Elischer fd->options = *(int *)addr; 23653a2f7427SDavid Greenman break; 23663a2f7427SDavid Greenman 2367f5f7ba03SJordan K. Hubbard default: 23683a2f7427SDavid Greenman error = ENOTTY; 2369f5f7ba03SJordan K. Hubbard break; 2370f5f7ba03SJordan K. Hubbard } 2371f5f7ba03SJordan K. Hubbard return (error); 2372f5f7ba03SJordan K. Hubbard } 2373f5f7ba03SJordan K. Hubbard 23743a2f7427SDavid Greenman /* 23753a2f7427SDavid Greenman * Hello emacs, these are the 23763a2f7427SDavid Greenman * Local Variables: 23773a2f7427SDavid Greenman * c-indent-level: 8 23783a2f7427SDavid Greenman * c-continued-statement-offset: 8 23793a2f7427SDavid Greenman * c-continued-brace-offset: 0 23803a2f7427SDavid Greenman * c-brace-offset: -8 23813a2f7427SDavid Greenman * c-brace-imaginary-offset: 0 23823a2f7427SDavid Greenman * c-argdecl-indent: 8 23833a2f7427SDavid Greenman * c-label-offset: -8 23843a2f7427SDavid Greenman * c++-hanging-braces: 1 23853a2f7427SDavid Greenman * c++-access-specifier-offset: -8 23863a2f7427SDavid Greenman * c++-empty-arglist-indent: 8 23873a2f7427SDavid Greenman * c++-friend-offset: 0 23883a2f7427SDavid Greenman * End: 23893a2f7427SDavid Greenman */ 2390