15b81b6b3SRodney W. Grimes /*- 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 * 83a2f7427SDavid Greenman * Portions Copyright (c) 1993, 1994 by 93a2f7427SDavid Greenman * jc@irbs.UUCP (John Capo) 103a2f7427SDavid Greenman * vak@zebub.msk.su (Serge Vakulenko) 113a2f7427SDavid Greenman * ache@astral.msk.su (Andrew A. Chernov) 123a2f7427SDavid Greenman * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 133a2f7427SDavid Greenman * 145b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 155b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 165b81b6b3SRodney W. Grimes * are met: 175b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 185b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 195b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 205b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 215b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 225b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 235b81b6b3SRodney W. Grimes * must display the following acknowledgement: 245b81b6b3SRodney W. Grimes * This product includes software developed by the University of 255b81b6b3SRodney W. Grimes * California, Berkeley and its contributors. 265b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 275b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 285b81b6b3SRodney W. Grimes * without specific prior written permission. 295b81b6b3SRodney W. Grimes * 305b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 315b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 325b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 335b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 345b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 355b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 365b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 375b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 385b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 395b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 405b81b6b3SRodney W. Grimes * SUCH DAMAGE. 415b81b6b3SRodney W. Grimes * 42dc4ff321SRodney W. Grimes * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 4385803790SJordan K. Hubbard * $Id: fd.c,v 1.43 1994/11/18 10:18:36 phk Exp $ 445b81b6b3SRodney W. Grimes * 455b81b6b3SRodney W. Grimes */ 465b81b6b3SRodney W. Grimes 47b99f0a4aSAndrew Moore #include "ft.h" 48b99f0a4aSAndrew Moore #if NFT < 1 49b99f0a4aSAndrew Moore #undef NFDC 50b99f0a4aSAndrew Moore #endif 515b81b6b3SRodney W. Grimes #include "fd.h" 525b81b6b3SRodney W. Grimes 5385803790SJordan K. Hubbard /* Flags */ 5485803790SJordan K. Hubbard #define FT_DONT_PROBE 0x1 5585803790SJordan K. Hubbard 56b99f0a4aSAndrew Moore #if NFDC > 0 57b99f0a4aSAndrew Moore 58b99f0a4aSAndrew Moore #include <sys/param.h> 59b99f0a4aSAndrew Moore #include <sys/dkbad.h> 60b99f0a4aSAndrew Moore #include <sys/systm.h> 61b99f0a4aSAndrew Moore #include <sys/kernel.h> 62b99f0a4aSAndrew Moore #include <sys/conf.h> 63b99f0a4aSAndrew Moore #include <sys/file.h> 64b99f0a4aSAndrew Moore #include <sys/ioctl.h> 65b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h> 66b99f0a4aSAndrew Moore #include <sys/disklabel.h> 67b99f0a4aSAndrew Moore #include <sys/buf.h> 68b99f0a4aSAndrew Moore #include <sys/uio.h> 69b99f0a4aSAndrew Moore #include <sys/malloc.h> 703a2f7427SDavid Greenman #include <sys/proc.h> 71b99f0a4aSAndrew Moore #include <sys/syslog.h> 7292200632SGarrett Wollman #include <sys/devconf.h> 7392200632SGarrett Wollman #include <sys/dkstat.h> 74f540b106SGarrett Wollman #include <i386/isa/isa.h> 75f540b106SGarrett Wollman #include <i386/isa/isa_device.h> 76f540b106SGarrett Wollman #include <i386/isa/fdreg.h> 77f540b106SGarrett Wollman #include <i386/isa/fdc.h> 78f540b106SGarrett Wollman #include <i386/isa/rtc.h> 7987eafbcaSPoul-Henning Kamp #if NFT > 0 8087eafbcaSPoul-Henning Kamp #include <sys/ftape.h> 8187eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h> 8287eafbcaSPoul-Henning Kamp #endif 835b81b6b3SRodney W. Grimes 8492200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int); 8592200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int); 865e235068SJordan K. Hubbard static int fd_externalize(struct proc *, struct kern_devconf *, void *, size_t); 875e235068SJordan K. Hubbard 885e235068SJordan K. Hubbard /* 895e235068SJordan K. Hubbard * Templates for the kern_devconf structures used when we attach. 905e235068SJordan K. Hubbard */ 915e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { { 925e235068SJordan K. Hubbard 0, 0, 0, /* filled in by kern_devconf.c */ 935e235068SJordan K. Hubbard "fd", 0, { MDDT_DISK, 0 }, 945e235068SJordan K. Hubbard fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN, 955e235068SJordan K. Hubbard 0, /* parent */ 965e235068SJordan K. Hubbard 0, /* parentdata */ 975e235068SJordan K. Hubbard DC_UNKNOWN, /* state */ 985e235068SJordan K. Hubbard "floppy disk" 995e235068SJordan K. Hubbard } }; 1005e235068SJordan K. Hubbard 1015e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { { 1025e235068SJordan K. Hubbard 0, 0, 0, /* filled in by kern_devconf.c */ 1035e235068SJordan K. Hubbard "fdc", 0, { MDDT_ISA, 0, "bio" }, 1045e235068SJordan K. Hubbard isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN, 1055e235068SJordan K. Hubbard 0, /* parent */ 1065e235068SJordan K. Hubbard 0, /* parentdata */ 1075e235068SJordan K. Hubbard DC_UNKNOWN, /* state */ 1085e235068SJordan K. Hubbard "floppy disk/tape controller" 1095e235068SJordan K. Hubbard } }; 1105e235068SJordan K. Hubbard 1115e235068SJordan K. Hubbard static inline void 1125e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit) 1135e235068SJordan K. Hubbard { 1145e235068SJordan K. Hubbard if(unit != 0) 1155e235068SJordan K. Hubbard kdc_fd[unit] = kdc_fd[0]; 1165e235068SJordan K. Hubbard 1175e235068SJordan K. Hubbard kdc_fd[unit].kdc_unit = unit; 1185e235068SJordan K. Hubbard kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr]; 1195e235068SJordan K. Hubbard kdc_fd[unit].kdc_parentdata = 0; 1205e235068SJordan K. Hubbard dev_attach(&kdc_fd[unit]); 1215e235068SJordan K. Hubbard } 1225e235068SJordan K. Hubbard 1235e235068SJordan K. Hubbard static inline void 1245e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp) 1255e235068SJordan K. Hubbard { 1265e235068SJordan K. Hubbard int unit = dvp->id_unit; 1275e235068SJordan K. Hubbard 1285e235068SJordan K. Hubbard if(unit != 0) 1295e235068SJordan K. Hubbard kdc_fdc[unit] = kdc_fdc[0]; 1305e235068SJordan K. Hubbard 1315e235068SJordan K. Hubbard kdc_fdc[unit].kdc_unit = unit; 1325e235068SJordan K. Hubbard kdc_fdc[unit].kdc_parent = &kdc_isa0; 1335e235068SJordan K. Hubbard kdc_fdc[unit].kdc_parentdata = dvp; 1345e235068SJordan K. Hubbard dev_attach(&kdc_fdc[unit]); 1355e235068SJordan K. Hubbard } 1365e235068SJordan K. Hubbard 1375e235068SJordan K. Hubbard static int 1385e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force) 1395e235068SJordan K. Hubbard { 1405e235068SJordan K. Hubbard if(force) { 1415e235068SJordan K. Hubbard dev_detach(kdc); 1425e235068SJordan K. Hubbard return 0; 1435e235068SJordan K. Hubbard } else { 1445e235068SJordan K. Hubbard return EBUSY; /* XXX fix */ 1455e235068SJordan K. Hubbard } 1465e235068SJordan K. Hubbard } 1475e235068SJordan K. Hubbard 1485e235068SJordan K. Hubbard static int 1495e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force) 1505e235068SJordan K. Hubbard { 1515e235068SJordan K. Hubbard dev_detach(kdc); 1525e235068SJordan K. Hubbard return 0; 1535e235068SJordan K. Hubbard } 15492200632SGarrett Wollman 155b2be795bSAndrey A. Chernov #define RAW_PART 2 1565b81b6b3SRodney W. Grimes #define b_cylin b_resid 1575b81b6b3SRodney W. Grimes 158b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */ 159b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX 1605b81b6b3SRodney W. Grimes 1613a2f7427SDavid Greenman /* 1623a2f7427SDavid Greenman * this biotab field doubles as a field for the physical unit number 1633a2f7427SDavid Greenman * on the controller 1643a2f7427SDavid Greenman */ 1653a2f7427SDavid Greenman #define id_physid id_scsiid 1663a2f7427SDavid Greenman 167b39c878eSAndrey A. Chernov #define NUMTYPES 14 168b39c878eSAndrey A. Chernov #define NUMDENS (NUMTYPES - 6) 1697ca0641bSAndrey A. Chernov 1703a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */ 171b99f0a4aSAndrew Moore #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 172b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 173b99f0a4aSAndrew Moore #define FD_1720 1 174b99f0a4aSAndrew Moore #define FD_1480 2 175b99f0a4aSAndrew Moore #define FD_1440 3 176b99f0a4aSAndrew Moore #define FD_1200 4 177b99f0a4aSAndrew Moore #define FD_820 5 178b99f0a4aSAndrew Moore #define FD_800 6 179b99f0a4aSAndrew Moore #define FD_720 7 180b99f0a4aSAndrew Moore #define FD_360 8 181ed2fa05eSAndrey A. Chernov 182b99f0a4aSAndrew Moore #define FD_1480in5_25 9 183b99f0a4aSAndrew Moore #define FD_1440in5_25 10 184b99f0a4aSAndrew Moore #define FD_820in5_25 11 185b99f0a4aSAndrew Moore #define FD_800in5_25 12 186b99f0a4aSAndrew Moore #define FD_720in5_25 13 187b99f0a4aSAndrew Moore #define FD_360in5_25 14 188b99f0a4aSAndrew Moore 1897ca0641bSAndrey A. Chernov 1905b81b6b3SRodney W. Grimes struct fd_type fd_types[NUMTYPES] = 1915b81b6b3SRodney W. Grimes { 192126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 193126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 194126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 195126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 196126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 197126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 198126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 199b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 200ed2fa05eSAndrey A. Chernov 201126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 202126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 203126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 204126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 205126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 206126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 2075b81b6b3SRodney W. Grimes }; 2085b81b6b3SRodney W. Grimes 209b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 2105b81b6b3SRodney W. Grimes /***********************************************************************\ 2115b81b6b3SRodney W. Grimes * Per controller structure. * 2125b81b6b3SRodney W. Grimes \***********************************************************************/ 213b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC]; 2145b81b6b3SRodney W. Grimes 2155b81b6b3SRodney W. Grimes /***********************************************************************\ 2165b81b6b3SRodney W. Grimes * Per drive structure. * 217b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 2185b81b6b3SRodney W. Grimes \***********************************************************************/ 2195b81b6b3SRodney W. Grimes struct fd_data { 220b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 2215b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 2223a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 2235b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 2245b81b6b3SRodney W. Grimes int flags; 2255b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 2265b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 2275b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 2285b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 2295b81b6b3SRodney W. Grimes int skip; 2305b81b6b3SRodney W. Grimes int hddrv; 2315b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 2323a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 23392200632SGarrett Wollman int dkunit; /* disk stats unit number */ 2345b81b6b3SRodney W. Grimes } fd_data[NFD]; 2355b81b6b3SRodney W. Grimes 2365b81b6b3SRodney W. Grimes /***********************************************************************\ 2375b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 2385b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 2395b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 2405b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 2415b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 2425b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 2435b81b6b3SRodney W. Grimes \***********************************************************************/ 244b99f0a4aSAndrew Moore 2453a2f7427SDavid Greenman #if NFT > 0 2463a2f7427SDavid Greenman int ftopen(dev_t, int); 2473a2f7427SDavid Greenman int ftintr(ftu_t ftu); 2483a2f7427SDavid Greenman int ftclose(dev_t, int); 2493a2f7427SDavid Greenman void ftstrategy(struct buf *); 2503a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *); 2513a2f7427SDavid Greenman int ftdump(dev_t); 2523a2f7427SDavid Greenman int ftsize(dev_t); 2533a2f7427SDavid Greenman int ftattach(struct isa_device *, struct isa_device *); 2543a2f7427SDavid Greenman #endif 2555b81b6b3SRodney W. Grimes 2563a2f7427SDavid Greenman /* autoconfig functions */ 2573a2f7427SDavid Greenman static int fdprobe(struct isa_device *); 2583a2f7427SDavid Greenman static int fdattach(struct isa_device *); 2593a2f7427SDavid Greenman 2603a2f7427SDavid Greenman /* exported functions */ 2613a2f7427SDavid Greenman int fdsize (dev_t); 2623a2f7427SDavid Greenman void fdintr(fdcu_t); 2633a2f7427SDavid Greenman int Fdopen(dev_t, int); 2643a2f7427SDavid Greenman int fdclose(dev_t, int); 2653a2f7427SDavid Greenman void fdstrategy(struct buf *); 2663a2f7427SDavid Greenman int fdioctl(dev_t, int, caddr_t, int, struct proc *); 2673a2f7427SDavid Greenman 2683a2f7427SDavid Greenman /* needed for ft driver, thus exported */ 2693a2f7427SDavid Greenman int in_fdc(fdcu_t); 2703a2f7427SDavid Greenman int out_fdc(fdcu_t, int); 2713a2f7427SDavid Greenman 2723a2f7427SDavid Greenman /* internal functions */ 2733a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int); 2743a2f7427SDavid Greenman # define TURNON 1 2753a2f7427SDavid Greenman # define TURNOFF 0 2763a2f7427SDavid Greenman static timeout_t fd_turnoff; 2773a2f7427SDavid Greenman static timeout_t fd_motor_on; 2783a2f7427SDavid Greenman static void fd_turnon(fdu_t); 2793a2f7427SDavid Greenman static void fdc_reset(fdc_p); 2803a2f7427SDavid Greenman static void fdstart(fdcu_t); 2813a2f7427SDavid Greenman static timeout_t fd_timeout; 2823a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2833a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p); 284aaf08d94SGarrett Wollman static int retrier(fdcu_t); 2853a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *); 2863a2f7427SDavid Greenman 287aaf08d94SGarrett Wollman 2885b81b6b3SRodney W. Grimes #define DEVIDLE 0 2895b81b6b3SRodney W. Grimes #define FINDWORK 1 2905b81b6b3SRodney W. Grimes #define DOSEEK 2 2915b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 3 2925b81b6b3SRodney W. Grimes #define IOCOMPLETE 4 2935b81b6b3SRodney W. Grimes #define RECALCOMPLETE 5 2945b81b6b3SRodney W. Grimes #define STARTRECAL 6 2955b81b6b3SRodney W. Grimes #define RESETCTLR 7 2965b81b6b3SRodney W. Grimes #define SEEKWAIT 8 2975b81b6b3SRodney W. Grimes #define RECALWAIT 9 2985b81b6b3SRodney W. Grimes #define MOTORWAIT 10 2995b81b6b3SRodney W. Grimes #define IOTIMEDOUT 11 3005b81b6b3SRodney W. Grimes 3015b81b6b3SRodney W. Grimes #ifdef DEBUG 3025b81b6b3SRodney W. Grimes char *fdstates[] = 3035b81b6b3SRodney W. Grimes { 3045b81b6b3SRodney W. Grimes "DEVIDLE", 3055b81b6b3SRodney W. Grimes "FINDWORK", 3065b81b6b3SRodney W. Grimes "DOSEEK", 3075b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 3085b81b6b3SRodney W. Grimes "IOCOMPLETE", 3095b81b6b3SRodney W. Grimes "RECALCOMPLETE", 3105b81b6b3SRodney W. Grimes "STARTRECAL", 3115b81b6b3SRodney W. Grimes "RESETCTLR", 3125b81b6b3SRodney W. Grimes "SEEKWAIT", 3135b81b6b3SRodney W. Grimes "RECALWAIT", 3145b81b6b3SRodney W. Grimes "MOTORWAIT", 3155b81b6b3SRodney W. Grimes "IOTIMEDOUT" 3165b81b6b3SRodney W. Grimes }; 3175b81b6b3SRodney W. Grimes 3183a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 3193a2f7427SDavid Greenman int fd_debug = 0; 3205b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 3215b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 322381fe1aaSGarrett Wollman #else /* DEBUG */ 3235b81b6b3SRodney W. Grimes #define TRACE0(arg) 3245b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 325381fe1aaSGarrett Wollman #endif /* DEBUG */ 3265b81b6b3SRodney W. Grimes 32792200632SGarrett Wollman struct isa_device *fdcdevs[NFDC]; 32892200632SGarrett Wollman 32992200632SGarrett Wollman /* 33092200632SGarrett Wollman * Provide hw.devconf information. 33192200632SGarrett Wollman */ 33292200632SGarrett Wollman static int 3335e235068SJordan K. Hubbard fd_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len) 33492200632SGarrett Wollman { 33592200632SGarrett Wollman return disk_externalize(fd_data[kdc->kdc_unit].fdsu, userp, &len); 33692200632SGarrett Wollman } 33792200632SGarrett Wollman 33892200632SGarrett Wollman static int 3395e235068SJordan K. Hubbard fdc_externalize(struct proc *p, struct kern_devconf *kdc, void *userp, size_t len) 34092200632SGarrett Wollman { 34192200632SGarrett Wollman return isa_externalize(fdcdevs[kdc->kdc_unit], userp, &len); 34292200632SGarrett Wollman } 34392200632SGarrett Wollman 3445b81b6b3SRodney W. Grimes /****************************************************************************/ 3455b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 3465b81b6b3SRodney W. Grimes /****************************************************************************/ 347b99f0a4aSAndrew Moore struct isa_driver fdcdriver = { 348b99f0a4aSAndrew Moore fdprobe, fdattach, "fdc", 3495b81b6b3SRodney W. Grimes }; 3505b81b6b3SRodney W. Grimes 3515b81b6b3SRodney W. Grimes /* 3525b81b6b3SRodney W. Grimes * probe for existance of controller 3535b81b6b3SRodney W. Grimes */ 3543a2f7427SDavid Greenman static int 3555b81b6b3SRodney W. Grimes fdprobe(dev) 3565b81b6b3SRodney W. Grimes struct isa_device *dev; 3575b81b6b3SRodney W. Grimes { 3585b81b6b3SRodney W. Grimes fdcu_t fdcu = dev->id_unit; 3595b81b6b3SRodney W. Grimes if(fdc_data[fdcu].flags & FDC_ATTACHED) 3605b81b6b3SRodney W. Grimes { 3615b81b6b3SRodney W. Grimes printf("fdc: same unit (%d) used multiple times\n", fdcu); 3625b81b6b3SRodney W. Grimes return 0; 3635b81b6b3SRodney W. Grimes } 3645b81b6b3SRodney W. Grimes 36592200632SGarrett Wollman fdcdevs[fdcu] = dev; 3665b81b6b3SRodney W. Grimes fdc_data[fdcu].baseport = dev->id_iobase; 3675b81b6b3SRodney W. Grimes 36816111cedSAndrew Moore /* First - lets reset the floppy controller */ 3693a2f7427SDavid Greenman outb(dev->id_iobase+FDOUT, 0); 37016111cedSAndrew Moore DELAY(100); 3713a2f7427SDavid Greenman outb(dev->id_iobase+FDOUT, FDO_FRST); 37216111cedSAndrew Moore 3735b81b6b3SRodney W. Grimes /* see if it can handle a command */ 3745b81b6b3SRodney W. Grimes if (out_fdc(fdcu, NE7CMD_SPECIFY) < 0) 3755b81b6b3SRodney W. Grimes { 3765b81b6b3SRodney W. Grimes return(0); 3775b81b6b3SRodney W. Grimes } 3783a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_1(3, 240)); 3793a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_2(2, 0)); 3805b81b6b3SRodney W. Grimes return (IO_FDCSIZE); 3815b81b6b3SRodney W. Grimes } 3825b81b6b3SRodney W. Grimes 3835b81b6b3SRodney W. Grimes /* 3845b81b6b3SRodney W. Grimes * wire controller into system, look for floppy units 3855b81b6b3SRodney W. Grimes */ 3863a2f7427SDavid Greenman static int 3875b81b6b3SRodney W. Grimes fdattach(dev) 3885b81b6b3SRodney W. Grimes struct isa_device *dev; 3895b81b6b3SRodney W. Grimes { 3903a2f7427SDavid Greenman unsigned fdt; 3915b81b6b3SRodney W. Grimes fdu_t fdu; 3925b81b6b3SRodney W. Grimes fdcu_t fdcu = dev->id_unit; 3935b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 3945b81b6b3SRodney W. Grimes fd_p fd; 3956b7bd95bSJoerg Wunsch int fdsu, st0, i; 396b99f0a4aSAndrew Moore struct isa_device *fdup; 39792200632SGarrett Wollman 3982f86936aSGarrett Wollman fdc_registerdev(dev); 3995b81b6b3SRodney W. Grimes 4005b81b6b3SRodney W. Grimes fdc->fdcu = fdcu; 4015b81b6b3SRodney W. Grimes fdc->flags |= FDC_ATTACHED; 4025b81b6b3SRodney W. Grimes fdc->dmachan = dev->id_drq; 4035b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 4043a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 4053a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 406b99f0a4aSAndrew Moore printf("fdc%d:", fdcu); 4075b81b6b3SRodney W. Grimes 4085b81b6b3SRodney W. Grimes /* check for each floppy drive */ 409b99f0a4aSAndrew Moore for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { 410b99f0a4aSAndrew Moore if (fdup->id_iobase != dev->id_iobase) 411b99f0a4aSAndrew Moore continue; 412b99f0a4aSAndrew Moore fdu = fdup->id_unit; 413b99f0a4aSAndrew Moore fd = &fd_data[fdu]; 414b99f0a4aSAndrew Moore if (fdu >= (NFD+NFT)) 415b99f0a4aSAndrew Moore continue; 416b99f0a4aSAndrew Moore fdsu = fdup->id_physid; 417b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 418b99f0a4aSAndrew Moore switch (fdu) { 419b99f0a4aSAndrew Moore case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 420b99f0a4aSAndrew Moore break; 421b99f0a4aSAndrew Moore case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 422b99f0a4aSAndrew Moore break; 423b99f0a4aSAndrew Moore default: fdt = RTCFDT_NONE; 424b99f0a4aSAndrew Moore break; 425b99f0a4aSAndrew Moore } 4265b81b6b3SRodney W. Grimes /* is there a unit? */ 427b99f0a4aSAndrew Moore if ((fdt == RTCFDT_NONE) 428b99f0a4aSAndrew Moore #if NFT > 0 429b99f0a4aSAndrew Moore || (fdsu >= DRVS_PER_CTLR)) { 430b99f0a4aSAndrew Moore #else 431b99f0a4aSAndrew Moore ) { 43256ef0285SAndrew Moore fd->type = NO_TYPE; 433b99f0a4aSAndrew Moore #endif 434b99f0a4aSAndrew Moore #if NFT > 0 435b99f0a4aSAndrew Moore /* If BIOS says no floppy, or > 2nd device */ 436b99f0a4aSAndrew Moore /* Probe for and attach a floppy tape. */ 43785803790SJordan K. Hubbard if (dev->id_flags & FT_DONT_PROBE) 43885803790SJordan K. Hubbard printf(" [fd%d: floppy tape not probed]", fdu); 43985803790SJordan K. Hubbard else if (ftattach(dev, fdup)) 440b99f0a4aSAndrew Moore continue; 44156ef0285SAndrew Moore if (fdsu < DRVS_PER_CTLR) 442b99f0a4aSAndrew Moore fd->type = NO_TYPE; 44356ef0285SAndrew Moore #endif 4445b81b6b3SRodney W. Grimes continue; 445f5f7ba03SJordan K. Hubbard } 4465b81b6b3SRodney W. Grimes 4475b81b6b3SRodney W. Grimes /* select it */ 4483a2f7427SDavid Greenman set_motor(fdcu, fdsu, TURNON); 4496b7bd95bSJoerg Wunsch DELAY(1000000); /* 1 sec */ 4506b7bd95bSJoerg Wunsch out_fdc(fdcu, NE7CMD_SENSED); 4516b7bd95bSJoerg Wunsch out_fdc(fdcu, fdsu); 4526b7bd95bSJoerg Wunsch if(in_fdc(fdcu) & NE7_ST3_T0) { 4536b7bd95bSJoerg Wunsch /* if at track 0, first seek inwards */ 4543a2f7427SDavid Greenman out_fdc(fdcu, NE7CMD_SEEK); /* seek some steps... */ 4555b81b6b3SRodney W. Grimes out_fdc(fdcu, fdsu); 4563a2f7427SDavid Greenman out_fdc(fdcu, 10); 4576b7bd95bSJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 4586b7bd95bSJoerg Wunsch out_fdc(fdcu, NE7CMD_SENSEI); /* make ctrlr happy */ 4593a2f7427SDavid Greenman (void)in_fdc(fdcu); 4603a2f7427SDavid Greenman (void)in_fdc(fdcu); 4616b7bd95bSJoerg Wunsch } 4626b7bd95bSJoerg Wunsch for(i = 0; i < 2; i++) { 4636b7bd95bSJoerg Wunsch /* 4646b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 4656b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 4666b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 4676b7bd95bSJoerg Wunsch * more than 77 steps 4686b7bd95bSJoerg Wunsch */ 4696b7bd95bSJoerg Wunsch out_fdc(fdcu, NE7CMD_RECAL); /* go back to 0 */ 4703a2f7427SDavid Greenman out_fdc(fdcu, fdsu); 4716b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek */ 4726b7bd95bSJoerg Wunsch DELAY(i == 0? 1000000: 300000); 4735b81b6b3SRodney W. Grimes 4746b7bd95bSJoerg Wunsch /* anything responding? */ 4755b81b6b3SRodney W. Grimes out_fdc(fdcu, NE7CMD_SENSEI); 4765b81b6b3SRodney W. Grimes st0 = in_fdc(fdcu); 4773a2f7427SDavid Greenman (void)in_fdc(fdcu); 4786b7bd95bSJoerg Wunsch 4796b7bd95bSJoerg Wunsch if ((st0 & NE7_ST0_EC) == 0) 4806b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 4816b7bd95bSJoerg Wunsch } 4826b7bd95bSJoerg Wunsch 4833a2f7427SDavid Greenman set_motor(fdcu, fdsu, TURNOFF); 4843a2f7427SDavid Greenman 4853a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 4865b81b6b3SRodney W. Grimes continue; 4875b81b6b3SRodney W. Grimes 488b99f0a4aSAndrew Moore fd->track = -2; 489b99f0a4aSAndrew Moore fd->fdc = fdc; 490b99f0a4aSAndrew Moore fd->fdsu = fdsu; 4913a2f7427SDavid Greenman fd->options = 0; 492b99f0a4aSAndrew Moore printf(" [%d: fd%d: ", fdsu, fdu); 4935b81b6b3SRodney W. Grimes 494b99f0a4aSAndrew Moore switch (fdt) { 4957ca0641bSAndrey A. Chernov case RTCFDT_12M: 496b99f0a4aSAndrew Moore printf("1.2MB 5.25in]"); 497b99f0a4aSAndrew Moore fd->type = FD_1200; 4987ca0641bSAndrey A. Chernov break; 4997ca0641bSAndrey A. Chernov case RTCFDT_144M: 500b99f0a4aSAndrew Moore printf("1.44MB 3.5in]"); 501b99f0a4aSAndrew Moore fd->type = FD_1440; 5027ca0641bSAndrey A. Chernov break; 5037ca0641bSAndrey A. Chernov case RTCFDT_360K: 504b99f0a4aSAndrew Moore printf("360KB 5.25in]"); 505b99f0a4aSAndrew Moore fd->type = FD_360; 5067ca0641bSAndrey A. Chernov break; 507ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 508b99f0a4aSAndrew Moore printf("720KB 3.5in]"); 509b99f0a4aSAndrew Moore fd->type = FD_720; 510ed2fa05eSAndrey A. Chernov break; 5117ca0641bSAndrey A. Chernov default: 512b99f0a4aSAndrew Moore printf("unknown]"); 513b99f0a4aSAndrew Moore fd->type = NO_TYPE; 5147ca0641bSAndrey A. Chernov break; 5155b81b6b3SRodney W. Grimes } 51692200632SGarrett Wollman fd_registerdev(fdcu, fdu); 51792200632SGarrett Wollman if(dk_ndrive < DK_NDRIVE) { 51892200632SGarrett Wollman sprintf(dk_names[dk_ndrive], "fd%d", fdu); 51992200632SGarrett Wollman dk_wpms[dk_ndrive] = (500 * 1024 / 2) / 1000; 52092200632SGarrett Wollman fd->dkunit = dk_ndrive++; 52192200632SGarrett Wollman } else { 52292200632SGarrett Wollman fd->dkunit = -1; 52392200632SGarrett Wollman } 5245b81b6b3SRodney W. Grimes } 525b99f0a4aSAndrew Moore printf("\n"); 5265b81b6b3SRodney W. Grimes 5273a2f7427SDavid Greenman return (1); 5285b81b6b3SRodney W. Grimes } 5295b81b6b3SRodney W. Grimes 5305b81b6b3SRodney W. Grimes int 5315b81b6b3SRodney W. Grimes fdsize(dev) 5325b81b6b3SRodney W. Grimes dev_t dev; 5335b81b6b3SRodney W. Grimes { 5345b81b6b3SRodney W. Grimes return(0); 5355b81b6b3SRodney W. Grimes } 5365b81b6b3SRodney W. Grimes 5375b81b6b3SRodney W. Grimes /****************************************************************************/ 5385b81b6b3SRodney W. Grimes /* motor control stuff */ 5395e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 5405b81b6b3SRodney W. Grimes /****************************************************************************/ 5413a2f7427SDavid Greenman static void 5423a2f7427SDavid Greenman set_motor(fdcu, fdsu, turnon) 543f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 5443a2f7427SDavid Greenman int fdsu; 5453a2f7427SDavid Greenman int turnon; 5465b81b6b3SRodney W. Grimes { 5473a2f7427SDavid Greenman int fdout = fdc_data[fdcu].fdout; 5483a2f7427SDavid Greenman int needspecify = 0; 5493a2f7427SDavid Greenman 5503a2f7427SDavid Greenman if(turnon) { 5513a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 5523a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 5533a2f7427SDavid Greenman } else 5543a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 5553a2f7427SDavid Greenman 5565e235068SJordan K. Hubbard if(!turnon 5575e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 5585e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 5595e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 5605e235068SJordan K. Hubbard else { 5613a2f7427SDavid Greenman /* make sure controller is selected and specified */ 5623a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 5633a2f7427SDavid Greenman needspecify = 1; 5643a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 5655b81b6b3SRodney W. Grimes } 5665b81b6b3SRodney W. Grimes 5673a2f7427SDavid Greenman outb(fdc_data[fdcu].baseport+FDOUT, fdout); 5683a2f7427SDavid Greenman fdc_data[fdcu].fdout = fdout; 5693a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 5703a2f7427SDavid Greenman 5713a2f7427SDavid Greenman if(needspecify) { 572dc8603e3SJoerg Wunsch /* 573dc8603e3SJoerg Wunsch * special case: since we have just woken up the FDC 574dc8603e3SJoerg Wunsch * from its sleep, we silently assume the command will 575dc8603e3SJoerg Wunsch * be accepted, and do not test for a timeout 576dc8603e3SJoerg Wunsch */ 5773a2f7427SDavid Greenman out_fdc(fdcu, NE7CMD_SPECIFY); 5783a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_1(3, 240)); 5793a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_2(2, 0)); 5803a2f7427SDavid Greenman } 5813a2f7427SDavid Greenman } 5823a2f7427SDavid Greenman 5835e235068SJordan K. Hubbard /* ARGSUSED */ 584381fe1aaSGarrett Wollman static void 585d0917939SPaul Richards fd_turnoff(void *arg1) 5865b81b6b3SRodney W. Grimes { 587381fe1aaSGarrett Wollman fdu_t fdu = (fdu_t)arg1; 588f5f7ba03SJordan K. Hubbard int s; 589f5f7ba03SJordan K. Hubbard 5905b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 591f5f7ba03SJordan K. Hubbard s = splbio(); 5925b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 5933a2f7427SDavid Greenman set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); 594f5f7ba03SJordan K. Hubbard splx(s); 5955b81b6b3SRodney W. Grimes } 5965b81b6b3SRodney W. Grimes 5975e235068SJordan K. Hubbard /* ARGSUSED */ 5983a2f7427SDavid Greenman static void 599d0917939SPaul Richards fd_motor_on(void *arg1) 6005b81b6b3SRodney W. Grimes { 601381fe1aaSGarrett Wollman fdu_t fdu = (fdu_t)arg1; 602f5f7ba03SJordan K. Hubbard int s; 603f5f7ba03SJordan K. Hubbard 6045b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 605f5f7ba03SJordan K. Hubbard s = splbio(); 6065b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 6075b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 6085b81b6b3SRodney W. Grimes { 609f5f7ba03SJordan K. Hubbard fdintr(fd->fdc->fdcu); 6105b81b6b3SRodney W. Grimes } 611f5f7ba03SJordan K. Hubbard splx(s); 6125b81b6b3SRodney W. Grimes } 6135b81b6b3SRodney W. Grimes 6143a2f7427SDavid Greenman static void 615f5f7ba03SJordan K. Hubbard fd_turnon(fdu) 616f5f7ba03SJordan K. Hubbard fdu_t fdu; 6175b81b6b3SRodney W. Grimes { 6185b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 6195b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 6205b81b6b3SRodney W. Grimes { 6213a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 6223a2f7427SDavid Greenman set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); 6235e235068SJordan K. Hubbard timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ 6245b81b6b3SRodney W. Grimes } 6255b81b6b3SRodney W. Grimes } 6265b81b6b3SRodney W. Grimes 627381fe1aaSGarrett Wollman static void 6283a2f7427SDavid Greenman fdc_reset(fdc) 6293a2f7427SDavid Greenman fdc_p fdc; 6305b81b6b3SRodney W. Grimes { 6313a2f7427SDavid Greenman fdcu_t fdcu = fdc->fdcu; 6323a2f7427SDavid Greenman 6333a2f7427SDavid Greenman /* Try a reset, keep motor on */ 6343a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 6353a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 6363a2f7427SDavid Greenman DELAY(100); 6373a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 6383a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); 6393a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 6403a2f7427SDavid Greenman DELAY(100); 6413a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout); 6423a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 6433a2f7427SDavid Greenman 644dc8603e3SJoerg Wunsch /* after a reset, silently believe the FDC will accept commands */ 6453a2f7427SDavid Greenman out_fdc(fdcu, NE7CMD_SPECIFY); 6463a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_1(3, 240)); 6473a2f7427SDavid Greenman out_fdc(fdcu, NE7_SPEC_2(2, 0)); 6485b81b6b3SRodney W. Grimes } 6495b81b6b3SRodney W. Grimes 6505b81b6b3SRodney W. Grimes /****************************************************************************/ 6515b81b6b3SRodney W. Grimes /* fdc in/out */ 6525b81b6b3SRodney W. Grimes /****************************************************************************/ 6535b81b6b3SRodney W. Grimes int 654f5f7ba03SJordan K. Hubbard in_fdc(fdcu) 655f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 6565b81b6b3SRodney W. Grimes { 6575b81b6b3SRodney W. Grimes int baseport = fdc_data[fdcu].baseport; 6585b81b6b3SRodney W. Grimes int i, j = 100000; 6593a2f7427SDavid Greenman while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 6605b81b6b3SRodney W. Grimes != (NE7_DIO|NE7_RQM) && j-- > 0) 6615b81b6b3SRodney W. Grimes if (i == NE7_RQM) return -1; 6625b81b6b3SRodney W. Grimes if (j <= 0) 6635b81b6b3SRodney W. Grimes return(-1); 6645b81b6b3SRodney W. Grimes #ifdef DEBUG 6653a2f7427SDavid Greenman i = inb(baseport+FDDATA); 6663a2f7427SDavid Greenman TRACE1("[FDDATA->0x%x]", (unsigned char)i); 6675b81b6b3SRodney W. Grimes return(i); 6685b81b6b3SRodney W. Grimes #else 6693a2f7427SDavid Greenman return inb(baseport+FDDATA); 6705b81b6b3SRodney W. Grimes #endif 6715b81b6b3SRodney W. Grimes } 6725b81b6b3SRodney W. Grimes 673381fe1aaSGarrett Wollman int 674f5f7ba03SJordan K. Hubbard out_fdc(fdcu, x) 675f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 676f5f7ba03SJordan K. Hubbard int x; 6775b81b6b3SRodney W. Grimes { 6785b81b6b3SRodney W. Grimes int baseport = fdc_data[fdcu].baseport; 6793b3837dbSRodney W. Grimes int i; 6805b81b6b3SRodney W. Grimes 6813b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 6823b3837dbSRodney W. Grimes i = 100000; 6833a2f7427SDavid Greenman while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); 6843b3837dbSRodney W. Grimes if (i <= 0) return (-1); /* Floppy timed out */ 6853b3837dbSRodney W. Grimes 6863b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 6873b3837dbSRodney W. Grimes i = 100000; 6883a2f7427SDavid Greenman while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); 6893b3837dbSRodney W. Grimes if (i <= 0) return (-1); /* Floppy timed out */ 6903b3837dbSRodney W. Grimes 6913b3837dbSRodney W. Grimes /* Send the command and return */ 6923a2f7427SDavid Greenman outb(baseport+FDDATA, x); 6933a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 6945b81b6b3SRodney W. Grimes return (0); 6955b81b6b3SRodney W. Grimes } 6965b81b6b3SRodney W. Grimes 6975b81b6b3SRodney W. Grimes /****************************************************************************/ 6985b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 6995b81b6b3SRodney W. Grimes /****************************************************************************/ 700381fe1aaSGarrett Wollman int 7015b81b6b3SRodney W. Grimes Fdopen(dev, flags) 7025b81b6b3SRodney W. Grimes dev_t dev; 7035b81b6b3SRodney W. Grimes int flags; 7045b81b6b3SRodney W. Grimes { 7055b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 70620a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 707b99f0a4aSAndrew Moore fdc_p fdc; 7086febd9aaSJordan K. Hubbard int st3; 7095b81b6b3SRodney W. Grimes 710b99f0a4aSAndrew Moore #if NFT > 0 711b99f0a4aSAndrew Moore /* check for a tape open */ 712b99f0a4aSAndrew Moore if (type & F_TAPE_TYPE) 713b99f0a4aSAndrew Moore return(ftopen(dev, flags)); 714b99f0a4aSAndrew Moore #endif 7155b81b6b3SRodney W. Grimes /* check bounds */ 716b99f0a4aSAndrew Moore if (fdu >= NFD) 717b99f0a4aSAndrew Moore return(ENXIO); 718b99f0a4aSAndrew Moore fdc = fd_data[fdu].fdc; 719b99f0a4aSAndrew Moore if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) 720b99f0a4aSAndrew Moore return(ENXIO); 721b99f0a4aSAndrew Moore if (type > NUMDENS) 722b99f0a4aSAndrew Moore return(ENXIO); 7237ca0641bSAndrey A. Chernov if (type == 0) 7247ca0641bSAndrey A. Chernov type = fd_data[fdu].type; 7257ca0641bSAndrey A. Chernov else { 7267ca0641bSAndrey A. Chernov if (type != fd_data[fdu].type) { 727fa4700b4SAndrey A. Chernov switch (fd_data[fdu].type) { 7287ca0641bSAndrey A. Chernov case FD_360: 7297ca0641bSAndrey A. Chernov return(ENXIO); 730ed2fa05eSAndrey A. Chernov case FD_720: 731b39c878eSAndrey A. Chernov if ( type != FD_820 732b39c878eSAndrey A. Chernov && type != FD_800 733ed2fa05eSAndrey A. Chernov ) 734ed2fa05eSAndrey A. Chernov return(ENXIO); 735ed2fa05eSAndrey A. Chernov break; 7367ca0641bSAndrey A. Chernov case FD_1200: 737b39c878eSAndrey A. Chernov switch (type) { 738b39c878eSAndrey A. Chernov case FD_1480: 739b39c878eSAndrey A. Chernov type = FD_1480in5_25; 740fa4700b4SAndrey A. Chernov break; 7417ca0641bSAndrey A. Chernov case FD_1440: 742b39c878eSAndrey A. Chernov type = FD_1440in5_25; 743b39c878eSAndrey A. Chernov break; 744b39c878eSAndrey A. Chernov case FD_820: 745b39c878eSAndrey A. Chernov type = FD_820in5_25; 746b39c878eSAndrey A. Chernov break; 747b39c878eSAndrey A. Chernov case FD_800: 748b39c878eSAndrey A. Chernov type = FD_800in5_25; 749b39c878eSAndrey A. Chernov break; 750b39c878eSAndrey A. Chernov case FD_720: 751b39c878eSAndrey A. Chernov type = FD_720in5_25; 752b39c878eSAndrey A. Chernov break; 753b39c878eSAndrey A. Chernov case FD_360: 754b39c878eSAndrey A. Chernov type = FD_360in5_25; 755b39c878eSAndrey A. Chernov break; 756b39c878eSAndrey A. Chernov default: 757b39c878eSAndrey A. Chernov return(ENXIO); 758b39c878eSAndrey A. Chernov } 759b39c878eSAndrey A. Chernov break; 760b39c878eSAndrey A. Chernov case FD_1440: 761b39c878eSAndrey A. Chernov if ( type != FD_1720 762b39c878eSAndrey A. Chernov && type != FD_1480 763ed2fa05eSAndrey A. Chernov && type != FD_1200 764b39c878eSAndrey A. Chernov && type != FD_820 765b39c878eSAndrey A. Chernov && type != FD_800 766b39c878eSAndrey A. Chernov && type != FD_720 7677ca0641bSAndrey A. Chernov ) 768dffff499SAndrey A. Chernov return(ENXIO); 769fa4700b4SAndrey A. Chernov break; 7707ca0641bSAndrey A. Chernov } 7717ca0641bSAndrey A. Chernov } 772fa4700b4SAndrey A. Chernov } 7736febd9aaSJordan K. Hubbard 7746febd9aaSJordan K. Hubbard /* select it */ 7756febd9aaSJordan K. Hubbard set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNON); 7766febd9aaSJordan K. Hubbard 7776febd9aaSJordan K. Hubbard if (flags & FWRITE) { 7786febd9aaSJordan K. Hubbard /* Check to make sure the diskette */ 7796febd9aaSJordan K. Hubbard /* is writable */ 7806febd9aaSJordan K. Hubbard out_fdc(fdc->fdcu, NE7CMD_SENSED); 7816febd9aaSJordan K. Hubbard out_fdc(fdc->fdcu, fdu); 7826febd9aaSJordan K. Hubbard st3 = in_fdc(fdc->fdcu); 7836febd9aaSJordan K. Hubbard if(st3 & NE7_ST3_WP) { 7844b69fb52SAndrey A. Chernov printf("fd%d: write protected\n", fdu); 7856febd9aaSJordan K. Hubbard set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNOFF); 7866febd9aaSJordan K. Hubbard return(EPERM); 7876febd9aaSJordan K. Hubbard } 7886febd9aaSJordan K. Hubbard } 7896febd9aaSJordan K. Hubbard 7906febd9aaSJordan K. Hubbard set_motor(fdc->fdcu, fd_data[fdu].fdsu, TURNOFF); 7916febd9aaSJordan K. Hubbard 792b99f0a4aSAndrew Moore fd_data[fdu].ft = fd_types + type - 1; 7935b81b6b3SRodney W. Grimes fd_data[fdu].flags |= FD_OPEN; 7945b81b6b3SRodney W. Grimes 7955b81b6b3SRodney W. Grimes return 0; 7965b81b6b3SRodney W. Grimes } 7975b81b6b3SRodney W. Grimes 798381fe1aaSGarrett Wollman int 7995b81b6b3SRodney W. Grimes fdclose(dev, flags) 8005b81b6b3SRodney W. Grimes dev_t dev; 801381fe1aaSGarrett Wollman int flags; 8025b81b6b3SRodney W. Grimes { 8035b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 804b99f0a4aSAndrew Moore 805b99f0a4aSAndrew Moore #if NFT > 0 8063a2f7427SDavid Greenman int type = FDTYPE(minor(dev)); 8073a2f7427SDavid Greenman 808b99f0a4aSAndrew Moore if (type & F_TAPE_TYPE) 8093a2f7427SDavid Greenman return ftclose(dev, flags); 810b99f0a4aSAndrew Moore #endif 8115b81b6b3SRodney W. Grimes fd_data[fdu].flags &= ~FD_OPEN; 8123a2f7427SDavid Greenman fd_data[fdu].options &= ~FDOPT_NORETRY; 8135b81b6b3SRodney W. Grimes return(0); 8145b81b6b3SRodney W. Grimes } 8155b81b6b3SRodney W. Grimes 8165b81b6b3SRodney W. Grimes 8173a2f7427SDavid Greenman /****************************************************************************/ 8183a2f7427SDavid Greenman /* fdstrategy */ 8193a2f7427SDavid Greenman /****************************************************************************/ 8203a2f7427SDavid Greenman void 8213a2f7427SDavid Greenman fdstrategy(struct buf *bp) 8223a2f7427SDavid Greenman { 8233a2f7427SDavid Greenman register struct buf *dp; 8243a2f7427SDavid Greenman long nblocks, blknum; 8253a2f7427SDavid Greenman int s; 8263a2f7427SDavid Greenman fdcu_t fdcu; 8273a2f7427SDavid Greenman fdu_t fdu; 8283a2f7427SDavid Greenman fdc_p fdc; 8293a2f7427SDavid Greenman fd_p fd; 8303a2f7427SDavid Greenman size_t fdblk; 8313a2f7427SDavid Greenman 8323a2f7427SDavid Greenman fdu = FDUNIT(minor(bp->b_dev)); 8333a2f7427SDavid Greenman fd = &fd_data[fdu]; 8343a2f7427SDavid Greenman fdc = fd->fdc; 8353a2f7427SDavid Greenman fdcu = fdc->fdcu; 8363a2f7427SDavid Greenman fdblk = 128 << (fd->ft->secsize); 8373a2f7427SDavid Greenman 8383a2f7427SDavid Greenman #if NFT > 0 8393a2f7427SDavid Greenman if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) { 8403a2f7427SDavid Greenman /* ft tapes do not (yet) support strategy i/o */ 8413a2f7427SDavid Greenman bp->b_error = ENXIO; 8423a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 8433a2f7427SDavid Greenman goto bad; 8443a2f7427SDavid Greenman } 8453a2f7427SDavid Greenman /* check for controller already busy with tape */ 8463a2f7427SDavid Greenman if (fdc->flags & FDC_TAPE_BUSY) { 8473a2f7427SDavid Greenman bp->b_error = EBUSY; 8483a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 8493a2f7427SDavid Greenman goto bad; 8503a2f7427SDavid Greenman } 8513a2f7427SDavid Greenman #endif 8523a2f7427SDavid Greenman if (!(bp->b_flags & B_FORMAT)) { 8533a2f7427SDavid Greenman if ((fdu >= NFD) || (bp->b_blkno < 0)) { 854702c623aSPoul-Henning Kamp printf("fdstrat: fdu = %d, blkno = %lu, bcount = %ld\n", 855702c623aSPoul-Henning Kamp fdu, (u_long)bp->b_blkno, bp->b_bcount); 8563a2f7427SDavid Greenman bp->b_error = EINVAL; 8573a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 8583a2f7427SDavid Greenman goto bad; 8593a2f7427SDavid Greenman } 8603a2f7427SDavid Greenman if ((bp->b_bcount % fdblk) != 0) { 8613a2f7427SDavid Greenman bp->b_error = EINVAL; 8623a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 8633a2f7427SDavid Greenman goto bad; 8643a2f7427SDavid Greenman } 8653a2f7427SDavid Greenman } 8663a2f7427SDavid Greenman 8673a2f7427SDavid Greenman /* 8683a2f7427SDavid Greenman * Set up block calculations. 8693a2f7427SDavid Greenman */ 8703a2f7427SDavid Greenman blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk; 8713a2f7427SDavid Greenman nblocks = fd->ft->size; 8723a2f7427SDavid Greenman if (blknum + (bp->b_bcount / fdblk) > nblocks) { 8733a2f7427SDavid Greenman if (blknum == nblocks) { 8743a2f7427SDavid Greenman bp->b_resid = bp->b_bcount; 8753a2f7427SDavid Greenman } else { 8763a2f7427SDavid Greenman bp->b_error = ENOSPC; 8773a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 8783a2f7427SDavid Greenman } 8793a2f7427SDavid Greenman goto bad; 8803a2f7427SDavid Greenman } 8813a2f7427SDavid Greenman bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads); 8823a2f7427SDavid Greenman dp = &(fdc->head); 8833a2f7427SDavid Greenman s = splbio(); 8843a2f7427SDavid Greenman disksort(dp, bp); 8855e235068SJordan K. Hubbard untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */ 8863a2f7427SDavid Greenman fdstart(fdcu); 8873a2f7427SDavid Greenman splx(s); 8883a2f7427SDavid Greenman return; 8893a2f7427SDavid Greenman 8903a2f7427SDavid Greenman bad: 8913a2f7427SDavid Greenman biodone(bp); 8923a2f7427SDavid Greenman } 8933a2f7427SDavid Greenman 8945b81b6b3SRodney W. Grimes /***************************************************************\ 8955b81b6b3SRodney W. Grimes * fdstart * 8965b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 8975b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 8985b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 8995b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 9005b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 9015b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 9025b81b6b3SRodney W. Grimes \***************************************************************/ 903381fe1aaSGarrett Wollman static void 904f5f7ba03SJordan K. Hubbard fdstart(fdcu) 905f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 9065b81b6b3SRodney W. Grimes { 9075b81b6b3SRodney W. Grimes int s; 9085b81b6b3SRodney W. Grimes 9095b81b6b3SRodney W. Grimes s = splbio(); 9105b81b6b3SRodney W. Grimes if(fdc_data[fdcu].state == DEVIDLE) 9115b81b6b3SRodney W. Grimes { 9125b81b6b3SRodney W. Grimes fdintr(fdcu); 9135b81b6b3SRodney W. Grimes } 9145b81b6b3SRodney W. Grimes splx(s); 9155b81b6b3SRodney W. Grimes } 9165b81b6b3SRodney W. Grimes 9175e235068SJordan K. Hubbard /* ARGSUSED */ 918381fe1aaSGarrett Wollman static void 919d0917939SPaul Richards fd_timeout(void *arg1) 9205b81b6b3SRodney W. Grimes { 921381fe1aaSGarrett Wollman fdcu_t fdcu = (fdcu_t)arg1; 9225b81b6b3SRodney W. Grimes fdu_t fdu = fdc_data[fdcu].fdu; 9233a2f7427SDavid Greenman int baseport = fdc_data[fdcu].baseport; 9245b81b6b3SRodney W. Grimes struct buf *dp, *bp; 925f5f7ba03SJordan K. Hubbard int s; 9265b81b6b3SRodney W. Grimes 9275b81b6b3SRodney W. Grimes dp = &fdc_data[fdcu].head; 9285b81b6b3SRodney W. Grimes bp = dp->b_actf; 9295b81b6b3SRodney W. Grimes 9303a2f7427SDavid Greenman /* 9313a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 9323a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 9333a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 9343a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 9353a2f7427SDavid Greenman * Many thanks, Big Blue! 9363a2f7427SDavid Greenman */ 9375b81b6b3SRodney W. Grimes 9383a2f7427SDavid Greenman s = splbio(); 9393a2f7427SDavid Greenman 9403a2f7427SDavid Greenman TRACE1("fd%d[fd_timeout()]", fdu); 9413a2f7427SDavid Greenman /* See if the controller is still busy (patiently awaiting data) */ 9423a2f7427SDavid Greenman if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB) 9433a2f7427SDavid Greenman { 9443a2f7427SDavid Greenman TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS)); 9453a2f7427SDavid Greenman /* yup, it is; kill it now */ 9463a2f7427SDavid Greenman fdc_reset(&fdc_data[fdcu]); 9473a2f7427SDavid Greenman printf("fd%d: Operation timeout\n", fdu); 9483a2f7427SDavid Greenman } 9495b81b6b3SRodney W. Grimes 9505b81b6b3SRodney W. Grimes if (bp) 9515b81b6b3SRodney W. Grimes { 9525b81b6b3SRodney W. Grimes retrier(fdcu); 9533a2f7427SDavid Greenman fdc_data[fdcu].status[0] = NE7_ST0_IC_RC; 9545b81b6b3SRodney W. Grimes fdc_data[fdcu].state = IOTIMEDOUT; 9555b81b6b3SRodney W. Grimes if( fdc_data[fdcu].retry < 6) 9565b81b6b3SRodney W. Grimes fdc_data[fdcu].retry = 6; 9575b81b6b3SRodney W. Grimes } 9585b81b6b3SRodney W. Grimes else 9595b81b6b3SRodney W. Grimes { 9605b81b6b3SRodney W. Grimes fdc_data[fdcu].fd = (fd_p) 0; 9615b81b6b3SRodney W. Grimes fdc_data[fdcu].fdu = -1; 9625b81b6b3SRodney W. Grimes fdc_data[fdcu].state = DEVIDLE; 9635b81b6b3SRodney W. Grimes } 964f5f7ba03SJordan K. Hubbard fdintr(fdcu); 965f5f7ba03SJordan K. Hubbard splx(s); 9665b81b6b3SRodney W. Grimes } 9675b81b6b3SRodney W. Grimes 9685b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 9695e235068SJordan K. Hubbard /* ARGSUSED */ 970381fe1aaSGarrett Wollman static void 971d0917939SPaul Richards fd_pseudointr(void *arg1) 9725b81b6b3SRodney W. Grimes { 973381fe1aaSGarrett Wollman fdcu_t fdcu = (fdcu_t)arg1; 9745b81b6b3SRodney W. Grimes int s; 9753a2f7427SDavid Greenman 9765b81b6b3SRodney W. Grimes s = splbio(); 9775b81b6b3SRodney W. Grimes fdintr(fdcu); 9785b81b6b3SRodney W. Grimes splx(s); 9795b81b6b3SRodney W. Grimes } 9805b81b6b3SRodney W. Grimes 9815b81b6b3SRodney W. Grimes /***********************************************************************\ 9825b81b6b3SRodney W. Grimes * fdintr * 9835b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 9845b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 9855b81b6b3SRodney W. Grimes \***********************************************************************/ 986381fe1aaSGarrett Wollman void 987381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu) 9885b81b6b3SRodney W. Grimes { 9895b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 990b99f0a4aSAndrew Moore #if NFT > 0 991b99f0a4aSAndrew Moore fdu_t fdu = fdc->fdu; 992b99f0a4aSAndrew Moore 993b99f0a4aSAndrew Moore if (fdc->flags & FDC_TAPE_BUSY) 994b99f0a4aSAndrew Moore (ftintr(fdu)); 995b99f0a4aSAndrew Moore else 996b99f0a4aSAndrew Moore #endif 997381fe1aaSGarrett Wollman while(fdstate(fdcu, fdc)) 998381fe1aaSGarrett Wollman ; 9995b81b6b3SRodney W. Grimes } 10005b81b6b3SRodney W. Grimes 10015b81b6b3SRodney W. Grimes /***********************************************************************\ 10025b81b6b3SRodney W. Grimes * The controller state machine. * 10035b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 10045b81b6b3SRodney W. Grimes \***********************************************************************/ 10053a2f7427SDavid Greenman static int 1006381fe1aaSGarrett Wollman fdstate(fdcu, fdc) 1007f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 1008f5f7ba03SJordan K. Hubbard fdc_p fdc; 10095b81b6b3SRodney W. Grimes { 10103a2f7427SDavid Greenman int read, format, head, sec = 0, i = 0, sectrac, st0, cyl, st3; 10115b81b6b3SRodney W. Grimes unsigned long blknum; 10125b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 10135b81b6b3SRodney W. Grimes fd_p fd; 10145b81b6b3SRodney W. Grimes register struct buf *dp, *bp; 1015b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 10163a2f7427SDavid Greenman size_t fdblk; 10175b81b6b3SRodney W. Grimes 10185b81b6b3SRodney W. Grimes dp = &(fdc->head); 10195b81b6b3SRodney W. Grimes bp = dp->b_actf; 10205b81b6b3SRodney W. Grimes if(!bp) 10215b81b6b3SRodney W. Grimes { 10225b81b6b3SRodney W. Grimes /***********************************************\ 10235b81b6b3SRodney W. Grimes * nothing left for this controller to do * 10245b81b6b3SRodney W. Grimes * Force into the IDLE state, * 10255b81b6b3SRodney W. Grimes \***********************************************/ 10265b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 10275b81b6b3SRodney W. Grimes if(fdc->fd) 10285b81b6b3SRodney W. Grimes { 10293a2f7427SDavid Greenman printf("unexpected valid fd pointer (fdu = %d)\n", 10303a2f7427SDavid Greenman fdc->fdu); 10315b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 10325b81b6b3SRodney W. Grimes fdc->fdu = -1; 10335b81b6b3SRodney W. Grimes } 10345b81b6b3SRodney W. Grimes TRACE1("[fdc%d IDLE]", fdcu); 10355b81b6b3SRodney W. Grimes return(0); 10365b81b6b3SRodney W. Grimes } 10375b81b6b3SRodney W. Grimes fdu = FDUNIT(minor(bp->b_dev)); 10385b81b6b3SRodney W. Grimes fd = fd_data + fdu; 10393a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 10405b81b6b3SRodney W. Grimes if (fdc->fd && (fd != fdc->fd)) 10415b81b6b3SRodney W. Grimes { 10425b81b6b3SRodney W. Grimes printf("confused fd pointers\n"); 10435b81b6b3SRodney W. Grimes } 10445b81b6b3SRodney W. Grimes read = bp->b_flags & B_READ; 1045b39c878eSAndrey A. Chernov format = bp->b_flags & B_FORMAT; 1046b39c878eSAndrey A. Chernov if(format) 1047b39c878eSAndrey A. Chernov finfo = (struct fd_formb *)bp->b_un.b_addr; 10485b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 10495b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 10505b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 10515e235068SJordan K. Hubbard untimeout(fd_turnoff, (caddr_t)fdu); 10525e235068SJordan K. Hubbard timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); 10535b81b6b3SRodney W. Grimes switch (fdc->state) 10545b81b6b3SRodney W. Grimes { 10555b81b6b3SRodney W. Grimes case DEVIDLE: 10565b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 10575b81b6b3SRodney W. Grimes fdc->retry = 0; 10585b81b6b3SRodney W. Grimes fd->skip = 0; 10595b81b6b3SRodney W. Grimes fdc->fd = fd; 10605b81b6b3SRodney W. Grimes fdc->fdu = fdu; 10613a2f7427SDavid Greenman outb(fdc->baseport+FDCTL, fd->ft->trans); 10623a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 10635b81b6b3SRodney W. Grimes /*******************************************************\ 10645b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 10655b81b6b3SRodney W. Grimes * it will start up in it's own good time * 10665b81b6b3SRodney W. Grimes \*******************************************************/ 10675b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 10685b81b6b3SRodney W. Grimes { 10695b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 10705b81b6b3SRodney W. Grimes return(0); /* come back later */ 10715b81b6b3SRodney W. Grimes } 10725b81b6b3SRodney W. Grimes /*******************************************************\ 10735b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 10745b81b6b3SRodney W. Grimes \*******************************************************/ 10755b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 10765b81b6b3SRodney W. Grimes { 10775b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 10785b81b6b3SRodney W. Grimes fd_turnon(fdu); 10795b81b6b3SRodney W. Grimes return(0); 10805b81b6b3SRodney W. Grimes } 10815b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 10825b81b6b3SRodney W. Grimes { 10833a2f7427SDavid Greenman set_motor(fdcu, fd->fdsu, TURNON); 10845b81b6b3SRodney W. Grimes } 10855e235068SJordan K. Hubbard fdc->state = DOSEEK; 10865b81b6b3SRodney W. Grimes break; 10875b81b6b3SRodney W. Grimes case DOSEEK: 10885b81b6b3SRodney W. Grimes if (bp->b_cylin == fd->track) 10895b81b6b3SRodney W. Grimes { 10905b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 10915b81b6b3SRodney W. Grimes break; 10925b81b6b3SRodney W. Grimes } 1093dc8603e3SJoerg Wunsch if(out_fdc(fdcu, NE7CMD_SEEK) < 0) /* Seek function */ 1094dc8603e3SJoerg Wunsch { 1095dc8603e3SJoerg Wunsch /* 1096dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1097dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1098dc8603e3SJoerg Wunsch */ 1099dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 1100dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1101dc8603e3SJoerg Wunsch } 11025b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->fdsu); /* Drive number */ 11035b81b6b3SRodney W. Grimes out_fdc(fdcu, bp->b_cylin * fd->ft->steptrac); 11045b81b6b3SRodney W. Grimes fd->track = -2; 11055b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 11065b81b6b3SRodney W. Grimes return(0); /* will return later */ 11075b81b6b3SRodney W. Grimes case SEEKWAIT: 11085b81b6b3SRodney W. Grimes /* allow heads to settle */ 110904b734cfSPoul-Henning Kamp timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); 11105b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 11115b81b6b3SRodney W. Grimes return(0); /* will return later */ 11125b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 11135b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 11145b81b6b3SRodney W. Grimes if(fd->track == -2) 11155b81b6b3SRodney W. Grimes { 11165b81b6b3SRodney W. Grimes int descyl = bp->b_cylin * fd->ft->steptrac; 11173a2f7427SDavid Greenman do { 11185b81b6b3SRodney W. Grimes out_fdc(fdcu, NE7CMD_SENSEI); 11193a2f7427SDavid Greenman st0 = in_fdc(fdcu); 11205b81b6b3SRodney W. Grimes cyl = in_fdc(fdcu); 11213a2f7427SDavid Greenman /* 11223a2f7427SDavid Greenman * if this was a "ready changed" interrupt, 11233a2f7427SDavid Greenman * fetch status again (can happen after 11243a2f7427SDavid Greenman * enabling controller from reset state) 11253a2f7427SDavid Greenman */ 11263a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 11273a2f7427SDavid Greenman if (0 == descyl) 11283a2f7427SDavid Greenman { 11293a2f7427SDavid Greenman /* 11303a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 11313a2f7427SDavid Greenman * really there 11323a2f7427SDavid Greenman */ 11333a2f7427SDavid Greenman out_fdc(fdcu, NE7CMD_SENSED); 11343a2f7427SDavid Greenman out_fdc(fdcu, fdu); 11353a2f7427SDavid Greenman st3 = in_fdc(fdcu); 11363a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 11373a2f7427SDavid Greenman printf( 11383a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 11393a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 11403a2f7427SDavid Greenman if(fdc->retry < 3) 11413a2f7427SDavid Greenman fdc->retry = 3; 11423a2f7427SDavid Greenman return(retrier(fdcu)); 11433a2f7427SDavid Greenman } 11443a2f7427SDavid Greenman } 11455b81b6b3SRodney W. Grimes if (cyl != descyl) 11465b81b6b3SRodney W. Grimes { 11473a2f7427SDavid Greenman printf( 11483a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 11495e235068SJordan K. Hubbard fdu, descyl, cyl, st0, NE7_ST0BITS); 11505b81b6b3SRodney W. Grimes return(retrier(fdcu)); 11515b81b6b3SRodney W. Grimes } 11525b81b6b3SRodney W. Grimes } 11535b81b6b3SRodney W. Grimes 11545b81b6b3SRodney W. Grimes fd->track = bp->b_cylin; 1155b39c878eSAndrey A. Chernov if(format) 1156b39c878eSAndrey A. Chernov fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1157b39c878eSAndrey A. Chernov - (char *)finfo; 11585b81b6b3SRodney W. Grimes isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip, 11593a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 11603a2f7427SDavid Greenman blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk 11613a2f7427SDavid Greenman + fd->skip/fdblk; 11625b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 11635b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 11645b81b6b3SRodney W. Grimes head = sec / sectrac; 11655b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 11663a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 11673a2f7427SDavid Greenman 11683a2f7427SDavid Greenman if(format || !read) 11693a2f7427SDavid Greenman { 11703a2f7427SDavid Greenman /* make sure the drive is writable */ 1171dc8603e3SJoerg Wunsch if(out_fdc(fdcu, NE7CMD_SENSED) < 0) 1172dc8603e3SJoerg Wunsch { 1173dc8603e3SJoerg Wunsch /* stuck controller? */ 1174dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 1175dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1176dc8603e3SJoerg Wunsch } 11773a2f7427SDavid Greenman out_fdc(fdcu, fdu); 11783a2f7427SDavid Greenman st3 = in_fdc(fdcu); 11793a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 11803a2f7427SDavid Greenman { 11813a2f7427SDavid Greenman /* 11823a2f7427SDavid Greenman * XXX YES! this is ugly. 11833a2f7427SDavid Greenman * in order to force the current operation 11843a2f7427SDavid Greenman * to fail, we will have to fake an FDC 11853a2f7427SDavid Greenman * error - all error handling is done 11863a2f7427SDavid Greenman * by the retrier() 11873a2f7427SDavid Greenman */ 11883a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 11893a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 11903a2f7427SDavid Greenman fdc->status[2] = 0; 11913a2f7427SDavid Greenman fdc->status[3] = fd->track; 11923a2f7427SDavid Greenman fdc->status[4] = head; 11933a2f7427SDavid Greenman fdc->status[5] = sec; 11943a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 11953a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 11963a2f7427SDavid Greenman return (1); 11973a2f7427SDavid Greenman } 11983a2f7427SDavid Greenman } 11995b81b6b3SRodney W. Grimes 1200b39c878eSAndrey A. Chernov if(format) 1201b39c878eSAndrey A. Chernov { 1202b39c878eSAndrey A. Chernov /* formatting */ 1203dc8603e3SJoerg Wunsch if(out_fdc(fdcu, NE7CMD_FORMAT) < 0) 1204dc8603e3SJoerg Wunsch { 1205dc8603e3SJoerg Wunsch /* controller fell over */ 1206dc8603e3SJoerg Wunsch fdc->retry = 6; 1207dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1208dc8603e3SJoerg Wunsch } 1209b39c878eSAndrey A. Chernov out_fdc(fdcu, head << 2 | fdu); 1210b39c878eSAndrey A. Chernov out_fdc(fdcu, finfo->fd_formb_secshift); 1211b39c878eSAndrey A. Chernov out_fdc(fdcu, finfo->fd_formb_nsecs); 1212b39c878eSAndrey A. Chernov out_fdc(fdcu, finfo->fd_formb_gaplen); 1213b39c878eSAndrey A. Chernov out_fdc(fdcu, finfo->fd_formb_fillbyte); 1214b39c878eSAndrey A. Chernov } 1215b39c878eSAndrey A. Chernov else 1216b39c878eSAndrey A. Chernov { 1217dc8603e3SJoerg Wunsch if ((read && out_fdc(fdcu, NE7CMD_READ) < 0) 1218dc8603e3SJoerg Wunsch || (!read /* i.e., write */ 1219dc8603e3SJoerg Wunsch && out_fdc(fdcu, NE7CMD_WRITE) < 0)) 12205b81b6b3SRodney W. Grimes { 1221dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 1222dc8603e3SJoerg Wunsch fdc->retry = 6; 1223dc8603e3SJoerg Wunsch return(retrier(fdcu)); 12245b81b6b3SRodney W. Grimes } 12255b81b6b3SRodney W. Grimes out_fdc(fdcu, head << 2 | fdu); /* head & unit */ 12265b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->track); /* track */ 12275b81b6b3SRodney W. Grimes out_fdc(fdcu, head); 12285b81b6b3SRodney W. Grimes out_fdc(fdcu, sec); /* sector XXX +1? */ 12295b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->ft->secsize); /* sector size */ 12305b81b6b3SRodney W. Grimes out_fdc(fdcu, sectrac); /* sectors/track */ 12315b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->ft->gap); /* gap size */ 12325b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->ft->datalen); /* data length */ 1233b39c878eSAndrey A. Chernov } 12345b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 12355e235068SJordan K. Hubbard timeout(fd_timeout, (caddr_t)fdcu, hz); 12365b81b6b3SRodney W. Grimes return(0); /* will return later */ 12375b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 12385e235068SJordan K. Hubbard untimeout(fd_timeout, (caddr_t)fdcu); 12395b81b6b3SRodney W. Grimes for(i=0;i<7;i++) 12405b81b6b3SRodney W. Grimes { 12415b81b6b3SRodney W. Grimes fdc->status[i] = in_fdc(fdcu); 12425b81b6b3SRodney W. Grimes } 12433a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 12443a2f7427SDavid Greenman /* FALLTHROUGH */ 12453a2f7427SDavid Greenman case IOTIMEDOUT: 12465b81b6b3SRodney W. Grimes isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip, 12473a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 12483a2f7427SDavid Greenman if (fdc->status[0] & NE7_ST0_IC) 12495b81b6b3SRodney W. Grimes { 12503a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 12513a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 1252b39c878eSAndrey A. Chernov /* 12533a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 12543a2f7427SDavid Greenman * and didn't release it in time for the 12553a2f7427SDavid Greenman * next FDC transfer. 12563a2f7427SDavid Greenman * Just restart it, don't increment retry 12573a2f7427SDavid Greenman * count. (vak) 1258b39c878eSAndrey A. Chernov */ 1259b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 1260b39c878eSAndrey A. Chernov return (1); 1261b39c878eSAndrey A. Chernov } 12623a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 12633a2f7427SDavid Greenman && fdc->retry < 6) 12643a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 12653a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 12663a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 12673a2f7427SDavid Greenman && fdc->retry < 3) 12683a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 12695b81b6b3SRodney W. Grimes return(retrier(fdcu)); 12705b81b6b3SRodney W. Grimes } 12715b81b6b3SRodney W. Grimes /* All OK */ 12723a2f7427SDavid Greenman fd->skip += fdblk; 1273b39c878eSAndrey A. Chernov if (!format && fd->skip < bp->b_bcount) 12745b81b6b3SRodney W. Grimes { 12755b81b6b3SRodney W. Grimes /* set up next transfer */ 12763a2f7427SDavid Greenman blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk 12773a2f7427SDavid Greenman + fd->skip/fdblk; 12783a2f7427SDavid Greenman bp->b_cylin = 12793a2f7427SDavid Greenman (blknum / (fd->ft->sectrac * fd->ft->heads)); 12805b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 12815b81b6b3SRodney W. Grimes } 12825b81b6b3SRodney W. Grimes else 12835b81b6b3SRodney W. Grimes { 12845b81b6b3SRodney W. Grimes /* ALL DONE */ 12855b81b6b3SRodney W. Grimes fd->skip = 0; 12865b81b6b3SRodney W. Grimes bp->b_resid = 0; 128726f9a767SRodney W. Grimes dp->b_actf = bp->b_actf; 12885b81b6b3SRodney W. Grimes biodone(bp); 12895b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 12905b81b6b3SRodney W. Grimes fdc->fdu = -1; 12915b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 12925b81b6b3SRodney W. Grimes } 12935b81b6b3SRodney W. Grimes return(1); 12945b81b6b3SRodney W. Grimes case RESETCTLR: 12953a2f7427SDavid Greenman fdc_reset(fdc); 12965b81b6b3SRodney W. Grimes fdc->retry++; 12975b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 12985b81b6b3SRodney W. Grimes break; 12995b81b6b3SRodney W. Grimes case STARTRECAL: 1300dc8603e3SJoerg Wunsch if(out_fdc(fdcu, NE7CMD_RECAL) < 0) /* Recalibrate Function */ 1301dc8603e3SJoerg Wunsch { 1302dc8603e3SJoerg Wunsch /* arrgl */ 1303dc8603e3SJoerg Wunsch fdc->retry = 6; 1304dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1305dc8603e3SJoerg Wunsch } 13065b81b6b3SRodney W. Grimes out_fdc(fdcu, fdu); 13075b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 13085b81b6b3SRodney W. Grimes return(0); /* will return later */ 13095b81b6b3SRodney W. Grimes case RECALWAIT: 13105b81b6b3SRodney W. Grimes /* allow heads to settle */ 131104b734cfSPoul-Henning Kamp timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); 13125b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 13135b81b6b3SRodney W. Grimes return(0); /* will return later */ 13145b81b6b3SRodney W. Grimes case RECALCOMPLETE: 13153a2f7427SDavid Greenman do { 13165b81b6b3SRodney W. Grimes out_fdc(fdcu, NE7CMD_SENSEI); 13175b81b6b3SRodney W. Grimes st0 = in_fdc(fdcu); 13185b81b6b3SRodney W. Grimes cyl = in_fdc(fdcu); 13193a2f7427SDavid Greenman /* 13203a2f7427SDavid Greenman * if this was a "ready changed" interrupt, 13213a2f7427SDavid Greenman * fetch status again (can happen after 13223a2f7427SDavid Greenman * enabling controller from reset state) 13233a2f7427SDavid Greenman */ 13243a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 13253a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 13265b81b6b3SRodney W. Grimes { 1327dc8603e3SJoerg Wunsch if(fdc->retry > 3) 1328dc8603e3SJoerg Wunsch /* 1329dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 1330dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 1331dc8603e3SJoerg Wunsch * since people used to complain much about 1332dc8603e3SJoerg Wunsch * the failure message, try not logging 1333dc8603e3SJoerg Wunsch * this one if it seems to be the first 1334dc8603e3SJoerg Wunsch * time in a line 1335dc8603e3SJoerg Wunsch */ 1336dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 1337dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 13383a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 13395b81b6b3SRodney W. Grimes return(retrier(fdcu)); 13405b81b6b3SRodney W. Grimes } 13415b81b6b3SRodney W. Grimes fd->track = 0; 13425b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 13435b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 13445b81b6b3SRodney W. Grimes return(1); /* will return immediatly */ 13455b81b6b3SRodney W. Grimes case MOTORWAIT: 13465b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 13475b81b6b3SRodney W. Grimes { 13485b81b6b3SRodney W. Grimes return(0); /* time's not up yet */ 13495b81b6b3SRodney W. Grimes } 13505e235068SJordan K. Hubbard /* 13515e235068SJordan K. Hubbard * since the controller was off, it has lost its 13525e235068SJordan K. Hubbard * idea about the current track it were; thus, 13535e235068SJordan K. Hubbard * recalibrate the bastard 13545e235068SJordan K. Hubbard */ 13555e235068SJordan K. Hubbard fdc->state = STARTRECAL; 13565b81b6b3SRodney W. Grimes return(1); /* will return immediatly */ 13575b81b6b3SRodney W. Grimes default: 13585b81b6b3SRodney W. Grimes printf("Unexpected FD int->"); 1359dc8603e3SJoerg Wunsch if(out_fdc(fdcu, NE7CMD_SENSEI) < 0) 1360dc8603e3SJoerg Wunsch { 1361dc8603e3SJoerg Wunsch printf("[controller is dead now]\n"); 1362dc8603e3SJoerg Wunsch return(0); 1363dc8603e3SJoerg Wunsch } 13645b81b6b3SRodney W. Grimes st0 = in_fdc(fdcu); 13655b81b6b3SRodney W. Grimes cyl = in_fdc(fdcu); 13663a2f7427SDavid Greenman printf("ST0 = %x, PCN = %x\n", st0, cyl); 13673a2f7427SDavid Greenman out_fdc(fdcu, NE7CMD_READID); 13685b81b6b3SRodney W. Grimes out_fdc(fdcu, fd->fdsu); 13695b81b6b3SRodney W. Grimes for(i=0;i<7;i++) { 13705b81b6b3SRodney W. Grimes fdc->status[i] = in_fdc(fdcu); 13715b81b6b3SRodney W. Grimes } 13723a2f7427SDavid Greenman if(fdc->status[0] != -1) 13733a2f7427SDavid Greenman printf("intr status :%lx %lx %lx %lx %lx %lx %lx\n", 13745b81b6b3SRodney W. Grimes fdc->status[0], 13755b81b6b3SRodney W. Grimes fdc->status[1], 13765b81b6b3SRodney W. Grimes fdc->status[2], 13775b81b6b3SRodney W. Grimes fdc->status[3], 13785b81b6b3SRodney W. Grimes fdc->status[4], 13795b81b6b3SRodney W. Grimes fdc->status[5], 13805b81b6b3SRodney W. Grimes fdc->status[6] ); 13813a2f7427SDavid Greenman else 13823a2f7427SDavid Greenman printf("FDC timed out\n"); 13835b81b6b3SRodney W. Grimes return(0); 13845b81b6b3SRodney W. Grimes } 13855b81b6b3SRodney W. Grimes return(1); /* Come back immediatly to new state */ 13865b81b6b3SRodney W. Grimes } 13875b81b6b3SRodney W. Grimes 1388aaf08d94SGarrett Wollman static int 1389f5f7ba03SJordan K. Hubbard retrier(fdcu) 1390f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 13915b81b6b3SRodney W. Grimes { 13925b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 13935b81b6b3SRodney W. Grimes register struct buf *dp, *bp; 13945b81b6b3SRodney W. Grimes 13955b81b6b3SRodney W. Grimes dp = &(fdc->head); 13965b81b6b3SRodney W. Grimes bp = dp->b_actf; 13975b81b6b3SRodney W. Grimes 13983a2f7427SDavid Greenman if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) 13993a2f7427SDavid Greenman goto fail; 14005b81b6b3SRodney W. Grimes switch(fdc->retry) 14015b81b6b3SRodney W. Grimes { 14025b81b6b3SRodney W. Grimes case 0: case 1: case 2: 14035b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 14045b81b6b3SRodney W. Grimes break; 14055b81b6b3SRodney W. Grimes case 3: case 4: case 5: 14065b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 14075b81b6b3SRodney W. Grimes break; 14085b81b6b3SRodney W. Grimes case 6: 14095b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 14105b81b6b3SRodney W. Grimes break; 14115b81b6b3SRodney W. Grimes case 7: 14125b81b6b3SRodney W. Grimes break; 14135b81b6b3SRodney W. Grimes default: 14143a2f7427SDavid Greenman fail: 14155b81b6b3SRodney W. Grimes { 14167ca0641bSAndrey A. Chernov dev_t sav_b_dev = bp->b_dev; 14177ca0641bSAndrey A. Chernov /* Trick diskerr */ 14183a2f7427SDavid Greenman bp->b_dev = makedev(major(bp->b_dev), 14193a2f7427SDavid Greenman (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); 142092ed385aSRodney W. Grimes diskerr(bp, "fd", "hard error", LOG_PRINTF, 14213a2f7427SDavid Greenman fdc->fd->skip / DEV_BSIZE, 14223a2f7427SDavid Greenman (struct disklabel *)NULL); 14237ca0641bSAndrey A. Chernov bp->b_dev = sav_b_dev; 142492ed385aSRodney W. Grimes printf(" (ST0 %b ", fdc->status[0], NE7_ST0BITS); 14255b81b6b3SRodney W. Grimes printf(" ST1 %b ", fdc->status[1], NE7_ST1BITS); 14265b81b6b3SRodney W. Grimes printf(" ST2 %b ", fdc->status[2], NE7_ST2BITS); 1427702c623aSPoul-Henning Kamp printf("cyl %ld hd %ld sec %ld)\n", 142892ed385aSRodney W. Grimes fdc->status[3], fdc->status[4], fdc->status[5]); 14295b81b6b3SRodney W. Grimes } 14305b81b6b3SRodney W. Grimes bp->b_flags |= B_ERROR; 14315b81b6b3SRodney W. Grimes bp->b_error = EIO; 14325b81b6b3SRodney W. Grimes bp->b_resid = bp->b_bcount - fdc->fd->skip; 143326f9a767SRodney W. Grimes dp->b_actf = bp->b_actf; 14345b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 14355b81b6b3SRodney W. Grimes biodone(bp); 143692ed385aSRodney W. Grimes fdc->state = FINDWORK; 14375b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 14385b81b6b3SRodney W. Grimes fdc->fdu = -1; 1439f5f7ba03SJordan K. Hubbard /* XXX abort current command, if any. */ 144092ed385aSRodney W. Grimes return(1); 14415b81b6b3SRodney W. Grimes } 14425b81b6b3SRodney W. Grimes fdc->retry++; 14435b81b6b3SRodney W. Grimes return(1); 14445b81b6b3SRodney W. Grimes } 14455b81b6b3SRodney W. Grimes 1446b39c878eSAndrey A. Chernov static int 1447b39c878eSAndrey A. Chernov fdformat(dev, finfo, p) 1448b39c878eSAndrey A. Chernov dev_t dev; 1449b39c878eSAndrey A. Chernov struct fd_formb *finfo; 1450b39c878eSAndrey A. Chernov struct proc *p; 1451b39c878eSAndrey A. Chernov { 1452b39c878eSAndrey A. Chernov fdu_t fdu; 1453b39c878eSAndrey A. Chernov fd_p fd; 1454b39c878eSAndrey A. Chernov 1455b39c878eSAndrey A. Chernov struct buf *bp; 1456b39c878eSAndrey A. Chernov int rv = 0, s; 14573a2f7427SDavid Greenman size_t fdblk; 1458b39c878eSAndrey A. Chernov 1459b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 1460b39c878eSAndrey A. Chernov fd = &fd_data[fdu]; 14613a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1462b39c878eSAndrey A. Chernov 1463b39c878eSAndrey A. Chernov /* set up a buffer header for fdstrategy() */ 1464b39c878eSAndrey A. Chernov bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 1465b39c878eSAndrey A. Chernov if(bp == 0) 1466b39c878eSAndrey A. Chernov return ENOBUFS; 1467b39c878eSAndrey A. Chernov bzero((void *)bp, sizeof(struct buf)); 1468b39c878eSAndrey A. Chernov bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; 1469b39c878eSAndrey A. Chernov bp->b_proc = p; 1470b39c878eSAndrey A. Chernov bp->b_dev = dev; 1471b39c878eSAndrey A. Chernov 1472b39c878eSAndrey A. Chernov /* 1473b39c878eSAndrey A. Chernov * calculate a fake blkno, so fdstrategy() would initiate a 1474b39c878eSAndrey A. Chernov * seek to the requested cylinder 1475b39c878eSAndrey A. Chernov */ 1476b39c878eSAndrey A. Chernov bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 14773a2f7427SDavid Greenman + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 1478b39c878eSAndrey A. Chernov 1479b39c878eSAndrey A. Chernov bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 14805e235068SJordan K. Hubbard bp->b_un.b_addr = (caddr_t)finfo; 1481b39c878eSAndrey A. Chernov 1482b39c878eSAndrey A. Chernov /* now do the format */ 1483b39c878eSAndrey A. Chernov fdstrategy(bp); 1484b39c878eSAndrey A. Chernov 1485b39c878eSAndrey A. Chernov /* ...and wait for it to complete */ 1486b39c878eSAndrey A. Chernov s = splbio(); 1487b39c878eSAndrey A. Chernov while(!(bp->b_flags & B_DONE)) 1488b39c878eSAndrey A. Chernov { 14895e235068SJordan K. Hubbard rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 1490b39c878eSAndrey A. Chernov if(rv == EWOULDBLOCK) 1491b39c878eSAndrey A. Chernov break; 1492b39c878eSAndrey A. Chernov } 1493b39c878eSAndrey A. Chernov splx(s); 1494b39c878eSAndrey A. Chernov 1495b39c878eSAndrey A. Chernov if(rv == EWOULDBLOCK) 1496b39c878eSAndrey A. Chernov /* timed out */ 1497b39c878eSAndrey A. Chernov rv = EIO; 14983a2f7427SDavid Greenman if(bp->b_flags & B_ERROR) 14993a2f7427SDavid Greenman rv = bp->b_error; 15003a2f7427SDavid Greenman biodone(bp); 1501b39c878eSAndrey A. Chernov free(bp, M_TEMP); 1502b39c878eSAndrey A. Chernov return rv; 1503b39c878eSAndrey A. Chernov } 1504b39c878eSAndrey A. Chernov 1505f5f7ba03SJordan K. Hubbard /* 1506f5f7ba03SJordan K. Hubbard * 15073a2f7427SDavid Greenman * TODO: Think about allocating buffer off stack. 1508f5f7ba03SJordan K. Hubbard * Don't pass uncast 0's and NULL's to read/write/setdisklabel(). 1509f5f7ba03SJordan K. Hubbard * Watch out for NetBSD's different *disklabel() interface. 1510b39c878eSAndrey A. Chernov * 1511f5f7ba03SJordan K. Hubbard */ 15125b81b6b3SRodney W. Grimes 1513f5f7ba03SJordan K. Hubbard int 1514b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p) 1515f5f7ba03SJordan K. Hubbard dev_t dev; 1516f5f7ba03SJordan K. Hubbard int cmd; 1517f5f7ba03SJordan K. Hubbard caddr_t addr; 1518f5f7ba03SJordan K. Hubbard int flag; 1519b39c878eSAndrey A. Chernov struct proc *p; 1520f5f7ba03SJordan K. Hubbard { 15213a2f7427SDavid Greenman fdu_t fdu = FDUNIT(minor(dev)); 15223a2f7427SDavid Greenman fd_p fd = &fd_data[fdu]; 15233a2f7427SDavid Greenman size_t fdblk; 15243a2f7427SDavid Greenman 1525f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 1526f5f7ba03SJordan K. Hubbard struct disklabel *dl; 1527f5f7ba03SJordan K. Hubbard char buffer[DEV_BSIZE]; 15283a2f7427SDavid Greenman int error = 0; 1529f5f7ba03SJordan K. Hubbard 1530b99f0a4aSAndrew Moore #if NFT > 0 1531a60eff27SNate Williams int type = FDTYPE(minor(dev)); 1532a60eff27SNate Williams 1533a60eff27SNate Williams /* check for a tape ioctl */ 1534a60eff27SNate Williams if (type & F_TAPE_TYPE) 1535b99f0a4aSAndrew Moore return ftioctl(dev, cmd, addr, flag, p); 1536b99f0a4aSAndrew Moore #endif 1537b99f0a4aSAndrew Moore 15383a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1539f5f7ba03SJordan K. Hubbard 1540f5f7ba03SJordan K. Hubbard switch (cmd) 1541f5f7ba03SJordan K. Hubbard { 1542f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 1543f5f7ba03SJordan K. Hubbard bzero(buffer, sizeof (buffer)); 1544f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 15453a2f7427SDavid Greenman dl->d_secsize = fdblk; 154692ed385aSRodney W. Grimes fdt = fd_data[FDUNIT(minor(dev))].ft; 1547f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 1548f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 1549f5f7ba03SJordan K. Hubbard 155054c7241bSJordan K. Hubbard if (readdisklabel(dev, fdstrategy, dl, NULL, 0) == NULL) 1551f5f7ba03SJordan K. Hubbard error = 0; 1552f5f7ba03SJordan K. Hubbard else 1553f5f7ba03SJordan K. Hubbard error = EINVAL; 1554f5f7ba03SJordan K. Hubbard 1555f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 1556f5f7ba03SJordan K. Hubbard break; 1557f5f7ba03SJordan K. Hubbard 1558f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 1559f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1560f5f7ba03SJordan K. Hubbard error = EBADF; 1561f5f7ba03SJordan K. Hubbard break; 1562f5f7ba03SJordan K. Hubbard 1563f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 1564f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1565f5f7ba03SJordan K. Hubbard error = EBADF; 1566f5f7ba03SJordan K. Hubbard break; 1567f5f7ba03SJordan K. Hubbard 1568f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 1569f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1570f5f7ba03SJordan K. Hubbard { 1571f5f7ba03SJordan K. Hubbard error = EBADF; 1572f5f7ba03SJordan K. Hubbard break; 1573f5f7ba03SJordan K. Hubbard } 1574f5f7ba03SJordan K. Hubbard 1575f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 1576f5f7ba03SJordan K. Hubbard 15773a2f7427SDavid Greenman if ((error = 157854c7241bSJordan K. Hubbard setdisklabel ((struct disklabel *)buffer, dl, 0))) 1579f5f7ba03SJordan K. Hubbard break; 1580f5f7ba03SJordan K. Hubbard 1581b39c878eSAndrey A. Chernov error = writedisklabel(dev, fdstrategy, 158254c7241bSJordan K. Hubbard (struct disklabel *)buffer); 1583b39c878eSAndrey A. Chernov break; 1584b39c878eSAndrey A. Chernov 1585b39c878eSAndrey A. Chernov case FD_FORM: 1586b39c878eSAndrey A. Chernov if((flag & FWRITE) == 0) 1587b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 1588b39c878eSAndrey A. Chernov else if(((struct fd_formb *)addr)->format_version != 1589b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 1590b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 1591b39c878eSAndrey A. Chernov else 1592b39c878eSAndrey A. Chernov error = fdformat(dev, (struct fd_formb *)addr, p); 1593b39c878eSAndrey A. Chernov break; 1594b39c878eSAndrey A. Chernov 1595b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 1596b39c878eSAndrey A. Chernov *(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft; 1597f5f7ba03SJordan K. Hubbard break; 1598f5f7ba03SJordan K. Hubbard 15993a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 16003a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 16013a2f7427SDavid Greenman if(suser(p->p_ucred, &p->p_acflag) != 0) 16023a2f7427SDavid Greenman return EPERM; 16033a2f7427SDavid Greenman *fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr; 16043a2f7427SDavid Greenman break; 16053a2f7427SDavid Greenman 16063a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 16073a2f7427SDavid Greenman *(int *)addr = fd_data[FDUNIT(minor(dev))].options; 16083a2f7427SDavid Greenman break; 16093a2f7427SDavid Greenman 16103a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 16113a2f7427SDavid Greenman fd_data[FDUNIT(minor(dev))].options = *(int *)addr; 16123a2f7427SDavid Greenman break; 16133a2f7427SDavid Greenman 1614f5f7ba03SJordan K. Hubbard default: 16153a2f7427SDavid Greenman error = ENOTTY; 1616f5f7ba03SJordan K. Hubbard break; 1617f5f7ba03SJordan K. Hubbard } 1618f5f7ba03SJordan K. Hubbard return (error); 1619f5f7ba03SJordan K. Hubbard } 1620f5f7ba03SJordan K. Hubbard 1621f5f7ba03SJordan K. Hubbard #endif 16223a2f7427SDavid Greenman /* 16233a2f7427SDavid Greenman * Hello emacs, these are the 16243a2f7427SDavid Greenman * Local Variables: 16253a2f7427SDavid Greenman * c-indent-level: 8 16263a2f7427SDavid Greenman * c-continued-statement-offset: 8 16273a2f7427SDavid Greenman * c-continued-brace-offset: 0 16283a2f7427SDavid Greenman * c-brace-offset: -8 16293a2f7427SDavid Greenman * c-brace-imaginary-offset: 0 16303a2f7427SDavid Greenman * c-argdecl-indent: 8 16313a2f7427SDavid Greenman * c-label-offset: -8 16323a2f7427SDavid Greenman * c++-hanging-braces: 1 16333a2f7427SDavid Greenman * c++-access-specifier-offset: -8 16343a2f7427SDavid Greenman * c++-empty-arglist-indent: 8 16353a2f7427SDavid Greenman * c++-friend-offset: 0 16363a2f7427SDavid Greenman * End: 16373a2f7427SDavid Greenman */ 1638