Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.10
      1  1.10       pk /*	$NetBSD: fd.c,v 1.10 1995/08/18 10:30:16 pk Exp $	*/
      2   1.1       pk 
      3   1.1       pk /*-
      4   1.1       pk  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5   1.1       pk  * Copyright (c) 1995 Paul Kranenburg.
      6   1.1       pk  * Copyright (c) 1990 The Regents of the University of California.
      7   1.1       pk  * All rights reserved.
      8   1.1       pk  *
      9   1.1       pk  * This code is derived from software contributed to Berkeley by
     10   1.1       pk  * Don Ahn.
     11   1.1       pk  *
     12   1.1       pk  * Redistribution and use in source and binary forms, with or without
     13   1.1       pk  * modification, are permitted provided that the following conditions
     14   1.1       pk  * are met:
     15   1.1       pk  * 1. Redistributions of source code must retain the above copyright
     16   1.1       pk  *    notice, this list of conditions and the following disclaimer.
     17   1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     19   1.1       pk  *    documentation and/or other materials provided with the distribution.
     20   1.1       pk  * 3. All advertising materials mentioning features or use of this software
     21   1.1       pk  *    must display the following acknowledgement:
     22   1.1       pk  *	This product includes software developed by the University of
     23   1.1       pk  *	California, Berkeley and its contributors.
     24   1.1       pk  * 4. Neither the name of the University nor the names of its contributors
     25   1.1       pk  *    may be used to endorse or promote products derived from this software
     26   1.1       pk  *    without specific prior written permission.
     27   1.1       pk  *
     28   1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29   1.1       pk  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30   1.1       pk  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31   1.1       pk  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32   1.1       pk  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33   1.1       pk  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34   1.1       pk  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35   1.1       pk  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36   1.1       pk  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37   1.1       pk  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38   1.1       pk  * SUCH DAMAGE.
     39   1.1       pk  *
     40   1.1       pk  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     41   1.1       pk  */
     42   1.1       pk 
     43   1.1       pk #include <sys/param.h>
     44   1.1       pk #include <sys/systm.h>
     45   1.1       pk #include <sys/kernel.h>
     46   1.1       pk #include <sys/conf.h>
     47   1.1       pk #include <sys/file.h>
     48   1.1       pk #include <sys/ioctl.h>
     49   1.1       pk #include <sys/device.h>
     50   1.1       pk #include <sys/disklabel.h>
     51   1.1       pk #include <sys/dkstat.h>
     52   1.1       pk #include <sys/disk.h>
     53   1.1       pk #include <sys/buf.h>
     54   1.1       pk #include <sys/uio.h>
     55   1.1       pk #include <sys/syslog.h>
     56   1.1       pk #include <sys/queue.h>
     57   1.1       pk 
     58   1.1       pk #include <machine/cpu.h>
     59   1.1       pk #include <machine/autoconf.h>
     60   1.2       pk #include <sparc/sparc/auxreg.h>
     61   1.1       pk #include <sparc/dev/fdreg.h>
     62   1.2       pk #include <sparc/dev/fdvar.h>
     63   1.1       pk 
     64   1.1       pk #define FDUNIT(dev)	(minor(dev) / 8)
     65   1.1       pk #define FDTYPE(dev)	(minor(dev) % 8)
     66   1.1       pk 
     67   1.1       pk #define b_cylin b_resid
     68   1.1       pk 
     69   1.2       pk #define FD_DEBUG
     70   1.2       pk #ifdef FD_DEBUG
     71   1.2       pk int	fdc_debug = 0;
     72   1.2       pk #endif
     73   1.2       pk 
     74   1.1       pk enum fdc_state {
     75   1.1       pk 	DEVIDLE = 0,
     76   1.1       pk 	MOTORWAIT,
     77   1.1       pk 	DOSEEK,
     78   1.1       pk 	SEEKWAIT,
     79   1.1       pk 	SEEKTIMEDOUT,
     80   1.1       pk 	SEEKCOMPLETE,
     81   1.1       pk 	DOIO,
     82   1.2       pk 	IOCOMPLETE,
     83   1.1       pk 	IOTIMEDOUT,
     84   1.1       pk 	DORESET,
     85   1.1       pk 	RESETCOMPLETE,
     86   1.1       pk 	RESETTIMEDOUT,
     87   1.1       pk 	DORECAL,
     88   1.1       pk 	RECALWAIT,
     89   1.1       pk 	RECALTIMEDOUT,
     90   1.1       pk 	RECALCOMPLETE,
     91   1.1       pk };
     92   1.1       pk 
     93   1.1       pk /* software state, per controller */
     94   1.1       pk struct fdc_softc {
     95   1.1       pk 	struct dkdevice sc_dk;		/* boilerplate */
     96   1.1       pk 	struct intrhand sc_sih;
     97   1.1       pk 	struct intrhand sc_hih;
     98   1.1       pk 	caddr_t		sc_reg;
     99   1.1       pk 	struct fd_softc *sc_fd[4];	/* pointers to children */
    100   1.1       pk 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    101   1.1       pk 	enum fdc_state	sc_state;
    102   1.2       pk 	int		sc_flags;
    103   1.2       pk #define FDC_82077		0x01
    104   1.2       pk #define FDC_NEEDHEADSETTLE	0x02
    105   1.2       pk #define FDC_EIS			0x04
    106   1.2       pk 	int		sc_errors;		/* number of retries so far */
    107   1.6       pk 	int		sc_overruns;		/* number of DMA overruns */
    108   1.2       pk 	int		sc_cfg;			/* current configuration */
    109   1.2       pk 	struct fdcio	sc_io;
    110   1.2       pk #define sc_reg_msr	sc_io.fdcio_reg_msr
    111   1.2       pk #define sc_reg_fifo	sc_io.fdcio_reg_fifo
    112   1.2       pk #define sc_reg_dor	sc_io.fdcio_reg_dor
    113   1.2       pk #define sc_reg_drs	sc_io.fdcio_reg_msr
    114   1.2       pk #define sc_istate	sc_io.fdcio_istate
    115   1.2       pk #define sc_data		sc_io.fdcio_data
    116   1.2       pk #define sc_tc		sc_io.fdcio_tc
    117   1.2       pk #define sc_nstat	sc_io.fdcio_nstat
    118   1.2       pk #define sc_status	sc_io.fdcio_status
    119   1.3       pk #define sc_intrcnt	sc_io.fdcio_intrcnt
    120   1.1       pk };
    121   1.1       pk 
    122   1.2       pk #ifndef FDC_C_HANDLER
    123   1.2       pk extern	struct fdcio	*fdciop;
    124   1.2       pk #endif
    125   1.2       pk 
    126   1.1       pk /* controller driver configuration */
    127   1.1       pk int	fdcmatch __P((struct device *, void *, void *));
    128   1.1       pk void	fdcattach __P((struct device *, struct device *, void *));
    129   1.1       pk 
    130   1.1       pk struct cfdriver fdccd = {
    131   1.1       pk 	NULL, "fdc", fdcmatch, fdcattach, DV_DULL, sizeof(struct fdc_softc)
    132   1.1       pk };
    133   1.1       pk 
    134   1.1       pk /*
    135   1.1       pk  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    136   1.1       pk  * we tell them apart.
    137   1.1       pk  */
    138   1.1       pk struct fd_type {
    139   1.1       pk 	int	sectrac;	/* sectors per track */
    140   1.1       pk 	int	heads;		/* number of heads */
    141   1.1       pk 	int	seccyl;		/* sectors per cylinder */
    142   1.1       pk 	int	secsize;	/* size code for sectors */
    143   1.1       pk 	int	datalen;	/* data len when secsize = 0 */
    144   1.1       pk 	int	steprate;	/* step rate and head unload time */
    145   1.1       pk 	int	gap1;		/* gap len between sectors */
    146   1.1       pk 	int	gap2;		/* formatting gap */
    147   1.1       pk 	int	tracks;		/* total num of tracks */
    148   1.1       pk 	int	size;		/* size of disk in sectors */
    149   1.1       pk 	int	step;		/* steps per cylinder */
    150   1.1       pk 	int	rate;		/* transfer speed code */
    151   1.1       pk 	char	*name;
    152   1.1       pk };
    153   1.1       pk 
    154   1.1       pk /* The order of entries in the following table is important -- BEWARE! */
    155   1.1       pk struct fd_type fd_types[] = {
    156   1.1       pk 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    157   1.1       pk 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB"    }, /* 1.2 MB AT-diskettes */
    158   1.1       pk 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */
    159   1.1       pk 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */
    160   1.1       pk 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB"    }, /* 3.5" 720kB diskette */
    161   1.1       pk 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x"  }, /* 720kB in 1.2MB drive */
    162   1.1       pk 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x"  }, /* 360kB in 720kB drive */
    163   1.1       pk };
    164   1.1       pk 
    165   1.1       pk /* software state, per disk (with up to 4 disks per ctlr) */
    166   1.1       pk struct fd_softc {
    167   1.1       pk 	struct dkdevice sc_dk;
    168   1.1       pk 
    169   1.1       pk 	struct fd_type *sc_deftype;	/* default type descriptor */
    170   1.1       pk 	struct fd_type *sc_type;	/* current type descriptor */
    171   1.1       pk 
    172   1.1       pk 	daddr_t	sc_blkno;	/* starting block number */
    173   1.1       pk 	int sc_bcount;		/* byte count left */
    174   1.1       pk 	int sc_skip;		/* bytes already transferred */
    175   1.1       pk 	int sc_nblks;		/* number of blocks currently tranferring */
    176   1.1       pk 	int sc_nbytes;		/* number of bytes currently tranferring */
    177   1.1       pk 
    178   1.1       pk 	int sc_drive;		/* physical unit number */
    179   1.1       pk 	int sc_flags;
    180   1.1       pk #define	FD_OPEN		0x01		/* it's open */
    181   1.1       pk #define	FD_MOTOR	0x02		/* motor should be on */
    182   1.1       pk #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    183   1.1       pk 	int sc_cylin;		/* where we think the head is */
    184   1.1       pk 
    185   1.1       pk 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    186   1.1       pk 	int sc_ops;		/* I/O ops since last switch */
    187   1.1       pk 	struct buf sc_q;	/* head of buf chain */
    188   1.1       pk };
    189   1.1       pk 
    190   1.1       pk /* floppy driver configuration */
    191   1.1       pk int	fdmatch __P((struct device *, void *, void *));
    192   1.1       pk void	fdattach __P((struct device *, struct device *, void *));
    193   1.1       pk 
    194   1.1       pk struct cfdriver fdcd = {
    195   1.1       pk 	NULL, "fd", fdmatch, fdattach, DV_DISK, sizeof(struct fd_softc)
    196   1.1       pk };
    197   1.1       pk 
    198   1.1       pk void fdgetdisklabel __P((struct fd_softc *));
    199   1.1       pk int fd_get_parms __P((struct fd_softc *));
    200   1.1       pk void fdstrategy __P((struct buf *));
    201   1.1       pk void fdstart __P((struct fd_softc *));
    202   1.1       pk 
    203   1.1       pk struct dkdriver fddkdriver = { fdstrategy };
    204   1.1       pk 
    205   1.1       pk struct	fd_type *fd_nvtotype __P((char *, int, int));
    206   1.1       pk void	fd_set_motor __P((struct fdc_softc *fdc, int reset));
    207   1.1       pk void	fd_motor_off __P((void *arg));
    208   1.1       pk void	fd_motor_on __P((void *arg));
    209   1.1       pk int	fdcresult __P((struct fdc_softc *fdc));
    210   1.1       pk int	out_fdc __P((struct fdc_softc *fdc, u_char x));
    211   1.1       pk void	fdcstart __P((struct fdc_softc *fdc));
    212   1.1       pk void	fdcstatus __P((struct device *dv, int n, char *s));
    213   1.1       pk void	fdctimeout __P((void *arg));
    214   1.1       pk void	fdcpseudointr __P((void *arg));
    215   1.2       pk #ifdef FDC_C_HANDLER
    216   1.1       pk int	fdchwintr __P((struct fdc_softc *));
    217   1.2       pk #else
    218   1.2       pk void	fdchwintr __P((void));
    219   1.2       pk #endif
    220   1.1       pk int	fdcswintr __P((struct fdc_softc *));
    221   1.1       pk void	fdcretry __P((struct fdc_softc *fdc));
    222   1.1       pk void	fdfinish __P((struct fd_softc *fd, struct buf *bp));
    223   1.1       pk 
    224   1.2       pk #if PIL_FDSOFT == 4
    225   1.1       pk #define IE_FDSOFT	IE_L4
    226   1.2       pk #else
    227   1.2       pk #error 4
    228   1.2       pk #endif
    229   1.1       pk 
    230  1.10       pk #define OBP_FDNAME	(cputyp == CPU_SUN4M ? "SUNW,fdtwo" : "fd")
    231  1.10       pk 
    232   1.1       pk int
    233   1.1       pk fdcmatch(parent, match, aux)
    234   1.1       pk 	struct device *parent;
    235   1.1       pk 	void *match, *aux;
    236   1.1       pk {
    237   1.1       pk 	struct cfdata *cf = match;
    238   1.1       pk 	register struct confargs *ca = aux;
    239   1.1       pk 	register struct romaux *ra = &ca->ca_ra;
    240   1.1       pk 
    241  1.10       pk 	/* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */
    242  1.10       pk 	if (strcmp(OBP_FDNAME, ra->ra_name))
    243   1.1       pk 		return (0);
    244   1.5       pk 	if (ca->ca_bustype == BUS_MAIN) {
    245   1.5       pk 		if (ca->ca_ra.ra_vaddr &&
    246   1.5       pk 		    probeget(ca->ca_ra.ra_vaddr, 1) == -1) {
    247   1.5       pk 			return (0);
    248   1.5       pk 		}
    249   1.1       pk 		return (1);
    250   1.5       pk 	}
    251   1.1       pk 
    252   1.1       pk 	return (0);
    253   1.1       pk }
    254   1.1       pk 
    255   1.1       pk /*
    256   1.1       pk  * Arguments passed between fdcattach and fdprobe.
    257   1.1       pk  */
    258   1.1       pk struct fdc_attach_args {
    259   1.1       pk 	int fa_drive;
    260   1.1       pk 	struct fd_type *fa_deftype;
    261   1.1       pk };
    262   1.1       pk 
    263   1.1       pk /*
    264   1.1       pk  * Print the location of a disk drive (called just before attaching the
    265   1.1       pk  * the drive).  If `fdc' is not NULL, the drive was found but was not
    266   1.1       pk  * in the system config file; print the drive name as well.
    267   1.1       pk  * Return QUIET (config_find ignores this if the device was configured) to
    268   1.1       pk  * avoid printing `fdN not configured' messages.
    269   1.1       pk  */
    270   1.1       pk int
    271   1.1       pk fdprint(aux, fdc)
    272   1.1       pk 	void *aux;
    273   1.1       pk 	char *fdc;
    274   1.1       pk {
    275   1.1       pk 	register struct fdc_attach_args *fa = aux;
    276   1.1       pk 
    277   1.1       pk 	if (!fdc)
    278   1.1       pk 		printf(" drive %d", fa->fa_drive);
    279   1.1       pk 	return QUIET;
    280   1.1       pk }
    281   1.1       pk 
    282   1.1       pk static void
    283   1.1       pk fdconf(fdc)
    284   1.1       pk 	struct fdc_softc *fdc;
    285   1.1       pk {
    286   1.1       pk 	int	vroom;
    287   1.1       pk 
    288   1.1       pk 	if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
    289   1.1       pk 		return;
    290   1.1       pk 
    291   1.1       pk 	/*
    292   1.1       pk 	 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
    293   1.1       pk 	 * the PROM thinks is appropriate.
    294   1.1       pk 	 */
    295   1.1       pk 	if ((vroom = fdc->sc_status[7]) == 0)
    296   1.1       pk 		vroom = 0x64;
    297   1.1       pk 
    298   1.1       pk 	/* Configure controller to use FIFO and Implied Seek */
    299   1.1       pk 	out_fdc(fdc, NE7CMD_CFG);
    300   1.1       pk 	out_fdc(fdc, vroom);
    301   1.2       pk 	out_fdc(fdc, fdc->sc_cfg);
    302   1.1       pk 	out_fdc(fdc, 0); /* PRETRK */
    303   1.1       pk 	/* No result phase */
    304   1.1       pk }
    305   1.1       pk 
    306   1.1       pk void
    307   1.1       pk fdcattach(parent, self, aux)
    308   1.1       pk 	struct device *parent, *self;
    309   1.1       pk 	void *aux;
    310   1.1       pk {
    311   1.1       pk 	register struct confargs *ca = aux;
    312   1.1       pk 	struct fdc_softc *fdc = (void *)self;
    313   1.1       pk 	struct fdc_attach_args fa;
    314   1.1       pk 	int n, pri;
    315   1.8       pk 	char code;
    316   1.1       pk 
    317   1.1       pk 	if (ca->ca_ra.ra_vaddr)
    318   1.1       pk 		fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr;
    319   1.1       pk 	else
    320   1.1       pk 		fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_paddr,
    321   1.1       pk 						ca->ca_ra.ra_len,
    322   1.1       pk 						ca->ca_bustype);
    323   1.1       pk 
    324   1.2       pk 	fdc->sc_state = DEVIDLE;
    325   1.2       pk 	fdc->sc_istate = ISTATE_IDLE;
    326   1.2       pk 	fdc->sc_flags |= FDC_EIS;
    327   1.2       pk 	TAILQ_INIT(&fdc->sc_drives);
    328   1.2       pk 
    329   1.1       pk 	pri = ca->ca_ra.ra_intr[0].int_pri;
    330   1.2       pk #ifdef FDC_C_HANDLER
    331   1.1       pk 	fdc->sc_hih.ih_fun = (void *)fdchwintr;
    332   1.1       pk 	fdc->sc_hih.ih_arg = fdc;
    333   1.1       pk 	intr_establish(pri, &fdc->sc_hih);
    334   1.2       pk #else
    335   1.2       pk 	fdciop = &fdc->sc_io;
    336   1.2       pk 	intr_fasttrap(pri, fdchwintr);
    337   1.2       pk #endif
    338   1.1       pk 	fdc->sc_sih.ih_fun = (void *)fdcswintr;
    339   1.1       pk 	fdc->sc_sih.ih_arg = fdc;
    340   1.2       pk 	intr_establish(PIL_FDSOFT, &fdc->sc_sih);
    341   1.1       pk 
    342   1.8       pk 	/* Assume a 82077 */
    343   1.8       pk 	fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr;
    344   1.8       pk 	fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo;
    345   1.8       pk 	fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor;
    346   1.8       pk 
    347   1.8       pk 	code = '7';
    348   1.8       pk 	if (*fdc->sc_reg_dor == NE7_RQM) {
    349   1.8       pk 		/*
    350   1.8       pk 		 * This hack from Chris Torek: apparently DOR really
    351   1.8       pk 		 * addresses MSR/DRS on a 82072.
    352   1.8       pk 		 * We used to rely on the VERSION command to tell the
    353   1.8       pk 		 * difference (which did not work).
    354   1.8       pk 		 */
    355   1.8       pk 		*fdc->sc_reg_dor = FDC_250KBPS;
    356   1.8       pk 		if (*fdc->sc_reg_dor == NE7_RQM)
    357   1.8       pk 			code = '2';
    358   1.5       pk 	}
    359   1.8       pk 	if (code == '7') {
    360   1.1       pk 		fdc->sc_flags |= FDC_82077;
    361   1.1       pk 	} else {
    362   1.8       pk 		fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr;
    363   1.8       pk 		fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo;
    364   1.8       pk 		fdc->sc_reg_dor = 0;
    365   1.1       pk 	}
    366   1.1       pk 
    367   1.8       pk #ifdef FD_DEBUG
    368   1.8       pk 	if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
    369   1.8       pk 	    fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
    370   1.8       pk 		if (fdc_debug)
    371   1.8       pk 			printf("[version cmd]");
    372   1.8       pk 	}
    373   1.8       pk #endif
    374   1.8       pk 
    375   1.2       pk 	/*
    376   1.2       pk 	 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
    377   1.6       pk 	 * Note: CFG_EFIFO is active-low, initial threshold value: 8
    378   1.2       pk 	 */
    379   1.6       pk 	fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
    380   1.1       pk 	fdconf(fdc);
    381   1.1       pk 
    382   1.1       pk 	if (fdc->sc_flags & FDC_82077) {
    383   1.1       pk 		/* Lock configuration across soft resets. */
    384   1.1       pk 		out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
    385   1.1       pk 		if (fdcresult(fdc) != 1)
    386   1.1       pk 			printf(" CFGLOCK: unexpected response");
    387   1.1       pk 	}
    388   1.3       pk 
    389   1.3       pk 	evcnt_attach(&fdc->sc_dk.dk_dev, "intr", &fdc->sc_intrcnt);
    390   1.1       pk 
    391   1.8       pk 	printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code);
    392   1.1       pk 
    393  1.10       pk 	/*
    394  1.10       pk 	 * Controller and drives are represented by one and the same
    395  1.10       pk 	 * Openprom node, so we can as well check for the floppy boots here.
    396  1.10       pk 	 */
    397  1.10       pk 	if (ca->ca_ra.ra_bp &&
    398  1.10       pk 	    strcmp(ca->ca_ra.ra_bp->name, OBP_FDNAME) == 0 &&
    399  1.10       pk 	    ca->ca_ra.ra_bp->val[0] == 0 &&
    400  1.10       pk 	    ca->ca_ra.ra_bp->val[1] == 0)
    401  1.10       pk 		bootdv = &fdc->sc_dk.dk_dev;
    402  1.10       pk 
    403   1.1       pk 	/* physical limit: four drives per controller. */
    404   1.1       pk 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    405   1.1       pk 		fa.fa_deftype = NULL;		/* unknown */
    406   1.1       pk 	fa.fa_deftype = &fd_types[0];		/* XXX */
    407   1.1       pk 		(void)config_found(self, (void *)&fa, fdprint);
    408   1.1       pk 	}
    409   1.1       pk }
    410   1.1       pk 
    411   1.1       pk int
    412   1.1       pk fdmatch(parent, match, aux)
    413   1.1       pk 	struct device *parent;
    414   1.1       pk 	void *match, *aux;
    415   1.1       pk {
    416   1.1       pk 	struct fdc_softc *fdc = (void *)parent;
    417   1.1       pk 	struct cfdata *cf = match;
    418   1.1       pk 	struct fdc_attach_args *fa = aux;
    419   1.1       pk 	int drive = fa->fa_drive;
    420   1.1       pk 	int n;
    421   1.1       pk 
    422   1.8       pk 	if (drive > 0)
    423   1.8       pk 		/* XXX - for now, punt > 1 drives */
    424   1.8       pk 		return 0;
    425   1.8       pk 
    426   1.1       pk 	if (fdc->sc_flags & FDC_82077) {
    427   1.1       pk 		/* select drive and turn on motor */
    428   1.1       pk 		*fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive);
    429   1.1       pk 		/* wait for motor to spin up */
    430   1.1       pk 		delay(250000);
    431   1.1       pk 	} else {
    432   1.1       pk 		auxregbisc(AUXIO_FDS, 0);
    433   1.1       pk 	}
    434   1.1       pk 	fdc->sc_nstat = 0;
    435   1.1       pk 	out_fdc(fdc, NE7CMD_RECAL);
    436   1.1       pk 	out_fdc(fdc, drive);
    437   1.1       pk 	/* wait for recalibrate */
    438   1.1       pk 	for (n = 0; n < 100000; n++) {
    439   1.1       pk 		delay(10);
    440   1.2       pk 		if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
    441   1.2       pk 			/* wait a bit longer till device *really* is ready */
    442   1.2       pk 			delay(100000);
    443   1.2       pk 			if (out_fdc(fdc, NE7CMD_SENSEI))
    444   1.2       pk 				break;
    445   1.7       pk 			if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
    446   1.2       pk 				/*
    447   1.2       pk 				 * Got `invalid command'; we interpret it
    448   1.2       pk 				 * to mean that the re-calibrate hasn't in
    449   1.2       pk 				 * fact finished yet
    450   1.2       pk 				 */
    451   1.2       pk 				continue;
    452   1.1       pk 			break;
    453   1.1       pk 		}
    454   1.1       pk 	}
    455   1.1       pk 	n = fdc->sc_nstat;
    456   1.1       pk #ifdef FD_DEBUG
    457   1.2       pk 	if (fdc_debug) {
    458   1.1       pk 		int i;
    459   1.2       pk 		printf("fdprobe: %d stati:", n);
    460   1.1       pk 		for (i = 0; i < n; i++)
    461   1.1       pk 			printf(" %x", fdc->sc_status[i]);
    462   1.1       pk 		printf("\n");
    463   1.1       pk 	}
    464   1.1       pk #endif
    465   1.1       pk 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    466   1.1       pk 		return 0;
    467   1.1       pk 	/* turn off motor */
    468   1.1       pk 	if (fdc->sc_flags & FDC_82077) {
    469   1.1       pk 		/* select drive and turn on motor */
    470   1.1       pk 		*fdc->sc_reg_dor = FDO_FRST;
    471   1.1       pk 	} else {
    472   1.1       pk 		auxregbisc(0, AUXIO_FDS);
    473   1.1       pk 	}
    474   1.1       pk 
    475   1.1       pk 	return 1;
    476   1.1       pk }
    477   1.1       pk 
    478   1.1       pk /*
    479   1.1       pk  * Controller is working, and drive responded.  Attach it.
    480   1.1       pk  */
    481   1.1       pk void
    482   1.1       pk fdattach(parent, self, aux)
    483   1.1       pk 	struct device *parent, *self;
    484   1.1       pk 	void *aux;
    485   1.1       pk {
    486   1.1       pk 	struct fdc_softc *fdc = (void *)parent;
    487   1.1       pk 	struct fd_softc *fd = (void *)self;
    488   1.1       pk 	struct fdc_attach_args *fa = aux;
    489   1.1       pk 	struct fd_type *type = fa->fa_deftype;
    490   1.1       pk 	int drive = fa->fa_drive;
    491   1.1       pk 
    492   1.1       pk 	/* XXX Allow `flags' to override device type? */
    493   1.1       pk 
    494   1.1       pk 	if (type)
    495   1.1       pk 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    496   1.1       pk 		    type->tracks, type->heads, type->sectrac);
    497   1.1       pk 	else
    498   1.1       pk 		printf(": density unknown\n");
    499   1.1       pk 
    500   1.1       pk 	fd->sc_cylin = -1;
    501   1.1       pk 	fd->sc_drive = drive;
    502   1.1       pk 	fd->sc_deftype = type;
    503   1.1       pk 	fdc->sc_fd[drive] = fd;
    504   1.1       pk 	fd->sc_dk.dk_driver = &fddkdriver;
    505  1.10       pk 
    506   1.1       pk 	/* XXX Need to do some more fiddling with sc_dk. */
    507   1.1       pk 	dk_establish(&fd->sc_dk, &fd->sc_dk.dk_dev);
    508   1.1       pk }
    509   1.1       pk 
    510   1.1       pk inline struct fd_type *
    511   1.1       pk fd_dev_to_type(fd, dev)
    512   1.1       pk 	struct fd_softc *fd;
    513   1.1       pk 	dev_t dev;
    514   1.1       pk {
    515   1.1       pk 	int type = FDTYPE(dev);
    516   1.1       pk 
    517   1.1       pk 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    518   1.1       pk 		return NULL;
    519   1.1       pk 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    520   1.1       pk }
    521   1.1       pk 
    522   1.1       pk void
    523   1.1       pk fdstrategy(bp)
    524   1.1       pk 	register struct buf *bp;	/* IO operation to perform */
    525   1.1       pk {
    526   1.1       pk 	struct fd_softc *fd;
    527   1.1       pk 	int unit = FDUNIT(bp->b_dev);
    528   1.1       pk 	int sz;
    529   1.1       pk  	int s;
    530   1.1       pk 
    531   1.1       pk 	/* Valid unit, controller, and request? */
    532   1.1       pk 	if (unit >= fdcd.cd_ndevs ||
    533   1.1       pk 	    (fd = fdcd.cd_devs[unit]) == 0 ||
    534   1.1       pk 	    bp->b_blkno < 0 ||
    535   1.1       pk 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    536   1.1       pk 		bp->b_error = EINVAL;
    537   1.1       pk 		goto bad;
    538   1.1       pk 	}
    539   1.1       pk 
    540   1.1       pk 	/* If it's a null transfer, return immediately. */
    541   1.1       pk 	if (bp->b_bcount == 0)
    542   1.1       pk 		goto done;
    543   1.1       pk 
    544   1.1       pk 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    545   1.1       pk 
    546   1.1       pk 	if (bp->b_blkno + sz > fd->sc_type->size) {
    547   1.1       pk 		sz = fd->sc_type->size - bp->b_blkno;
    548   1.1       pk 		if (sz == 0) {
    549   1.1       pk 			/* If exactly at end of disk, return EOF. */
    550   1.1       pk 			bp->b_resid = bp->b_bcount;
    551   1.1       pk 			goto done;
    552   1.1       pk 		}
    553   1.1       pk 		if (sz < 0) {
    554   1.1       pk 			/* If past end of disk, return EINVAL. */
    555   1.1       pk 			bp->b_error = EINVAL;
    556   1.1       pk 			goto bad;
    557   1.1       pk 		}
    558   1.1       pk 		/* Otherwise, truncate request. */
    559   1.1       pk 		bp->b_bcount = sz << DEV_BSHIFT;
    560   1.1       pk 	}
    561   1.1       pk 
    562   1.1       pk  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    563   1.1       pk 
    564   1.1       pk #ifdef FD_DEBUG
    565   1.2       pk 	if (fdc_debug > 1)
    566   1.2       pk 		printf("fdstrategy: b_blkno %d b_bcount %d blkno %d cylin %d\n",
    567   1.2       pk 		    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
    568   1.1       pk #endif
    569   1.1       pk 
    570   1.1       pk 	/* Queue transfer on drive, activate drive and controller if idle. */
    571   1.1       pk 	s = splbio();
    572   1.1       pk 	disksort(&fd->sc_q, bp);
    573   1.1       pk 	untimeout(fd_motor_off, fd); /* a good idea */
    574   1.1       pk 	if (!fd->sc_q.b_active)
    575   1.1       pk 		fdstart(fd);
    576   1.1       pk #ifdef DIAGNOSTIC
    577   1.1       pk 	else {
    578   1.1       pk 		struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    579   1.1       pk 		if (fdc->sc_state == DEVIDLE) {
    580   1.1       pk 			printf("fdstrategy: controller inactive\n");
    581   1.1       pk 			fdcstart(fdc);
    582   1.1       pk 		}
    583   1.1       pk 	}
    584   1.1       pk #endif
    585   1.1       pk 	splx(s);
    586   1.1       pk 	return;
    587   1.1       pk 
    588   1.1       pk bad:
    589   1.1       pk 	bp->b_flags |= B_ERROR;
    590   1.1       pk done:
    591   1.1       pk 	/* Toss transfer; we're done early. */
    592   1.1       pk 	biodone(bp);
    593   1.1       pk }
    594   1.1       pk 
    595   1.1       pk void
    596   1.1       pk fdstart(fd)
    597   1.1       pk 	struct fd_softc *fd;
    598   1.1       pk {
    599   1.1       pk 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    600   1.1       pk 	int active = fdc->sc_drives.tqh_first != 0;
    601   1.1       pk 
    602   1.1       pk 	/* Link into controller queue. */
    603   1.1       pk 	fd->sc_q.b_active = 1;
    604   1.1       pk 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    605   1.1       pk 
    606   1.1       pk 	/* If controller not already active, start it. */
    607   1.1       pk 	if (!active)
    608   1.1       pk 		fdcstart(fdc);
    609   1.1       pk }
    610   1.1       pk 
    611   1.1       pk void
    612   1.1       pk fdfinish(fd, bp)
    613   1.1       pk 	struct fd_softc *fd;
    614   1.1       pk 	struct buf *bp;
    615   1.1       pk {
    616   1.1       pk 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    617   1.1       pk 
    618   1.1       pk 	/*
    619   1.1       pk 	 * Move this drive to the end of the queue to give others a `fair'
    620   1.1       pk 	 * chance.  We only force a switch if N operations are completed while
    621   1.1       pk 	 * another drive is waiting to be serviced, since there is a long motor
    622   1.1       pk 	 * startup delay whenever we switch.
    623   1.1       pk 	 */
    624   1.1       pk 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    625   1.1       pk 		fd->sc_ops = 0;
    626   1.1       pk 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    627   1.1       pk 		if (bp->b_actf) {
    628   1.1       pk 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    629   1.1       pk 		} else
    630   1.1       pk 			fd->sc_q.b_active = 0;
    631   1.1       pk 	}
    632   1.1       pk 	bp->b_resid = fd->sc_bcount;
    633   1.1       pk 	fd->sc_skip = 0;
    634   1.1       pk 	fd->sc_q.b_actf = bp->b_actf;
    635   1.1       pk 	biodone(bp);
    636   1.1       pk 	/* turn off motor 5s from now */
    637   1.1       pk 	timeout(fd_motor_off, fd, 5 * hz);
    638   1.1       pk 	fdc->sc_state = DEVIDLE;
    639   1.1       pk }
    640   1.1       pk 
    641   1.1       pk void
    642   1.1       pk fd_set_motor(fdc, reset)
    643   1.1       pk 	struct fdc_softc *fdc;
    644   1.1       pk 	int reset;
    645   1.1       pk {
    646   1.1       pk 	struct fd_softc *fd;
    647   1.1       pk 	u_char status;
    648   1.1       pk 	int n;
    649   1.1       pk 
    650   1.1       pk 	if (fdc->sc_flags & FDC_82077) {
    651   1.1       pk 		if (fd = fdc->sc_drives.tqh_first)
    652   1.1       pk 			status = fd->sc_drive;
    653   1.1       pk 		else
    654   1.1       pk 			status = 0;
    655   1.1       pk 		if (!reset)
    656   1.1       pk 			status |= FDO_FRST | FDO_FDMAEN;
    657   1.1       pk 		for (n = 0; n < 4; n++)
    658   1.1       pk 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    659   1.1       pk 				status |= FDO_MOEN(n);
    660   1.1       pk 		*fdc->sc_reg_dor = status;
    661   1.1       pk 	} else {
    662   1.1       pk 		int on = 0;
    663   1.1       pk 
    664   1.1       pk 		for (n = 0; n < 4; n++)
    665   1.1       pk 			if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    666   1.1       pk 				on = 1;
    667   1.1       pk 		if (on) {
    668   1.1       pk 			auxregbisc(AUXIO_FDS, 0);
    669   1.1       pk 		} else {
    670   1.1       pk 			auxregbisc(0, AUXIO_FDS);
    671   1.1       pk 		}
    672   1.1       pk 		delay(10);
    673   1.1       pk 		if (reset) {
    674   1.1       pk 			*fdc->sc_reg_drs = DRS_RESET;
    675   1.1       pk 			delay(10);
    676   1.1       pk 			*fdc->sc_reg_drs = 0;
    677   1.1       pk #ifdef FD_DEBUG
    678   1.2       pk 			if (fdc_debug)
    679   1.2       pk 				printf("fdc reset\n");
    680   1.1       pk #endif
    681   1.1       pk 			fdconf(fdc);
    682   1.1       pk 		}
    683   1.1       pk 
    684   1.1       pk 	}
    685   1.1       pk }
    686   1.1       pk 
    687   1.1       pk void
    688   1.1       pk fd_motor_off(arg)
    689   1.1       pk 	void *arg;
    690   1.1       pk {
    691   1.1       pk 	struct fd_softc *fd = arg;
    692   1.1       pk 	int s;
    693   1.1       pk 
    694   1.1       pk 	s = splbio();
    695   1.1       pk 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    696   1.1       pk 	fd_set_motor((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent, 0);
    697   1.1       pk 	splx(s);
    698   1.1       pk }
    699   1.1       pk 
    700   1.1       pk void
    701   1.1       pk fd_motor_on(arg)
    702   1.1       pk 	void *arg;
    703   1.1       pk {
    704   1.1       pk 	struct fd_softc *fd = arg;
    705   1.1       pk 	struct fdc_softc *fdc = (void *)fd->sc_dk.dk_dev.dv_parent;
    706   1.1       pk 	int s;
    707   1.1       pk 
    708   1.1       pk 	s = splbio();
    709   1.1       pk 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    710   1.1       pk 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    711   1.1       pk 		(void) fdcswintr(fdc);
    712   1.1       pk 	splx(s);
    713   1.1       pk }
    714   1.1       pk 
    715   1.1       pk int
    716   1.1       pk fdcresult(fdc)
    717   1.1       pk 	struct fdc_softc *fdc;
    718   1.1       pk {
    719   1.1       pk 	u_char i;
    720   1.1       pk 	int j = 100000,
    721   1.1       pk 	    n = 0;
    722   1.1       pk 
    723   1.1       pk 	for (; j; j--) {
    724   1.1       pk 		i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB);
    725   1.1       pk 		if (i == NE7_RQM)
    726   1.1       pk 			return (fdc->sc_nstat = n);
    727   1.1       pk 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    728   1.1       pk 			if (n >= sizeof(fdc->sc_status)) {
    729   1.1       pk 				log(LOG_ERR, "fdcresult: overrun\n");
    730   1.1       pk 				return -1;
    731   1.1       pk 			}
    732   1.2       pk 			fdc->sc_status[n++] = *fdc->sc_reg_fifo;
    733   1.1       pk 		}
    734   1.1       pk 	}
    735   1.1       pk 	log(LOG_ERR, "fdcresult: timeout\n");
    736   1.1       pk 	return (fdc->sc_nstat = -1);
    737   1.1       pk }
    738   1.1       pk 
    739   1.1       pk int
    740   1.1       pk out_fdc(fdc, x)
    741   1.1       pk 	struct fdc_softc *fdc;
    742   1.1       pk 	u_char x;
    743   1.1       pk {
    744   1.1       pk 	int i = 100000;
    745   1.1       pk 
    746   1.2       pk 	while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0);
    747   1.1       pk 	if (i <= 0)
    748   1.1       pk 		return -1;
    749   1.1       pk 
    750   1.2       pk 	*fdc->sc_reg_fifo = x;
    751   1.1       pk 	return 0;
    752   1.1       pk }
    753   1.1       pk 
    754   1.1       pk int
    755   1.1       pk Fdopen(dev, flags)
    756   1.1       pk 	dev_t dev;
    757   1.1       pk 	int flags;
    758   1.1       pk {
    759   1.1       pk  	int unit;
    760   1.1       pk 	struct fd_softc *fd;
    761   1.1       pk 	struct fd_type *type;
    762   1.1       pk 
    763   1.1       pk 	unit = FDUNIT(dev);
    764   1.1       pk 	if (unit >= fdcd.cd_ndevs)
    765   1.1       pk 		return ENXIO;
    766   1.1       pk 	fd = fdcd.cd_devs[unit];
    767   1.1       pk 	if (fd == 0)
    768   1.1       pk 		return ENXIO;
    769   1.1       pk 	type = fd_dev_to_type(fd, dev);
    770   1.1       pk 	if (type == NULL)
    771   1.1       pk 		return ENXIO;
    772   1.1       pk 
    773   1.1       pk 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    774   1.1       pk 	    fd->sc_type != type)
    775   1.1       pk 		return EBUSY;
    776   1.1       pk 
    777   1.1       pk 	fd->sc_type = type;
    778   1.1       pk 	fd->sc_cylin = -1;
    779   1.1       pk 	fd->sc_flags |= FD_OPEN;
    780   1.1       pk 
    781   1.1       pk 	return 0;
    782   1.1       pk }
    783   1.1       pk 
    784   1.1       pk int
    785   1.4  mycroft fdclose(dev, flags)
    786   1.1       pk 	dev_t dev;
    787   1.1       pk 	int flags;
    788   1.1       pk {
    789   1.1       pk 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
    790   1.1       pk 
    791   1.1       pk 	fd->sc_flags &= ~FD_OPEN;
    792   1.1       pk 	return 0;
    793   1.9       pk }
    794   1.9       pk 
    795   1.9       pk int
    796   1.9       pk fdread(dev, uio)
    797   1.9       pk         dev_t dev;
    798   1.9       pk         struct uio *uio;
    799   1.9       pk {
    800   1.9       pk 
    801   1.9       pk         return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    802   1.9       pk }
    803   1.9       pk 
    804   1.9       pk int
    805   1.9       pk fdwrite(dev, uio)
    806   1.9       pk         dev_t dev;
    807   1.9       pk         struct uio *uio;
    808   1.9       pk {
    809   1.9       pk 
    810   1.9       pk         return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    811   1.1       pk }
    812   1.1       pk 
    813   1.1       pk void
    814   1.1       pk fdcstart(fdc)
    815   1.1       pk 	struct fdc_softc *fdc;
    816   1.1       pk {
    817   1.1       pk 
    818   1.1       pk #ifdef DIAGNOSTIC
    819   1.1       pk 	/* only got here if controller's drive queue was inactive; should
    820   1.1       pk 	   be in idle state */
    821   1.1       pk 	if (fdc->sc_state != DEVIDLE) {
    822   1.1       pk 		printf("fdcstart: not idle\n");
    823   1.1       pk 		return;
    824   1.1       pk 	}
    825   1.1       pk #endif
    826   1.1       pk 	(void) fdcswintr(fdc);
    827   1.1       pk }
    828   1.1       pk 
    829   1.1       pk void
    830   1.1       pk fdcstatus(dv, n, s)
    831   1.1       pk 	struct device *dv;
    832   1.1       pk 	int n;
    833   1.1       pk 	char *s;
    834   1.1       pk {
    835   1.1       pk 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    836   1.1       pk 
    837   1.1       pk #if 0
    838   1.1       pk 	/*
    839   1.1       pk 	 * A 82072 seems to return <invalid command> on
    840   1.1       pk 	 * gratuitous Sense Interrupt commands.
    841   1.1       pk 	 */
    842   1.1       pk 	if (n == 0 && (fdc->sc_flags & FDC_82077)) {
    843   1.1       pk 		out_fdc(fdc, NE7CMD_SENSEI);
    844   1.1       pk 		(void) fdcresult(fdc);
    845   1.1       pk 		n = 2;
    846   1.1       pk 	}
    847   1.1       pk #endif
    848   1.1       pk 
    849   1.1       pk 	/* Just print last status */
    850   1.1       pk 	n = fdc->sc_nstat;
    851   1.1       pk 
    852   1.1       pk 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    853   1.1       pk 
    854   1.1       pk 	switch (n) {
    855   1.1       pk 	case 0:
    856   1.1       pk 		printf("\n");
    857   1.1       pk 		break;
    858   1.1       pk 	case 2:
    859   1.1       pk 		printf(" (st0 %b cyl %d)\n",
    860   1.1       pk 		    fdc->sc_status[0], NE7_ST0BITS,
    861   1.1       pk 		    fdc->sc_status[1]);
    862   1.1       pk 		break;
    863   1.1       pk 	case 7:
    864   1.1       pk 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
    865   1.1       pk 		    fdc->sc_status[0], NE7_ST0BITS,
    866   1.1       pk 		    fdc->sc_status[1], NE7_ST1BITS,
    867   1.1       pk 		    fdc->sc_status[2], NE7_ST2BITS,
    868   1.1       pk 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    869   1.1       pk 		break;
    870   1.1       pk #ifdef DIAGNOSTIC
    871   1.1       pk 	default:
    872   1.2       pk 		printf(" fdcstatus: weird size: %d\n", n);
    873   1.1       pk 		break;
    874   1.1       pk #endif
    875   1.1       pk 	}
    876   1.1       pk }
    877   1.1       pk 
    878   1.1       pk void
    879   1.1       pk fdctimeout(arg)
    880   1.1       pk 	void *arg;
    881   1.1       pk {
    882   1.1       pk 	struct fdc_softc *fdc = arg;
    883   1.1       pk 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    884   1.1       pk 	int s;
    885   1.1       pk 
    886   1.1       pk 	s = splbio();
    887   1.1       pk 	fdcstatus(&fd->sc_dk.dk_dev, 0, "timeout");
    888   1.1       pk 
    889   1.1       pk 	if (fd->sc_q.b_actf)
    890   1.1       pk 		fdc->sc_state++;
    891   1.1       pk 	else
    892   1.1       pk 		fdc->sc_state = DEVIDLE;
    893   1.1       pk 
    894   1.1       pk 	(void) fdcswintr(fdc);
    895   1.1       pk 	splx(s);
    896   1.1       pk }
    897   1.1       pk 
    898   1.2       pk void
    899   1.2       pk fdcpseudointr(arg)
    900   1.2       pk 	void *arg;
    901   1.2       pk {
    902   1.2       pk 	struct fdc_softc *fdc = arg;
    903   1.2       pk 	int s;
    904   1.2       pk 
    905   1.2       pk 	/* Just ensure it has the right spl. */
    906   1.2       pk 	s = splbio();
    907   1.2       pk 	(void) fdcswintr(fdc);
    908   1.2       pk 	splx(s);
    909   1.2       pk }
    910   1.2       pk 
    911   1.2       pk 
    912   1.2       pk #ifdef FDC_C_HANDLER
    913   1.1       pk /*
    914   1.1       pk  * hardware interrupt entry point: must be converted to `fast'
    915   1.1       pk  * (in-window) handler.
    916   1.1       pk  */
    917   1.1       pk int
    918   1.1       pk fdchwintr(fdc)
    919   1.1       pk 	struct fdc_softc *fdc;
    920   1.1       pk {
    921   1.1       pk 	struct buf *bp;
    922   1.1       pk 	int read;
    923   1.1       pk 
    924   1.2       pk 	switch (fdc->sc_istate) {
    925   1.2       pk 	case ISTATE_SENSEI:
    926   1.1       pk 		out_fdc(fdc, NE7CMD_SENSEI);
    927   1.1       pk 		fdcresult(fdc);
    928   1.2       pk 		fdc->sc_istate = ISTATE_IDLE;
    929   1.1       pk 		ienab_bis(IE_FDSOFT);
    930   1.1       pk 		return 1;
    931   1.2       pk 	case ISTATE_IDLE:
    932   1.2       pk 	case ISTATE_SPURIOUS:
    933   1.2       pk 		auxregbisc(0, AUXIO_FDS);	/* Does this help? */
    934   1.1       pk 		fdcresult(fdc);
    935   1.2       pk 		fdc->sc_istate = ISTATE_SPURIOUS;
    936   1.2       pk 		printf("fdc: stray hard interrupt... ");
    937   1.1       pk 		ienab_bis(IE_FDSOFT);
    938   1.1       pk 		return 1;
    939   1.2       pk 	case ISTATE_DMA:
    940   1.2       pk 		break;
    941   1.2       pk 	default:
    942   1.2       pk 		printf("fdc: goofed ...\n");
    943   1.2       pk 		return 1;
    944   1.1       pk 	}
    945   1.1       pk 
    946   1.1       pk 	read = bp->b_flags & B_READ;
    947   1.1       pk 	for (;;) {
    948   1.1       pk 		register int msr;
    949   1.1       pk 
    950   1.1       pk 		msr = *fdc->sc_reg_msr;
    951   1.1       pk 
    952   1.1       pk 		if ((msr & NE7_RQM) == 0)
    953   1.1       pk 			break;
    954   1.1       pk 
    955   1.1       pk 		if ((msr & NE7_NDM) == 0) {
    956   1.1       pk 			fdcresult(fdc);
    957   1.2       pk 			fdc->sc_istate = ISTATE_IDLE;
    958   1.1       pk 			ienab_bis(IE_FDSOFT);
    959   1.1       pk 			printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
    960   1.1       pk 			break;
    961   1.1       pk 		}
    962   1.1       pk 
    963   1.1       pk 		if (msr & NE7_DIO) {
    964   1.1       pk #ifdef DIAGNOSTIC
    965   1.1       pk 			if (!read)
    966   1.1       pk 				printf("fdxfer: false read\n");
    967   1.1       pk #endif
    968   1.2       pk 			*fdc->sc_data++ = *fdc->sc_reg_fifo;
    969   1.1       pk 		} else {
    970   1.1       pk #ifdef DIAGNOSTIC
    971   1.1       pk 			if (read)
    972   1.1       pk 				printf("fdxfer: false write\n");
    973   1.1       pk #endif
    974   1.2       pk 			*fdc->sc_reg_fifo = *fdc->sc_data++;
    975   1.1       pk 		}
    976   1.1       pk 		if (--fdc->sc_tc == 0) {
    977   1.1       pk 			auxregbisc(AUXIO_FTC, 0);
    978   1.2       pk 			fdc->sc_istate = ISTATE_IDLE;
    979   1.1       pk 			delay(10);
    980   1.1       pk 			auxregbisc(0, AUXIO_FTC);
    981   1.1       pk 			fdcresult(fdc);
    982   1.1       pk 			ienab_bis(IE_FDSOFT);
    983   1.1       pk 			break;
    984   1.1       pk 		}
    985   1.1       pk 	}
    986   1.1       pk 	return 1;
    987   1.1       pk }
    988   1.2       pk #endif
    989   1.1       pk 
    990   1.1       pk int
    991   1.1       pk fdcswintr(fdc)
    992   1.1       pk 	struct fdc_softc *fdc;
    993   1.1       pk {
    994   1.1       pk #define	st0	fdc->sc_status[0]
    995   1.1       pk #define	st1	fdc->sc_status[1]
    996   1.1       pk #define	cyl	fdc->sc_status[1]
    997   1.2       pk #define OUT_FDC(fdc, c, s) \
    998   1.2       pk     do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
    999   1.2       pk 
   1000   1.1       pk 	struct fd_softc *fd;
   1001   1.1       pk 	struct buf *bp;
   1002   1.1       pk 	int read, head, trac, sec, i, s, nblks;
   1003   1.1       pk 	struct fd_type *type;
   1004   1.1       pk 
   1005   1.1       pk loop:
   1006   1.2       pk 	if (fdc->sc_istate != ISTATE_IDLE) {
   1007   1.2       pk 		/* Trouble... */
   1008   1.2       pk 		printf("fdc: spurious interrupt: istate=%d\n", fdc->sc_istate);
   1009   1.2       pk 		fdc->sc_istate = ISTATE_IDLE;
   1010   1.2       pk 		goto doreset;
   1011   1.2       pk 	}
   1012   1.2       pk 
   1013   1.1       pk 	/* Is there a drive for the controller to do a transfer with? */
   1014   1.1       pk 	fd = fdc->sc_drives.tqh_first;
   1015   1.1       pk 	if (fd == NULL) {
   1016   1.1       pk 		fdc->sc_state = DEVIDLE;
   1017   1.1       pk  		return 0;
   1018   1.1       pk 	}
   1019   1.1       pk 
   1020   1.1       pk 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1021   1.1       pk 	bp = fd->sc_q.b_actf;
   1022   1.1       pk 	if (bp == NULL) {
   1023   1.1       pk 		fd->sc_ops = 0;
   1024   1.1       pk 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1025   1.1       pk 		fd->sc_q.b_active = 0;
   1026   1.1       pk 		goto loop;
   1027   1.1       pk 	}
   1028   1.1       pk 
   1029   1.1       pk 	switch (fdc->sc_state) {
   1030   1.1       pk 	case DEVIDLE:
   1031   1.1       pk 		fdc->sc_errors = 0;
   1032   1.1       pk 		fd->sc_skip = 0;
   1033   1.1       pk 		fd->sc_bcount = bp->b_bcount;
   1034   1.1       pk 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1035   1.1       pk 		untimeout(fd_motor_off, fd);
   1036   1.1       pk 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1037   1.1       pk 			fdc->sc_state = MOTORWAIT;
   1038   1.1       pk 			return 1;
   1039   1.1       pk 		}
   1040   1.1       pk 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1041   1.1       pk 			/* Turn on the motor, being careful about pairing. */
   1042   1.1       pk 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1043   1.1       pk 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1044   1.1       pk 				untimeout(fd_motor_off, ofd);
   1045   1.1       pk 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1046   1.1       pk 			}
   1047   1.1       pk 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1048   1.1       pk 			fd_set_motor(fdc, 0);
   1049   1.1       pk 			fdc->sc_state = MOTORWAIT;
   1050   1.2       pk 			if (fdc->sc_flags & FDC_82077) { /* XXX */
   1051   1.2       pk 				/* Allow .25s for motor to stabilize. */
   1052   1.2       pk 				timeout(fd_motor_on, fd, hz / 4);
   1053   1.2       pk 			} else {
   1054   1.2       pk 				fd->sc_flags &= ~FD_MOTOR_WAIT;
   1055   1.2       pk 				goto loop;
   1056   1.2       pk 			}
   1057   1.1       pk 			return 1;
   1058   1.1       pk 		}
   1059   1.1       pk 		/* Make sure the right drive is selected. */
   1060   1.1       pk 		fd_set_motor(fdc, 0);
   1061   1.1       pk 
   1062   1.1       pk 		/* fall through */
   1063   1.1       pk 	case DOSEEK:
   1064   1.1       pk 	doseek:
   1065   1.2       pk 		if (fdc->sc_flags & FDC_EIS) {
   1066   1.2       pk 			fd->sc_cylin = bp->b_cylin;
   1067   1.2       pk 			/* We use implied seek */
   1068   1.2       pk 			goto doio;
   1069   1.2       pk 		}
   1070   1.2       pk 
   1071   1.1       pk 		if (fd->sc_cylin == bp->b_cylin)
   1072   1.1       pk 			goto doio;
   1073   1.1       pk 
   1074   1.2       pk 		/* specify command */
   1075   1.2       pk 		OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
   1076   1.2       pk 		OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
   1077   1.2       pk 		OUT_FDC(fdc, 6, SEEKTIMEDOUT);	/* XXX head load time == 6ms */
   1078   1.2       pk 
   1079   1.2       pk 		fdc->sc_istate = ISTATE_SENSEI;
   1080   1.2       pk 		/* seek function */
   1081   1.2       pk 		OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
   1082   1.2       pk 		OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
   1083   1.2       pk 		OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
   1084   1.1       pk 
   1085   1.1       pk 		fd->sc_cylin = -1;
   1086   1.1       pk 		fdc->sc_state = SEEKWAIT;
   1087   1.1       pk 		fdc->sc_nstat = 0;
   1088   1.1       pk 		timeout(fdctimeout, fdc, 4 * hz);
   1089   1.1       pk 		return 1;
   1090   1.1       pk 
   1091   1.1       pk 	case DOIO:
   1092   1.1       pk 	doio:
   1093   1.1       pk 		type = fd->sc_type;
   1094   1.1       pk 		sec = fd->sc_blkno % type->seccyl;
   1095   1.1       pk 		nblks = type->seccyl - sec;
   1096   1.1       pk 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1097   1.1       pk 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1098   1.1       pk 		fd->sc_nblks = nblks;
   1099   1.1       pk 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1100   1.1       pk 		head = sec / type->sectrac;
   1101   1.1       pk 		sec -= head * type->sectrac;
   1102   1.1       pk #ifdef DIAGNOSTIC
   1103   1.1       pk 		{int block;
   1104   1.1       pk 		 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
   1105   1.1       pk 		 if (block != fd->sc_blkno) {
   1106   1.1       pk 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1107   1.1       pk #ifdef DDB
   1108   1.1       pk 			 Debugger();
   1109   1.1       pk #endif
   1110   1.1       pk 		 }}
   1111   1.1       pk #endif
   1112   1.1       pk 		read = bp->b_flags & B_READ;
   1113   1.1       pk 
   1114   1.1       pk 		/* Setup for pseudo DMA */
   1115   1.2       pk 		fdc->sc_data = bp->b_data + fd->sc_skip;
   1116   1.1       pk 		fdc->sc_tc = fd->sc_nbytes;
   1117   1.1       pk 
   1118   1.1       pk 		*fdc->sc_reg_drs = type->rate;
   1119   1.1       pk #ifdef FD_DEBUG
   1120   1.2       pk 		if (fdc_debug > 1)
   1121   1.2       pk 			printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1122   1.2       pk 				read ? "read" : "write", fd->sc_drive,
   1123   1.2       pk 				fd->sc_cylin, head, sec, nblks);
   1124   1.1       pk #endif
   1125   1.2       pk 		fdc->sc_state = IOCOMPLETE;
   1126   1.2       pk 		fdc->sc_istate = ISTATE_DMA;
   1127   1.1       pk 		fdc->sc_nstat = 0;
   1128   1.1       pk 		if (read)
   1129   1.2       pk 			OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);	/* READ */
   1130   1.1       pk 		else
   1131   1.2       pk 			OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);	/* WRITE */
   1132   1.2       pk 		OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
   1133   1.2       pk 		OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT);	/* track */
   1134   1.2       pk 		OUT_FDC(fdc, head, IOTIMEDOUT);
   1135   1.2       pk 		OUT_FDC(fdc, sec + 1, IOTIMEDOUT);	/* sector +1 */
   1136   1.2       pk 		OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */
   1137   1.2       pk 		OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */
   1138   1.2       pk 		OUT_FDC(fdc, type->gap1, IOTIMEDOUT);	/* gap1 size */
   1139   1.2       pk 		OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */
   1140   1.1       pk 		/* allow 2 seconds for operation */
   1141   1.1       pk 		timeout(fdctimeout, fdc, 2 * hz);
   1142   1.1       pk 		return 1;				/* will return later */
   1143   1.1       pk 
   1144   1.1       pk 	case SEEKWAIT:
   1145   1.1       pk 		untimeout(fdctimeout, fdc);
   1146   1.1       pk 		fdc->sc_state = SEEKCOMPLETE;
   1147   1.2       pk 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1148   1.2       pk 			/* allow 1/50 second for heads to settle */
   1149   1.2       pk 			timeout(fdcpseudointr, fdc, hz / 50);
   1150   1.2       pk 			return 1;		/* will return later */
   1151   1.2       pk 		}
   1152   1.2       pk 
   1153   1.1       pk 	case SEEKCOMPLETE:
   1154   1.1       pk 		/* Make sure seek really happened. */
   1155   1.1       pk 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
   1156   1.1       pk 		    cyl != bp->b_cylin * fd->sc_type->step) {
   1157   1.1       pk #ifdef FD_DEBUG
   1158   1.2       pk 			if (fdc_debug)
   1159   1.2       pk 				fdcstatus(&fd->sc_dk.dk_dev, 2, "seek failed");
   1160   1.1       pk #endif
   1161   1.1       pk 			fdcretry(fdc);
   1162   1.1       pk 			goto loop;
   1163   1.1       pk 		}
   1164   1.1       pk 		fd->sc_cylin = bp->b_cylin;
   1165   1.1       pk 		goto doio;
   1166   1.1       pk 
   1167   1.1       pk 	case IOTIMEDOUT:
   1168   1.1       pk 		auxregbisc(AUXIO_FTC, 0);
   1169   1.1       pk 		delay(10);
   1170   1.1       pk 		auxregbisc(0, AUXIO_FTC);
   1171   1.1       pk 		(void)fdcresult(fdc);
   1172   1.1       pk 	case SEEKTIMEDOUT:
   1173   1.1       pk 	case RECALTIMEDOUT:
   1174   1.1       pk 	case RESETTIMEDOUT:
   1175   1.1       pk 		fdcretry(fdc);
   1176   1.1       pk 		goto loop;
   1177   1.1       pk 
   1178   1.1       pk 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1179   1.1       pk 		untimeout(fdctimeout, fdc);
   1180   1.1       pk 		if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) {
   1181   1.1       pk #ifdef FD_DEBUG
   1182   1.2       pk 			if (fdc_debug) {
   1183   1.2       pk 				fdcstatus(&fd->sc_dk.dk_dev, 7,
   1184   1.2       pk 					bp->b_flags & B_READ
   1185   1.2       pk 					? "read failed" : "write failed");
   1186   1.2       pk 				printf("blkno %d nblks %d tc %d\n",
   1187   1.2       pk 				       fd->sc_blkno, fd->sc_nblks, fdc->sc_tc);
   1188   1.2       pk 			}
   1189   1.1       pk #endif
   1190   1.6       pk 			if (fdc->sc_nstat == 7 &&
   1191   1.6       pk 			    (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
   1192   1.6       pk 
   1193   1.6       pk 				/*
   1194   1.6       pk 				 * Silently retry overruns if no other
   1195   1.6       pk 				 * error bit is set. Adjust threshold.
   1196   1.6       pk 				 */
   1197   1.2       pk 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1198   1.6       pk 				if (thr < 15) {
   1199   1.6       pk 					thr++;
   1200   1.6       pk 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1201   1.6       pk 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1202   1.2       pk #ifdef FD_DEBUG
   1203   1.6       pk 					if (fdc_debug)
   1204   1.6       pk 						printf("fdc: %d -> threshold\n", thr);
   1205   1.2       pk #endif
   1206   1.6       pk 					fdconf(fdc);
   1207   1.6       pk 					fdc->sc_state = DOIO;
   1208   1.6       pk 					fdc->sc_overruns = 0;
   1209   1.6       pk 				}
   1210   1.6       pk 				if (++fdc->sc_overruns < 3)
   1211   1.6       pk 					goto loop;
   1212   1.2       pk 			}
   1213   1.1       pk 			fdcretry(fdc);
   1214   1.1       pk 			goto loop;
   1215   1.1       pk 		}
   1216   1.1       pk 		if (fdc->sc_errors) {
   1217   1.1       pk 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1218   1.1       pk 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1219   1.1       pk 			printf("\n");
   1220   1.1       pk 			fdc->sc_errors = 0;
   1221   1.6       pk 		} else {
   1222   1.6       pk 			if (--fdc->sc_overruns < -5) {
   1223   1.6       pk 				int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
   1224   1.6       pk 				if (thr > 0) {
   1225   1.6       pk 					thr--;
   1226   1.6       pk 					fdc->sc_cfg &= ~CFG_THRHLD_MASK;
   1227   1.6       pk 					fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
   1228   1.6       pk #ifdef FD_DEBUG
   1229   1.6       pk 					if (fdc_debug)
   1230   1.6       pk 						printf("fdc: %d -> threshold\n", thr);
   1231   1.6       pk #endif
   1232   1.6       pk 					fdconf(fdc);
   1233   1.6       pk 				}
   1234   1.6       pk 				fdc->sc_overruns = 0;
   1235   1.6       pk 			}
   1236   1.1       pk 		}
   1237   1.1       pk 		fd->sc_blkno += fd->sc_nblks;
   1238   1.1       pk 		fd->sc_skip += fd->sc_nbytes;
   1239   1.1       pk 		fd->sc_bcount -= fd->sc_nbytes;
   1240   1.1       pk 		if (fd->sc_bcount > 0) {
   1241   1.1       pk 			bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
   1242   1.1       pk 			goto doseek;
   1243   1.1       pk 		}
   1244   1.1       pk 		fdfinish(fd, bp);
   1245   1.1       pk 		goto loop;
   1246   1.1       pk 
   1247   1.1       pk 	case DORESET:
   1248   1.2       pk 	doreset:
   1249   1.1       pk 		/* try a reset, keep motor on */
   1250   1.1       pk 		fd_set_motor(fdc, 1);
   1251   1.1       pk 		delay(100);
   1252   1.1       pk 		fd_set_motor(fdc, 0);
   1253   1.1       pk 		fdc->sc_state = RESETCOMPLETE;
   1254   1.1       pk 		timeout(fdctimeout, fdc, hz / 2);
   1255   1.1       pk 		return 1;			/* will return later */
   1256   1.1       pk 
   1257   1.1       pk 	case RESETCOMPLETE:
   1258   1.1       pk 		untimeout(fdctimeout, fdc);
   1259   1.1       pk 		/* clear the controller output buffer */
   1260   1.1       pk 		for (i = 0; i < 4; i++) {
   1261   1.1       pk 			out_fdc(fdc, NE7CMD_SENSEI);
   1262   1.1       pk 			(void) fdcresult(fdc);
   1263   1.1       pk 		}
   1264   1.1       pk 
   1265   1.1       pk 		/* fall through */
   1266   1.1       pk 	case DORECAL:
   1267   1.1       pk 		fdc->sc_state = RECALWAIT;
   1268   1.2       pk 		fdc->sc_istate = ISTATE_SENSEI;
   1269   1.1       pk 		fdc->sc_nstat = 0;
   1270   1.2       pk 		/* recalibrate function */
   1271   1.2       pk 		OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
   1272   1.2       pk 		OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
   1273   1.1       pk 		timeout(fdctimeout, fdc, 5 * hz);
   1274   1.1       pk 		return 1;			/* will return later */
   1275   1.1       pk 
   1276   1.1       pk 	case RECALWAIT:
   1277   1.1       pk 		untimeout(fdctimeout, fdc);
   1278   1.1       pk 		fdc->sc_state = RECALCOMPLETE;
   1279   1.2       pk 		if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
   1280   1.2       pk 			/* allow 1/30 second for heads to settle */
   1281   1.2       pk 			timeout(fdcpseudointr, fdc, hz / 30);
   1282   1.2       pk 			return 1;		/* will return later */
   1283   1.2       pk 		}
   1284   1.1       pk 
   1285   1.1       pk 	case RECALCOMPLETE:
   1286   1.1       pk 		if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1287   1.1       pk #ifdef FD_DEBUG
   1288   1.2       pk 			if (fdc_debug)
   1289   1.2       pk 				fdcstatus(&fd->sc_dk.dk_dev, 2, "recalibrate failed");
   1290   1.1       pk #endif
   1291   1.1       pk 			fdcretry(fdc);
   1292   1.1       pk 			goto loop;
   1293   1.1       pk 		}
   1294   1.1       pk 		fd->sc_cylin = 0;
   1295   1.1       pk 		goto doseek;
   1296   1.1       pk 
   1297   1.1       pk 	case MOTORWAIT:
   1298   1.1       pk 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1299   1.1       pk 			return 1;		/* time's not up yet */
   1300   1.1       pk 		goto doseek;
   1301   1.1       pk 
   1302   1.1       pk 	default:
   1303   1.1       pk 		fdcstatus(&fd->sc_dk.dk_dev, 0, "stray interrupt");
   1304   1.1       pk 		return 1;
   1305   1.1       pk 	}
   1306   1.1       pk #ifdef DIAGNOSTIC
   1307   1.1       pk 	panic("fdcintr: impossible");
   1308   1.1       pk #endif
   1309   1.1       pk #undef	st0
   1310   1.1       pk #undef	st1
   1311   1.1       pk #undef	cyl
   1312   1.1       pk }
   1313   1.1       pk 
   1314   1.1       pk void
   1315   1.1       pk fdcretry(fdc)
   1316   1.1       pk 	struct fdc_softc *fdc;
   1317   1.1       pk {
   1318   1.1       pk 	struct fd_softc *fd;
   1319   1.1       pk 	struct buf *bp;
   1320   1.1       pk 
   1321   1.1       pk 	fd = fdc->sc_drives.tqh_first;
   1322   1.1       pk 	bp = fd->sc_q.b_actf;
   1323   1.6       pk 
   1324   1.6       pk 	fdc->sc_overruns = 0;
   1325   1.1       pk 
   1326   1.1       pk 	switch (fdc->sc_errors) {
   1327   1.1       pk 	case 0:
   1328   1.1       pk 		/* try again */
   1329   1.2       pk 		fdc->sc_state =
   1330   1.2       pk 			(fdc->sc_flags & FDC_EIS) ? DOIO : SEEKCOMPLETE;
   1331   1.1       pk 		break;
   1332   1.1       pk 
   1333   1.1       pk 	case 1: case 2: case 3:
   1334   1.1       pk 		/* didn't work; try recalibrating */
   1335   1.1       pk 		fdc->sc_state = DORECAL;
   1336   1.1       pk 		break;
   1337   1.1       pk 
   1338   1.1       pk 	case 4:
   1339   1.1       pk 		/* still no go; reset the bastard */
   1340   1.1       pk 		fdc->sc_state = DORESET;
   1341   1.1       pk 		break;
   1342   1.1       pk 
   1343   1.1       pk 	default:
   1344   1.1       pk 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1345   1.1       pk 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1346   1.1       pk 		printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
   1347   1.1       pk 		    fdc->sc_status[0], NE7_ST0BITS,
   1348   1.1       pk 		    fdc->sc_status[1], NE7_ST1BITS,
   1349   1.1       pk 		    fdc->sc_status[2], NE7_ST2BITS,
   1350   1.1       pk 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1351   1.1       pk 
   1352   1.1       pk 		bp->b_flags |= B_ERROR;
   1353   1.1       pk 		bp->b_error = EIO;
   1354   1.1       pk 		fdfinish(fd, bp);
   1355   1.1       pk 	}
   1356   1.1       pk 	fdc->sc_errors++;
   1357   1.1       pk }
   1358   1.1       pk 
   1359   1.1       pk int
   1360   1.1       pk fdsize(dev)
   1361   1.1       pk 	dev_t dev;
   1362   1.1       pk {
   1363   1.1       pk 
   1364   1.1       pk 	/* Swapping to floppies would not make sense. */
   1365   1.1       pk 	return -1;
   1366   1.1       pk }
   1367   1.1       pk 
   1368   1.1       pk int
   1369   1.1       pk fddump()
   1370   1.1       pk {
   1371   1.1       pk 
   1372   1.1       pk 	/* Not implemented. */
   1373   1.1       pk 	return EINVAL;
   1374   1.1       pk }
   1375   1.1       pk 
   1376   1.1       pk int
   1377   1.1       pk fdioctl(dev, cmd, addr, flag)
   1378   1.1       pk 	dev_t dev;
   1379   1.1       pk 	u_long cmd;
   1380   1.1       pk 	caddr_t addr;
   1381   1.1       pk 	int flag;
   1382   1.1       pk {
   1383   1.1       pk 	struct fd_softc *fd = fdcd.cd_devs[FDUNIT(dev)];
   1384   1.1       pk 	struct disklabel buffer;
   1385   1.1       pk 	int error;
   1386   1.1       pk 
   1387   1.1       pk 	switch (cmd) {
   1388   1.1       pk 	case DIOCGDINFO:
   1389   1.1       pk 		bzero(&buffer, sizeof(buffer));
   1390   1.1       pk 
   1391   1.1       pk 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1392   1.1       pk 		buffer.d_type = DTYPE_FLOPPY;
   1393   1.1       pk 		buffer.d_secsize = FDC_BSIZE;
   1394   1.1       pk 
   1395   1.1       pk 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1396   1.1       pk 			return EINVAL;
   1397   1.1       pk 
   1398   1.1       pk 		*(struct disklabel *)addr = buffer;
   1399   1.1       pk 		return 0;
   1400   1.1       pk 
   1401   1.1       pk 	case DIOCWLABEL:
   1402   1.1       pk 		if ((flag & FWRITE) == 0)
   1403   1.1       pk 			return EBADF;
   1404   1.1       pk 		/* XXX do something */
   1405   1.1       pk 		return 0;
   1406   1.1       pk 
   1407   1.1       pk 	case DIOCWDINFO:
   1408   1.1       pk 		if ((flag & FWRITE) == 0)
   1409   1.1       pk 			return EBADF;
   1410   1.1       pk 
   1411   1.1       pk 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
   1412   1.1       pk 		if (error)
   1413   1.1       pk 			return error;
   1414   1.1       pk 
   1415   1.1       pk 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1416   1.1       pk 		return error;
   1417   1.1       pk 
   1418   1.1       pk 	case FDIOCEJECT:
   1419   1.1       pk 		auxregbisc(AUXIO_FDS, AUXIO_FEJ);
   1420   1.1       pk 		delay(10);
   1421   1.1       pk 		auxregbisc(AUXIO_FEJ, AUXIO_FDS);
   1422   1.1       pk 		return 0;
   1423   1.1       pk #ifdef DEBUG
   1424   1.1       pk 	case _IO('f', 100):
   1425   1.1       pk 		{
   1426   1.1       pk 		int i;
   1427   1.1       pk 		struct fdc_softc *fdc = (struct fdc_softc *)
   1428   1.1       pk 					fd->sc_dk.dk_dev.dv_parent;
   1429   1.1       pk 
   1430   1.1       pk 		out_fdc(fdc, NE7CMD_DUMPREG);
   1431   1.1       pk 		fdcresult(fdc);
   1432   1.1       pk 		printf("dumpreg(%d regs): <", fdc->sc_nstat);
   1433   1.1       pk 		for (i = 0; i < fdc->sc_nstat; i++)
   1434   1.1       pk 			printf(" %x", fdc->sc_status[i]);
   1435   1.1       pk 		printf(">\n");
   1436   1.1       pk 		}
   1437   1.1       pk 
   1438   1.1       pk 		return 0;
   1439   1.1       pk 	case _IOW('f', 101, int):
   1440   1.2       pk 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg &=
   1441   1.2       pk 			~CFG_THRHLD_MASK;
   1442   1.2       pk 		((struct fdc_softc *)fd->sc_dk.dk_dev.dv_parent)->sc_cfg |=
   1443   1.2       pk 			(*(int *)addr & CFG_THRHLD_MASK);
   1444   1.1       pk 		fdconf(fd->sc_dk.dk_dev.dv_parent);
   1445   1.1       pk 		return 0;
   1446   1.1       pk 	case _IO('f', 102):
   1447   1.1       pk 		{
   1448   1.1       pk 		int i;
   1449   1.1       pk 		struct fdc_softc *fdc = (struct fdc_softc *)
   1450   1.1       pk 					fd->sc_dk.dk_dev.dv_parent;
   1451   1.1       pk 		out_fdc(fdc, NE7CMD_SENSEI);
   1452   1.1       pk 		fdcresult(fdc);
   1453   1.1       pk 		printf("sensei(%d regs): <", fdc->sc_nstat);
   1454   1.1       pk 		for (i=0; i< fdc->sc_nstat; i++)
   1455   1.1       pk 			printf(" 0x%x", fdc->sc_status[i]);
   1456   1.1       pk 		}
   1457   1.1       pk 		printf(">\n");
   1458   1.1       pk 		return 0;
   1459   1.1       pk #endif
   1460   1.1       pk 	default:
   1461   1.1       pk 		return ENOTTY;
   1462   1.1       pk 	}
   1463   1.1       pk 
   1464   1.1       pk #ifdef DIAGNOSTIC
   1465   1.1       pk 	panic("fdioctl: impossible");
   1466   1.1       pk #endif
   1467   1.1       pk }
   1468