Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.78
      1  1.78        ad /*	$NetBSD: fd.c,v 1.78 2007/10/08 16:41:09 ad Exp $	*/
      2  1.22   mycroft 
      3  1.22   mycroft /*-
      4  1.22   mycroft  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.22   mycroft  * All rights reserved.
      6  1.22   mycroft  *
      7  1.22   mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.25   minoura  * by Charles M. Hannum and Minoura Makoto.
      9  1.22   mycroft  *
     10  1.22   mycroft  * Redistribution and use in source and binary forms, with or without
     11  1.22   mycroft  * modification, are permitted provided that the following conditions
     12  1.22   mycroft  * are met:
     13  1.22   mycroft  * 1. Redistributions of source code must retain the above copyright
     14  1.22   mycroft  *    notice, this list of conditions and the following disclaimer.
     15  1.22   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.22   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17  1.22   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.22   mycroft  * 3. All advertising materials mentioning features or use of this software
     19  1.22   mycroft  *    must display the following acknowledgement:
     20  1.22   mycroft  *        This product includes software developed by the NetBSD
     21  1.22   mycroft  *        Foundation, Inc. and its contributors.
     22  1.22   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.22   mycroft  *    contributors may be used to endorse or promote products derived
     24  1.22   mycroft  *    from this software without specific prior written permission.
     25  1.22   mycroft  *
     26  1.22   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.22   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.22   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.22   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.22   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.22   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.22   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.22   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.22   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.22   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.22   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37  1.22   mycroft  */
     38   1.1       oki 
     39   1.1       oki /*-
     40   1.1       oki  * Copyright (c) 1990 The Regents of the University of California.
     41   1.1       oki  * All rights reserved.
     42   1.1       oki  *
     43   1.1       oki  * This code is derived from software contributed to Berkeley by
     44   1.1       oki  * Don Ahn.
     45   1.1       oki  *
     46   1.1       oki  * Redistribution and use in source and binary forms, with or without
     47   1.1       oki  * modification, are permitted provided that the following conditions
     48   1.1       oki  * are met:
     49   1.1       oki  * 1. Redistributions of source code must retain the above copyright
     50   1.1       oki  *    notice, this list of conditions and the following disclaimer.
     51   1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     52   1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     53   1.1       oki  *    documentation and/or other materials provided with the distribution.
     54  1.58       agc  * 3. Neither the name of the University nor the names of its contributors
     55   1.1       oki  *    may be used to endorse or promote products derived from this software
     56   1.1       oki  *    without specific prior written permission.
     57   1.1       oki  *
     58   1.1       oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1       oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1       oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1       oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1       oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1       oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1       oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1       oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1       oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1       oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1       oki  * SUCH DAMAGE.
     69   1.1       oki  *
     70   1.1       oki  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     71   1.1       oki  */
     72  1.57     lukem 
     73  1.57     lukem #include <sys/cdefs.h>
     74  1.78        ad __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.78 2007/10/08 16:41:09 ad Exp $");
     75  1.19  jonathan 
     76  1.25   minoura #include "rnd.h"
     77  1.19  jonathan #include "opt_ddb.h"
     78  1.34   minoura #include "opt_m680x0.h"
     79   1.1       oki 
     80   1.1       oki #include <sys/param.h>
     81   1.1       oki #include <sys/systm.h>
     82  1.31   thorpej #include <sys/callout.h>
     83   1.1       oki #include <sys/kernel.h>
     84   1.1       oki #include <sys/conf.h>
     85   1.1       oki #include <sys/file.h>
     86   1.1       oki #include <sys/stat.h>
     87   1.1       oki #include <sys/ioctl.h>
     88   1.1       oki #include <sys/malloc.h>
     89   1.1       oki #include <sys/device.h>
     90   1.1       oki #include <sys/disklabel.h>
     91   1.1       oki #include <sys/disk.h>
     92   1.1       oki #include <sys/buf.h>
     93  1.62      yamt #include <sys/bufq.h>
     94   1.1       oki #include <sys/uio.h>
     95   1.1       oki #include <sys/syslog.h>
     96   1.1       oki #include <sys/queue.h>
     97  1.25   minoura #include <sys/fdio.h>
     98  1.25   minoura #if NRND > 0
     99  1.25   minoura #include <sys/rnd.h>
    100  1.25   minoura #endif
    101  1.27       mrg 
    102  1.25   minoura #include <uvm/uvm_extern.h>
    103   1.1       oki 
    104  1.25   minoura #include <machine/bus.h>
    105   1.1       oki #include <machine/cpu.h>
    106   1.1       oki 
    107  1.25   minoura #include <arch/x68k/dev/intiovar.h>
    108  1.25   minoura #include <arch/x68k/dev/dmacvar.h>
    109  1.25   minoura #include <arch/x68k/dev/fdreg.h>
    110  1.61   minoura #include <arch/x68k/dev/opmvar.h> /* for CT1 access */
    111   1.1       oki 
    112  1.12       jtk #include "locators.h"
    113  1.12       jtk 
    114  1.25   minoura #ifdef FDDEBUG
    115   1.8  christos #define DPRINTF(x)      if (fddebug) printf x
    116   1.1       oki int     fddebug = 0;
    117   1.1       oki #else
    118   1.1       oki #define DPRINTF(x)
    119   1.1       oki #endif
    120   1.1       oki 
    121   1.1       oki #define FDUNIT(dev)	(minor(dev) / 8)
    122   1.1       oki #define FDTYPE(dev)	(minor(dev) % 8)
    123   1.1       oki 
    124   1.1       oki enum fdc_state {
    125   1.1       oki 	DEVIDLE = 0,
    126   1.1       oki 	MOTORWAIT,
    127   1.1       oki 	DOSEEK,
    128   1.1       oki 	SEEKWAIT,
    129   1.1       oki 	SEEKTIMEDOUT,
    130   1.1       oki 	SEEKCOMPLETE,
    131   1.1       oki 	DOIO,
    132   1.1       oki 	IOCOMPLETE,
    133   1.1       oki 	IOTIMEDOUT,
    134   1.1       oki 	DORESET,
    135   1.1       oki 	RESETCOMPLETE,
    136   1.1       oki 	RESETTIMEDOUT,
    137   1.1       oki 	DORECAL,
    138   1.1       oki 	RECALWAIT,
    139   1.1       oki 	RECALTIMEDOUT,
    140   1.1       oki 	RECALCOMPLETE,
    141   1.1       oki 	DOCOPY,
    142   1.1       oki 	DOIOHALF,
    143   1.1       oki 	COPYCOMPLETE,
    144   1.1       oki };
    145   1.1       oki 
    146   1.1       oki /* software state, per controller */
    147   1.1       oki struct fdc_softc {
    148   1.1       oki 	struct device sc_dev;		/* boilerplate */
    149  1.25   minoura 
    150  1.25   minoura 	bus_space_tag_t sc_iot;		/* intio i/o space identifier */
    151  1.25   minoura 	bus_space_handle_t sc_ioh;	/* intio io handle */
    152  1.31   thorpej 
    153  1.31   thorpej 	struct callout sc_timo_ch;	/* timeout callout */
    154  1.31   thorpej 	struct callout sc_intr_ch;	/* pseudo-intr callout */
    155  1.31   thorpej 
    156  1.55       wiz 	bus_dma_tag_t sc_dmat;		/* intio DMA tag */
    157  1.55       wiz 	bus_dmamap_t sc_dmamap;		/* DMA map */
    158  1.25   minoura 	u_int8_t *sc_addr;			/* physical address */
    159  1.55       wiz 	struct dmac_channel_stat *sc_dmachan; /* intio DMA channel */
    160  1.55       wiz 	struct dmac_dma_xfer *sc_xfer;	/* DMA transfer */
    161   1.1       oki 
    162   1.1       oki 	struct fd_softc *sc_fd[4];	/* pointers to children */
    163   1.1       oki 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    164   1.1       oki 	enum fdc_state sc_state;
    165   1.1       oki 	int sc_errors;			/* number of retries so far */
    166   1.1       oki 	u_char sc_status[7];		/* copy of registers */
    167   1.1       oki } fdc_softc;
    168   1.1       oki 
    169  1.63       chs int fdcintr(void *);
    170  1.63       chs void fdcreset(struct fdc_softc *);
    171   1.1       oki 
    172   1.1       oki /* controller driver configuration */
    173  1.63       chs int fdcprobe(struct device *, struct cfdata *, void *);
    174  1.63       chs void fdcattach(struct device *, struct device *, void *);
    175  1.63       chs int fdprint(void *, const char *);
    176   1.1       oki 
    177  1.45   thorpej CFATTACH_DECL(fdc, sizeof(struct fdc_softc),
    178  1.47   thorpej     fdcprobe, fdcattach, NULL, NULL);
    179  1.18   msaitoh 
    180  1.18   msaitoh extern struct cfdriver fdc_cd;
    181   1.1       oki 
    182   1.1       oki /*
    183   1.1       oki  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    184   1.1       oki  * we tell them apart.
    185   1.1       oki  */
    186   1.1       oki struct fd_type {
    187   1.1       oki 	int	sectrac;	/* sectors per track */
    188   1.1       oki 	int	heads;		/* number of heads */
    189   1.1       oki 	int	seccyl;		/* sectors per cylinder */
    190   1.1       oki 	int	secsize;	/* size code for sectors */
    191   1.1       oki 	int	datalen;	/* data len when secsize = 0 */
    192   1.1       oki 	int	steprate;	/* step rate and head unload time */
    193   1.1       oki 	int	gap1;		/* gap len between sectors */
    194   1.1       oki 	int	gap2;		/* formatting gap */
    195  1.25   minoura 	int	cyls;		/* total num of cylinders */
    196   1.1       oki 	int	size;		/* size of disk in sectors */
    197   1.1       oki 	int	step;		/* steps per cylinder */
    198   1.1       oki 	int	rate;		/* transfer speed code */
    199  1.64        he 	const char *name;
    200   1.1       oki };
    201   1.1       oki 
    202   1.1       oki /* The order of entries in the following table is important -- BEWARE! */
    203   1.1       oki struct fd_type fd_types[] = {
    204   1.1       oki         {  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/[1024bytes/sector]"    }, /* 1.2 MB japanese format */
    205   1.1       oki         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    206   1.1       oki         { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    207   1.1       oki         {  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
    208   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
    209   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    }, /* 3.5" 720kB diskette */
    210   1.1       oki         {  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  }, /* 720kB in 1.2MB drive */
    211   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  }, /* 360kB in 720kB drive */
    212   1.1       oki };
    213   1.1       oki 
    214   1.1       oki /* software state, per disk (with up to 4 disks per ctlr) */
    215   1.1       oki struct fd_softc {
    216   1.1       oki 	struct device sc_dev;
    217   1.1       oki 	struct disk sc_dk;
    218   1.1       oki 
    219   1.1       oki 	struct fd_type *sc_deftype;	/* default type descriptor */
    220   1.1       oki 	struct fd_type *sc_type;	/* current type descriptor */
    221   1.1       oki 
    222  1.33   thorpej 	struct callout sc_motoron_ch;
    223  1.33   thorpej 	struct callout sc_motoroff_ch;
    224  1.31   thorpej 
    225   1.1       oki 	daddr_t	sc_blkno;	/* starting block number */
    226   1.1       oki 	int sc_bcount;		/* byte count left */
    227  1.74     isaki 	int sc_opts;		/* user-set options */
    228   1.1       oki 	int sc_skip;		/* bytes already transferred */
    229  1.37       wiz 	int sc_nblks;		/* number of blocks currently transferring */
    230  1.37       wiz 	int sc_nbytes;		/* number of bytes currently transferring */
    231   1.1       oki 
    232   1.1       oki 	int sc_drive;		/* physical unit number */
    233   1.1       oki 	int sc_flags;
    234   1.1       oki #define	FD_BOPEN	0x01		/* it's open */
    235   1.1       oki #define	FD_COPEN	0x02		/* it's open */
    236   1.1       oki #define	FD_OPEN		(FD_BOPEN|FD_COPEN)	/* it's open */
    237   1.1       oki #define	FD_MOTOR	0x04		/* motor should be on */
    238   1.1       oki #define	FD_MOTOR_WAIT	0x08		/* motor coming up */
    239   1.1       oki #define	FD_ALIVE	0x10		/* alive */
    240   1.1       oki 	int sc_cylin;		/* where we think the head is */
    241   1.1       oki 
    242   1.1       oki 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    243   1.1       oki 	int sc_ops;		/* I/O ops since last switch */
    244  1.65      yamt 	struct bufq_state *sc_q;/* pending I/O requests */
    245  1.29   thorpej 	int sc_active;		/* number of active I/O operations */
    246   1.1       oki 	u_char *sc_copybuf;	/* for secsize >=3 */
    247   1.1       oki 	u_char sc_part;		/* for secsize >=3 */
    248   1.1       oki #define	SEC_P10	0x02		/* first part */
    249   1.1       oki #define	SEC_P01	0x01		/* second part */
    250   1.1       oki #define	SEC_P11	0x03		/* both part */
    251  1.25   minoura 
    252  1.25   minoura #if NRND > 0
    253  1.25   minoura 	rndsource_element_t	rnd_source;
    254  1.25   minoura #endif
    255   1.1       oki };
    256   1.1       oki 
    257   1.1       oki /* floppy driver configuration */
    258  1.63       chs int fdprobe(struct device *, struct cfdata *, void *);
    259  1.63       chs void fdattach(struct device *, struct device *, void *);
    260   1.1       oki 
    261  1.46   thorpej CFATTACH_DECL(fd, sizeof(struct fd_softc),
    262  1.47   thorpej     fdprobe, fdattach, NULL, NULL);
    263   1.1       oki 
    264  1.15   thorpej extern struct cfdriver fd_cd;
    265   1.1       oki 
    266  1.43   gehenna dev_type_open(fdopen);
    267  1.43   gehenna dev_type_close(fdclose);
    268  1.43   gehenna dev_type_read(fdread);
    269  1.43   gehenna dev_type_write(fdwrite);
    270  1.43   gehenna dev_type_ioctl(fdioctl);
    271  1.43   gehenna dev_type_strategy(fdstrategy);
    272  1.43   gehenna 
    273  1.43   gehenna const struct bdevsw fd_bdevsw = {
    274  1.43   gehenna 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    275  1.43   gehenna };
    276  1.43   gehenna 
    277  1.43   gehenna const struct cdevsw fd_cdevsw = {
    278  1.43   gehenna 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    279  1.50  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    280  1.43   gehenna };
    281  1.43   gehenna 
    282  1.63       chs void fdstart(struct fd_softc *);
    283   1.1       oki 
    284   1.1       oki struct dkdriver fddkdriver = { fdstrategy };
    285   1.1       oki 
    286  1.63       chs void fd_set_motor(struct fdc_softc *, int);
    287  1.63       chs void fd_motor_off(void *);
    288  1.63       chs void fd_motor_on(void *);
    289  1.63       chs int fdcresult(struct fdc_softc *);
    290  1.63       chs int out_fdc(bus_space_tag_t, bus_space_handle_t, u_char);
    291  1.63       chs void fdcstart(struct fdc_softc *);
    292  1.64        he void fdcstatus(struct device *, int, const char *);
    293  1.63       chs void fdctimeout(void *);
    294  1.63       chs void fdcpseudointr(void *);
    295  1.63       chs void fdcretry(struct fdc_softc *);
    296  1.63       chs void fdfinish(struct fd_softc *, struct buf *);
    297  1.67     perry inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    298  1.63       chs static int fdcpoll(struct fdc_softc *);
    299  1.63       chs static int fdgetdisklabel(struct fd_softc *, dev_t);
    300  1.63       chs static void fd_do_eject(struct fdc_softc *, int);
    301  1.13       oki 
    302  1.63       chs void fd_mountroot_hook(struct device *);
    303   1.1       oki 
    304  1.55       wiz /* DMA transfer routines */
    305  1.71  christos inline static void fdc_dmastart(struct fdc_softc *, int, void *, vsize_t);
    306  1.63       chs static int fdcdmaintr(void *);
    307  1.63       chs static int fdcdmaerrintr(void *);
    308   1.1       oki 
    309  1.67     perry inline static void
    310  1.71  christos fdc_dmastart(struct fdc_softc *fdc, int read, void *addr, vsize_t count)
    311   1.1       oki {
    312  1.25   minoura 	int error;
    313   1.1       oki 
    314  1.49     isaki 	DPRINTF(("fdc_dmastart: %s, addr = %p, count = %ld\n",
    315  1.71  christos 		 read ? "read" : "write", (void *) addr, count));
    316   1.1       oki 
    317  1.25   minoura 	error = bus_dmamap_load(fdc->sc_dmat, fdc->sc_dmamap, addr, count,
    318  1.25   minoura 				0, BUS_DMA_NOWAIT);
    319  1.25   minoura 	if (error) {
    320  1.25   minoura 		panic ("fdc_dmastart: cannot load dmamap");
    321  1.25   minoura 	}
    322  1.25   minoura 
    323  1.25   minoura 	bus_dmamap_sync(fdc->sc_dmat, fdc->sc_dmamap, 0, count,
    324  1.25   minoura 			read?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE);
    325  1.25   minoura 
    326  1.25   minoura 	fdc->sc_xfer = dmac_prepare_xfer(fdc->sc_dmachan, fdc->sc_dmat,
    327  1.25   minoura 					 fdc->sc_dmamap,
    328  1.25   minoura 					 (read?
    329  1.25   minoura 					  DMAC_OCR_DIR_DTM:DMAC_OCR_DIR_MTD),
    330  1.25   minoura 					 (DMAC_SCR_MAC_COUNT_UP|
    331  1.25   minoura 					  DMAC_SCR_DAC_NO_COUNT),
    332  1.25   minoura 					 (u_int8_t*) (fdc->sc_addr +
    333  1.25   minoura 						      fddata));	/* XXX */
    334  1.25   minoura 
    335  1.25   minoura 	dmac_start_xfer(fdc->sc_dmachan->ch_softc, fdc->sc_xfer);
    336   1.1       oki }
    337   1.1       oki 
    338  1.25   minoura static int
    339  1.63       chs fdcdmaintr(void *arg)
    340   1.1       oki {
    341  1.25   minoura 	struct fdc_softc *fdc = arg;
    342  1.25   minoura 
    343  1.25   minoura 	bus_dmamap_unload(fdc->sc_dmat, fdc->sc_dmamap);
    344  1.25   minoura 
    345  1.25   minoura 	return 0;
    346   1.1       oki }
    347   1.1       oki 
    348  1.25   minoura static int
    349  1.63       chs fdcdmaerrintr(void *dummy)
    350   1.1       oki {
    351  1.25   minoura 	DPRINTF(("fdcdmaerrintr\n"));
    352  1.25   minoura 
    353  1.25   minoura 	return 0;
    354   1.1       oki }
    355   1.1       oki 
    356  1.20   minoura /* ARGSUSED */
    357   1.1       oki int
    358  1.63       chs fdcprobe(struct device *parent, struct cfdata *cf, void *aux)
    359   1.1       oki {
    360  1.25   minoura 	struct intio_attach_args *ia = aux;
    361  1.25   minoura 
    362  1.25   minoura 	if (strcmp(ia->ia_name, "fdc") != 0)
    363   1.1       oki 		return 0;
    364  1.25   minoura 
    365  1.25   minoura 	if (ia->ia_addr == INTIOCF_ADDR_DEFAULT)
    366  1.25   minoura 		ia->ia_addr = FDC_ADDR;
    367  1.25   minoura 	if (ia->ia_intr == INTIOCF_INTR_DEFAULT)
    368  1.25   minoura 		ia->ia_intr = FDC_INTR;
    369  1.25   minoura 	if (ia->ia_dma == INTIOCF_DMA_DEFAULT)
    370  1.25   minoura 		ia->ia_dma = FDC_DMA;
    371  1.25   minoura 	if (ia->ia_dmaintr == INTIOCF_DMAINTR_DEFAULT)
    372  1.25   minoura 		ia->ia_dmaintr = FDC_DMAINTR;
    373  1.25   minoura 
    374  1.28   minoura 	if ((ia->ia_intr & 0x03) != 0)
    375  1.25   minoura 		return 0;
    376  1.25   minoura 
    377  1.25   minoura 	ia->ia_size = 0x2000;
    378  1.25   minoura 	if (intio_map_allocate_region (parent, ia, INTIO_MAP_TESTONLY))
    379  1.25   minoura 		return 0;
    380  1.25   minoura 
    381  1.25   minoura 	/* builtin device; always there */
    382   1.1       oki 	return 1;
    383   1.1       oki }
    384   1.1       oki 
    385   1.1       oki /*
    386   1.1       oki  * Arguments passed between fdcattach and fdprobe.
    387   1.1       oki  */
    388   1.1       oki struct fdc_attach_args {
    389   1.1       oki 	int fa_drive;
    390   1.1       oki 	struct fd_type *fa_deftype;
    391   1.1       oki };
    392   1.1       oki 
    393   1.1       oki /*
    394   1.1       oki  * Print the location of a disk drive (called just before attaching the
    395   1.1       oki  * the drive).  If `fdc' is not NULL, the drive was found but was not
    396   1.1       oki  * in the system config file; print the drive name as well.
    397   1.1       oki  * Return QUIET (config_find ignores this if the device was configured) to
    398   1.1       oki  * avoid printing `fdN not configured' messages.
    399   1.1       oki  */
    400   1.1       oki int
    401  1.63       chs fdprint(void *aux, const char *fdc)
    402   1.1       oki {
    403  1.63       chs 	struct fdc_attach_args *fa = aux;
    404   1.1       oki 
    405   1.1       oki 	if (!fdc)
    406  1.51   thorpej 		aprint_normal(" drive %d", fa->fa_drive);
    407   1.1       oki 	return QUIET;
    408   1.1       oki }
    409   1.1       oki 
    410   1.1       oki void
    411  1.63       chs fdcattach(struct device *parent, struct device *self, void *aux)
    412   1.1       oki {
    413   1.1       oki 	struct fdc_softc *fdc = (void *)self;
    414  1.25   minoura 	bus_space_tag_t iot;
    415  1.25   minoura 	bus_space_handle_t ioh;
    416  1.25   minoura 	struct intio_attach_args *ia = aux;
    417   1.1       oki 	struct fdc_attach_args fa;
    418   1.1       oki 
    419  1.25   minoura 	iot = ia->ia_bst;
    420  1.25   minoura 
    421  1.25   minoura 	printf("\n");
    422  1.25   minoura 
    423  1.76        ad 	callout_init(&fdc->sc_timo_ch, 0);
    424  1.76        ad 	callout_init(&fdc->sc_intr_ch, 0);
    425  1.31   thorpej 
    426  1.25   minoura 	/* Re-map the I/O space. */
    427  1.25   minoura 	bus_space_map(iot, ia->ia_addr, 0x2000, BUS_SPACE_MAP_SHIFTED, &ioh);
    428  1.25   minoura 
    429  1.25   minoura 	fdc->sc_iot = iot;
    430  1.25   minoura 	fdc->sc_ioh = ioh;
    431  1.73     isaki 	fdc->sc_addr = (void *)ia->ia_addr;
    432  1.25   minoura 
    433  1.25   minoura 	fdc->sc_dmat = ia->ia_dmat;
    434   1.1       oki 	fdc->sc_state = DEVIDLE;
    435   1.1       oki 	TAILQ_INIT(&fdc->sc_drives);
    436   1.1       oki 
    437  1.25   minoura 	/* Initialize DMAC channel */
    438  1.25   minoura 	fdc->sc_dmachan = dmac_alloc_channel(parent, ia->ia_dma, "fdc",
    439  1.25   minoura 					     ia->ia_dmaintr, fdcdmaintr, fdc,
    440  1.25   minoura 					     ia->ia_dmaintr+1, fdcdmaerrintr,
    441  1.25   minoura 					     fdc);
    442  1.38   minoura 	if (bus_dmamap_create(fdc->sc_dmat, FDC_MAXIOSIZE, 1, DMAC_MAXSEGSZ,
    443  1.38   minoura 			      0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    444  1.25   minoura 			      &fdc->sc_dmamap)) {
    445  1.25   minoura 		printf("%s: can't set up intio DMA map\n",
    446  1.25   minoura 		    fdc->sc_dev.dv_xname);
    447  1.25   minoura 		return;
    448  1.25   minoura 	}
    449  1.25   minoura 
    450  1.25   minoura 	if (intio_intr_establish(ia->ia_intr, "fdc", fdcintr, fdc))
    451  1.25   minoura 		panic ("Could not establish interrupt (duplicated vector?).");
    452  1.25   minoura 	intio_set_ivec(ia->ia_intr);
    453   1.1       oki 
    454   1.1       oki 	/* reset */
    455  1.25   minoura 	intio_disable_intr(SICILIAN_INTR_FDD);
    456  1.25   minoura 	intio_enable_intr(SICILIAN_INTR_FDC);
    457   1.1       oki 	fdcresult(fdc);
    458  1.25   minoura 	fdcreset(fdc);
    459   1.1       oki 
    460  1.25   minoura 	printf("%s: uPD72065 FDC\n", fdc->sc_dev.dv_xname);
    461  1.25   minoura 	out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
    462  1.25   minoura 	out_fdc(iot, ioh, 0xd0);
    463  1.25   minoura 	out_fdc(iot, ioh, 0x10);
    464   1.1       oki 
    465   1.1       oki 	/* physical limit: four drives per controller. */
    466   1.1       oki 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    467   1.1       oki 		(void)config_found(self, (void *)&fa, fdprint);
    468   1.1       oki 	}
    469  1.25   minoura 
    470  1.25   minoura 	intio_enable_intr(SICILIAN_INTR_FDC);
    471   1.1       oki }
    472   1.1       oki 
    473   1.1       oki void
    474  1.63       chs fdcreset(struct fdc_softc *fdc)
    475   1.1       oki {
    476  1.25   minoura 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdsts, NE7CMD_RESET);
    477   1.1       oki }
    478   1.1       oki 
    479   1.1       oki static int
    480  1.63       chs fdcpoll(struct fdc_softc *fdc)
    481   1.1       oki {
    482  1.25   minoura 	int i = 25000, n;
    483   1.1       oki 	while (--i > 0) {
    484  1.25   minoura 		if ((intio_get_sicilian_intr() & SICILIAN_STAT_FDC)) {
    485  1.25   minoura 			out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    486  1.25   minoura 			n = fdcresult(fdc);
    487   1.1       oki 			break;
    488   1.1       oki 		}
    489   1.1       oki 		DELAY(100);
    490   1.1       oki 	}
    491   1.1       oki 	return i;
    492   1.1       oki }
    493   1.1       oki 
    494   1.1       oki int
    495  1.63       chs fdprobe(struct device *parent, struct cfdata *cf, void *aux)
    496   1.1       oki {
    497   1.1       oki 	struct fdc_softc *fdc = (void *)parent;
    498   1.1       oki 	struct fd_type *type;
    499  1.25   minoura 	struct fdc_attach_args *fa = aux;
    500  1.25   minoura 	int drive = fa->fa_drive;
    501  1.25   minoura 	bus_space_tag_t iot = fdc->sc_iot;
    502  1.25   minoura 	bus_space_handle_t ioh = fdc->sc_ioh;
    503  1.60     isaki 	int n = 0;
    504   1.1       oki 	int found = 0;
    505   1.1       oki 	int i;
    506   1.1       oki 
    507  1.12       jtk 	if (cf->cf_loc[FDCCF_UNIT] != FDCCF_UNIT_DEFAULT &&
    508  1.12       jtk 	    cf->cf_loc[FDCCF_UNIT] != drive)
    509   1.1       oki 		return 0;
    510   1.1       oki 
    511   1.1       oki 	type = &fd_types[0];	/* XXX 1.2MB */
    512   1.1       oki 
    513  1.25   minoura 	intio_disable_intr(SICILIAN_INTR_FDC);
    514   1.1       oki 
    515   1.1       oki 	/* select drive and turn on motor */
    516  1.25   minoura 	bus_space_write_1(iot, ioh, fdctl, 0x80 | (type->rate << 4)| drive);
    517   1.1       oki 	fdc_force_ready(FDCRDY);
    518   1.1       oki 	fdcpoll(fdc);
    519   1.1       oki 
    520   1.1       oki retry:
    521  1.25   minoura 	out_fdc(iot, ioh, NE7CMD_RECAL);
    522  1.25   minoura 	out_fdc(iot, ioh, drive);
    523   1.1       oki 
    524   1.1       oki 	i = 25000;
    525   1.1       oki 	while (--i > 0) {
    526  1.25   minoura 		if ((intio_get_sicilian_intr() & SICILIAN_STAT_FDC)) {
    527  1.25   minoura 			out_fdc(iot, ioh, NE7CMD_SENSEI);
    528   1.1       oki 			n = fdcresult(fdc);
    529   1.1       oki 			break;
    530   1.1       oki 		}
    531   1.1       oki 		DELAY(100);
    532   1.1       oki 	}
    533   1.1       oki 
    534   1.1       oki #ifdef FDDEBUG
    535   1.1       oki 	{
    536  1.75     isaki 		int _i;
    537  1.25   minoura 		DPRINTF(("fdprobe: status"));
    538  1.75     isaki 		for (_i = 0; _i < n; _i++)
    539  1.75     isaki 			DPRINTF((" %x", fdc->sc_status[_i]));
    540  1.25   minoura 		DPRINTF(("\n"));
    541   1.1       oki 	}
    542   1.1       oki #endif
    543   1.1       oki 
    544  1.60     isaki 	if (n == 2) {
    545  1.60     isaki 		if ((fdc->sc_status[0] & 0xf0) == 0x20)
    546  1.60     isaki 			found = 1;
    547  1.60     isaki 		else if ((fdc->sc_status[0] & 0xf0) == 0xc0)
    548  1.60     isaki 			goto retry;
    549  1.60     isaki 	}
    550  1.60     isaki 
    551   1.1       oki 	/* turn off motor */
    552  1.25   minoura 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh,
    553  1.25   minoura 			  fdctl, (type->rate << 4)| drive);
    554   1.1       oki 	fdc_force_ready(FDCSTBY);
    555   1.1       oki 	if (!found) {
    556  1.25   minoura 		intio_enable_intr(SICILIAN_INTR_FDC);
    557   1.1       oki 		return 0;
    558   1.1       oki 	}
    559   1.1       oki 
    560   1.1       oki 	return 1;
    561   1.1       oki }
    562   1.1       oki 
    563  1.25   minoura /*
    564  1.25   minoura  * Controller is working, and drive responded.  Attach it.
    565  1.25   minoura  */
    566   1.1       oki void
    567  1.63       chs fdattach(struct device *parent, struct device *self, void *aux)
    568   1.1       oki {
    569   1.1       oki 	struct fdc_softc *fdc = (void *)parent;
    570  1.25   minoura 	struct fd_softc *fd = (void *)self;
    571   1.1       oki 	struct fdc_attach_args *fa = aux;
    572  1.25   minoura 	struct fd_type *type = &fd_types[0];	/* XXX 1.2MB */
    573   1.1       oki 	int drive = fa->fa_drive;
    574   1.1       oki 
    575  1.76        ad 	callout_init(&fd->sc_motoron_ch, 0);
    576  1.76        ad 	callout_init(&fd->sc_motoroff_ch, 0);
    577  1.31   thorpej 
    578   1.1       oki 	fd->sc_flags = 0;
    579   1.1       oki 
    580   1.1       oki 	if (type)
    581  1.25   minoura 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
    582  1.25   minoura 		       type->cyls, type->heads, type->sectrac);
    583   1.1       oki 	else
    584   1.8  christos 		printf(": density unknown\n");
    585   1.1       oki 
    586  1.65      yamt 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    587   1.1       oki 	fd->sc_cylin = -1;
    588   1.1       oki 	fd->sc_drive = drive;
    589   1.1       oki 	fd->sc_deftype = type;
    590   1.1       oki 	fdc->sc_fd[drive] = fd;
    591   1.1       oki 
    592  1.54   thorpej 	fd->sc_copybuf = (u_char *)malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    593   1.1       oki 	if (fd->sc_copybuf == 0)
    594   1.8  christos 		printf("fdprobe: WARNING!! malloc() failed.\n");
    595   1.1       oki 	fd->sc_flags |= FD_ALIVE;
    596   1.1       oki 
    597   1.1       oki 	/*
    598   1.1       oki 	 * Initialize and attach the disk structure.
    599   1.1       oki 	 */
    600  1.78        ad 	disk_init(&fd->sc_dk, fd->sc_dev.dv_xname, &fddkdriver);
    601   1.1       oki 	disk_attach(&fd->sc_dk);
    602   1.4       oki 
    603   1.4       oki 	/*
    604   1.4       oki 	 * Establish a mountroot_hook anyway in case we booted
    605   1.4       oki 	 * with RB_ASKNAME and get selected as the boot device.
    606   1.4       oki 	 */
    607   1.9   thorpej 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
    608  1.25   minoura 
    609  1.25   minoura #if NRND > 0
    610  1.26   minoura 	rnd_attach_source(&fd->rnd_source, fd->sc_dev.dv_xname,
    611  1.26   minoura 			  RND_TYPE_DISK, 0);
    612  1.25   minoura #endif
    613   1.1       oki }
    614   1.1       oki 
    615  1.67     perry inline struct fd_type *
    616  1.63       chs fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    617   1.1       oki {
    618   1.1       oki 	int type = FDTYPE(dev);
    619   1.1       oki 
    620   1.1       oki 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    621   1.1       oki 		return NULL;
    622   1.1       oki 	return &fd_types[type];
    623   1.1       oki }
    624   1.1       oki 
    625   1.1       oki void
    626  1.63       chs fdstrategy(struct buf *bp)
    627   1.1       oki {
    628   1.1       oki 	struct fd_softc *fd;
    629   1.1       oki 	int unit = FDUNIT(bp->b_dev);
    630   1.1       oki 	int sz;
    631  1.74     isaki 	int s;
    632   1.1       oki 
    633   1.1       oki 	if (unit >= fd_cd.cd_ndevs ||
    634   1.1       oki 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
    635   1.1       oki 	    bp->b_blkno < 0 ||
    636   1.1       oki 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    637  1.60     isaki 		DPRINTF(("fdstrategy: unit=%d, blkno=%" PRId64 ", "
    638  1.75     isaki 			 "bcount=%d\n", unit,
    639  1.25   minoura 			 bp->b_blkno, bp->b_bcount));
    640   1.1       oki 		bp->b_error = EINVAL;
    641  1.77        ad 		goto done;
    642   1.1       oki 	}
    643   1.1       oki 
    644   1.1       oki 	/* If it's a null transfer, return immediately. */
    645   1.1       oki 	if (bp->b_bcount == 0)
    646   1.1       oki 		goto done;
    647   1.1       oki 
    648   1.1       oki 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    649   1.1       oki 
    650  1.48     isaki 	if (bp->b_blkno + sz >
    651  1.48     isaki 	    (fd->sc_type->size << (fd->sc_type->secsize - 2))) {
    652  1.48     isaki 		sz = (fd->sc_type->size << (fd->sc_type->secsize - 2))
    653  1.48     isaki 		     - bp->b_blkno;
    654   1.1       oki 		if (sz == 0) {
    655   1.1       oki 			/* If exactly at end of disk, return EOF. */
    656   1.1       oki 			bp->b_resid = bp->b_bcount;
    657   1.1       oki 			goto done;
    658   1.1       oki 		}
    659   1.1       oki 		if (sz < 0) {
    660   1.1       oki 			/* If past end of disk, return EINVAL. */
    661   1.1       oki 			bp->b_error = EINVAL;
    662  1.77        ad 			goto done;
    663   1.1       oki 		}
    664   1.1       oki 		/* Otherwise, truncate request. */
    665   1.1       oki 		bp->b_bcount = sz << DEV_BSHIFT;
    666   1.1       oki 	}
    667   1.1       oki 
    668  1.30   thorpej 	bp->b_rawblkno = bp->b_blkno;
    669  1.74     isaki 	bp->b_cylinder = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE)
    670   1.1       oki 		/ (fd->sc_type->seccyl * (1 << (fd->sc_type->secsize - 2)));
    671   1.1       oki 
    672  1.75     isaki 	DPRINTF(("fdstrategy: %s b_blkno %" PRId64 " b_bcount %d cylin %d\n",
    673   1.1       oki 		 bp->b_flags & B_READ ? "read" : "write",
    674  1.29   thorpej 		 bp->b_blkno, bp->b_bcount, bp->b_cylinder));
    675   1.1       oki 	/* Queue transfer on drive, activate drive and controller if idle. */
    676   1.1       oki 	s = splbio();
    677  1.65      yamt 	BUFQ_PUT(fd->sc_q, bp);
    678  1.33   thorpej 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    679  1.29   thorpej 	if (fd->sc_active == 0)
    680   1.1       oki 		fdstart(fd);
    681   1.1       oki #ifdef DIAGNOSTIC
    682   1.1       oki 	else {
    683  1.68   thorpej 		struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
    684   1.1       oki 		if (fdc->sc_state == DEVIDLE) {
    685   1.8  christos 			printf("fdstrategy: controller inactive\n");
    686   1.1       oki 			fdcstart(fdc);
    687   1.1       oki 		}
    688   1.1       oki 	}
    689   1.1       oki #endif
    690   1.1       oki 	splx(s);
    691   1.1       oki 	return;
    692   1.1       oki 
    693   1.1       oki done:
    694   1.1       oki 	/* Toss transfer; we're done early. */
    695   1.1       oki 	biodone(bp);
    696   1.1       oki }
    697   1.1       oki 
    698   1.1       oki void
    699  1.63       chs fdstart(struct fd_softc *fd)
    700   1.1       oki {
    701  1.68   thorpej 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
    702   1.1       oki 	int active = fdc->sc_drives.tqh_first != 0;
    703   1.1       oki 
    704   1.1       oki 	/* Link into controller queue. */
    705  1.29   thorpej 	fd->sc_active = 1;
    706   1.1       oki 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    707   1.1       oki 
    708   1.1       oki 	/* If controller not already active, start it. */
    709   1.1       oki 	if (!active)
    710   1.1       oki 		fdcstart(fdc);
    711   1.1       oki }
    712   1.1       oki 
    713   1.1       oki void
    714  1.63       chs fdfinish(struct fd_softc *fd, struct buf *bp)
    715   1.1       oki {
    716  1.68   thorpej 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
    717   1.1       oki 
    718   1.1       oki 	/*
    719   1.1       oki 	 * Move this drive to the end of the queue to give others a `fair'
    720   1.1       oki 	 * chance.  We only force a switch if N operations are completed while
    721   1.1       oki 	 * another drive is waiting to be serviced, since there is a long motor
    722   1.1       oki 	 * startup delay whenever we switch.
    723   1.1       oki 	 */
    724  1.65      yamt 	(void)BUFQ_GET(fd->sc_q);
    725   1.1       oki 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    726   1.1       oki 		fd->sc_ops = 0;
    727   1.1       oki 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    728  1.65      yamt 		if (BUFQ_PEEK(fd->sc_q) != NULL) {
    729   1.1       oki 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    730   1.1       oki 		} else
    731  1.29   thorpej 			fd->sc_active = 0;
    732   1.1       oki 	}
    733   1.1       oki 	bp->b_resid = fd->sc_bcount;
    734   1.1       oki 	fd->sc_skip = 0;
    735  1.25   minoura 
    736  1.25   minoura #if NRND > 0
    737  1.25   minoura 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
    738  1.25   minoura #endif
    739  1.25   minoura 
    740   1.1       oki 	biodone(bp);
    741   1.1       oki 	/* turn off motor 5s from now */
    742  1.33   thorpej 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    743   1.1       oki 	fdc->sc_state = DEVIDLE;
    744   1.1       oki }
    745   1.1       oki 
    746   1.1       oki int
    747  1.63       chs fdread(dev_t dev, struct uio *uio, int flags)
    748   1.1       oki {
    749   1.1       oki 
    750   1.1       oki 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    751   1.1       oki }
    752   1.1       oki 
    753   1.1       oki int
    754  1.63       chs fdwrite(dev_t dev, struct uio *uio, int flags)
    755   1.1       oki {
    756   1.1       oki 
    757   1.1       oki 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    758   1.1       oki }
    759   1.1       oki 
    760   1.1       oki void
    761  1.63       chs fd_set_motor(struct fdc_softc *fdc, int reset)
    762   1.1       oki {
    763   1.1       oki 	struct fd_softc *fd;
    764   1.1       oki 	int n;
    765   1.1       oki 
    766   1.1       oki 	DPRINTF(("fd_set_motor:\n"));
    767   1.1       oki 	for (n = 0; n < 4; n++)
    768   1.1       oki 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) {
    769  1.25   minoura 			bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdctl,
    770  1.25   minoura 					  0x80 | (fd->sc_type->rate << 4)| n);
    771   1.1       oki 		}
    772   1.1       oki }
    773   1.1       oki 
    774   1.1       oki void
    775  1.63       chs fd_motor_off(void *arg)
    776   1.1       oki {
    777   1.1       oki 	struct fd_softc *fd = arg;
    778  1.68   thorpej 	struct fdc_softc *fdc = (struct fdc_softc*) device_parent(&fd->sc_dev);
    779   1.1       oki 	int s;
    780   1.1       oki 
    781   1.1       oki 	DPRINTF(("fd_motor_off:\n"));
    782   1.1       oki 
    783   1.1       oki 	s = splbio();
    784   1.1       oki 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    785  1.25   minoura 	bus_space_write_1 (fdc->sc_iot, fdc->sc_ioh, fdctl,
    786  1.25   minoura 			   (fd->sc_type->rate << 4) | fd->sc_drive);
    787   1.1       oki #if 0
    788  1.25   minoura 	fd_set_motor(fdc, 0); /* XXX */
    789   1.1       oki #endif
    790   1.1       oki 	splx(s);
    791   1.1       oki }
    792   1.1       oki 
    793   1.1       oki void
    794  1.63       chs fd_motor_on(void *arg)
    795   1.1       oki {
    796   1.1       oki 	struct fd_softc *fd = arg;
    797  1.68   thorpej 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
    798   1.1       oki 	int s;
    799   1.1       oki 
    800   1.1       oki 	DPRINTF(("fd_motor_on:\n"));
    801   1.1       oki 
    802   1.1       oki 	s = splbio();
    803   1.1       oki 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    804   1.1       oki 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    805  1.25   minoura 		(void) fdcintr(fdc);
    806   1.1       oki 	splx(s);
    807   1.1       oki }
    808   1.1       oki 
    809   1.1       oki int
    810  1.63       chs fdcresult(struct fdc_softc *fdc)
    811   1.1       oki {
    812  1.25   minoura 	bus_space_tag_t iot = fdc->sc_iot;
    813  1.25   minoura 	bus_space_handle_t ioh = fdc->sc_ioh;
    814   1.1       oki 	u_char i;
    815   1.1       oki 	int j = 100000,
    816   1.1       oki 	    n = 0;
    817   1.1       oki 
    818   1.1       oki 	for (; j; j--) {
    819  1.25   minoura 		i = bus_space_read_1(iot, ioh, fdsts) &
    820  1.25   minoura 		  (NE7_DIO | NE7_RQM | NE7_CB);
    821   1.1       oki 
    822   1.1       oki 		if (i == NE7_RQM)
    823   1.1       oki 			return n;
    824   1.1       oki 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    825   1.1       oki 			if (n >= sizeof(fdc->sc_status)) {
    826   1.1       oki 				log(LOG_ERR, "fdcresult: overrun\n");
    827   1.1       oki 				return -1;
    828   1.1       oki 			}
    829  1.25   minoura 			fdc->sc_status[n++] =
    830  1.25   minoura 			  bus_space_read_1(iot, ioh, fddata);
    831   1.1       oki 		}
    832  1.25   minoura 		delay(10);
    833   1.1       oki 	}
    834   1.1       oki 	log(LOG_ERR, "fdcresult: timeout\n");
    835   1.1       oki 	return -1;
    836   1.1       oki }
    837   1.1       oki 
    838   1.1       oki int
    839  1.63       chs out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
    840   1.1       oki {
    841   1.1       oki 	int i = 100000;
    842   1.1       oki 
    843  1.25   minoura 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0);
    844   1.1       oki 	if (i <= 0)
    845   1.1       oki 		return -1;
    846  1.25   minoura 	while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0);
    847   1.1       oki 	if (i <= 0)
    848   1.1       oki 		return -1;
    849  1.25   minoura 	bus_space_write_1(iot, ioh, fddata, x);
    850   1.1       oki 	return 0;
    851   1.1       oki }
    852   1.1       oki 
    853   1.1       oki int
    854  1.66  christos fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    855   1.1       oki {
    856  1.74     isaki 	int unit;
    857   1.1       oki 	struct fd_softc *fd;
    858   1.1       oki 	struct fd_type *type;
    859  1.25   minoura 	struct fdc_softc *fdc;
    860   1.1       oki 
    861   1.1       oki 	unit = FDUNIT(dev);
    862   1.1       oki 	if (unit >= fd_cd.cd_ndevs)
    863   1.1       oki 		return ENXIO;
    864   1.1       oki 	fd = fd_cd.cd_devs[unit];
    865   1.1       oki 	if (fd == 0)
    866   1.1       oki 		return ENXIO;
    867   1.1       oki 	type = fd_dev_to_type(fd, dev);
    868   1.1       oki 	if (type == NULL)
    869   1.1       oki 		return ENXIO;
    870   1.1       oki 
    871   1.1       oki 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    872   1.1       oki 	    fd->sc_type != type)
    873   1.1       oki 		return EBUSY;
    874   1.1       oki 
    875  1.68   thorpej 	fdc = (void *)device_parent(&fd->sc_dev);
    876   1.1       oki 	if ((fd->sc_flags & FD_OPEN) == 0) {
    877   1.1       oki 		/* Lock eject button */
    878  1.25   minoura 		bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout,
    879  1.25   minoura 				  0x40 | ( 1 << unit));
    880  1.25   minoura 		bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, 0x40);
    881   1.1       oki 	}
    882   1.1       oki 
    883   1.1       oki 	fd->sc_type = type;
    884   1.1       oki 	fd->sc_cylin = -1;
    885   1.1       oki 
    886  1.14       oki 	switch (mode) {
    887   1.1       oki 	case S_IFCHR:
    888   1.1       oki 		fd->sc_flags |= FD_COPEN;
    889   1.1       oki 		break;
    890   1.1       oki 	case S_IFBLK:
    891   1.1       oki 		fd->sc_flags |= FD_BOPEN;
    892   1.1       oki 		break;
    893   1.1       oki 	}
    894   1.1       oki 
    895   1.1       oki 	fdgetdisklabel(fd, dev);
    896   1.1       oki 
    897   1.1       oki 	return 0;
    898   1.1       oki }
    899   1.1       oki 
    900   1.1       oki int
    901  1.66  christos fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    902   1.1       oki {
    903  1.74     isaki 	int unit = FDUNIT(dev);
    904  1.25   minoura 	struct fd_softc *fd = fd_cd.cd_devs[unit];
    905  1.68   thorpej 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
    906   1.1       oki 
    907   1.1       oki 	DPRINTF(("fdclose %d\n", unit));
    908   1.1       oki 
    909  1.14       oki 	switch (mode) {
    910   1.1       oki 	case S_IFCHR:
    911   1.1       oki 		fd->sc_flags &= ~FD_COPEN;
    912   1.1       oki 		break;
    913   1.1       oki 	case S_IFBLK:
    914   1.1       oki 		fd->sc_flags &= ~FD_BOPEN;
    915   1.1       oki 		break;
    916   1.1       oki 	}
    917   1.1       oki 
    918   1.1       oki 	if ((fd->sc_flags & FD_OPEN) == 0) {
    919  1.25   minoura 		bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout,
    920  1.25   minoura 				  ( 1 << unit));
    921  1.25   minoura 		bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, 0);
    922   1.1       oki 	}
    923   1.1       oki 	return 0;
    924   1.1       oki }
    925   1.1       oki 
    926   1.1       oki void
    927  1.63       chs fdcstart(struct fdc_softc *fdc)
    928   1.1       oki {
    929   1.1       oki 
    930   1.1       oki #ifdef DIAGNOSTIC
    931   1.1       oki 	/* only got here if controller's drive queue was inactive; should
    932   1.1       oki 	   be in idle state */
    933   1.1       oki 	if (fdc->sc_state != DEVIDLE) {
    934   1.8  christos 		printf("fdcstart: not idle\n");
    935   1.1       oki 		return;
    936   1.1       oki 	}
    937   1.1       oki #endif
    938  1.25   minoura 	(void) fdcintr(fdc);
    939   1.1       oki }
    940   1.1       oki 
    941   1.1       oki void
    942  1.64        he fdcstatus(struct device *dv, int n, const char *s)
    943   1.1       oki {
    944  1.68   thorpej 	struct fdc_softc *fdc = (void *)device_parent(dv);
    945  1.10       oki 	char bits[64];
    946   1.1       oki 
    947   1.1       oki 	if (n == 0) {
    948  1.25   minoura 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    949   1.1       oki 		(void) fdcresult(fdc);
    950   1.1       oki 		n = 2;
    951   1.1       oki 	}
    952   1.1       oki 
    953   1.8  christos 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    954   1.1       oki 
    955   1.1       oki 	switch (n) {
    956   1.1       oki 	case 0:
    957   1.8  christos 		printf("\n");
    958   1.1       oki 		break;
    959   1.1       oki 	case 2:
    960  1.10       oki 		printf(" (st0 %s cyl %d)\n",
    961  1.10       oki 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    962  1.10       oki 		    bits, sizeof(bits)), fdc->sc_status[1]);
    963   1.1       oki 		break;
    964   1.1       oki 	case 7:
    965  1.10       oki 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    966  1.10       oki 		    NE7_ST0BITS, bits, sizeof(bits)));
    967  1.10       oki 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    968  1.10       oki 		    NE7_ST1BITS, bits, sizeof(bits)));
    969  1.10       oki 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    970  1.10       oki 		    NE7_ST2BITS, bits, sizeof(bits)));
    971  1.10       oki 		printf(" cyl %d head %d sec %d)\n",
    972   1.1       oki 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    973   1.1       oki 		break;
    974   1.1       oki #ifdef DIAGNOSTIC
    975   1.1       oki 	default:
    976   1.8  christos 		printf(" fdcstatus: weird size: %d\n", n);
    977   1.1       oki 		break;
    978   1.1       oki #endif
    979   1.1       oki 	}
    980   1.1       oki }
    981   1.1       oki 
    982   1.1       oki void
    983  1.63       chs fdctimeout(void *arg)
    984   1.1       oki {
    985   1.1       oki 	struct fdc_softc *fdc = arg;
    986   1.1       oki 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    987   1.1       oki 	int s;
    988   1.1       oki 
    989   1.1       oki 	s = splbio();
    990   1.1       oki 	fdcstatus(&fd->sc_dev, 0, "timeout");
    991   1.1       oki 
    992  1.65      yamt 	if (BUFQ_PEEK(fd->sc_q) != NULL)
    993   1.1       oki 		fdc->sc_state++;
    994   1.1       oki 	else
    995   1.1       oki 		fdc->sc_state = DEVIDLE;
    996   1.1       oki 
    997  1.25   minoura 	(void) fdcintr(fdc);
    998   1.1       oki 	splx(s);
    999   1.1       oki }
   1000   1.1       oki 
   1001  1.25   minoura #if 0
   1002   1.1       oki void
   1003  1.63       chs fdcpseudointr(void *arg)
   1004   1.1       oki {
   1005   1.1       oki 	int s;
   1006  1.25   minoura 	struct fdc_softc *fdc = arg;
   1007   1.1       oki 
   1008   1.1       oki 	/* just ensure it has the right spl */
   1009   1.1       oki 	s = splbio();
   1010  1.25   minoura 	(void) fdcintr(fdc);
   1011   1.1       oki 	splx(s);
   1012   1.1       oki }
   1013  1.25   minoura #endif
   1014   1.1       oki 
   1015   1.1       oki int
   1016  1.63       chs fdcintr(void *arg)
   1017   1.1       oki {
   1018  1.25   minoura 	struct fdc_softc *fdc = arg;
   1019   1.1       oki #define	st0	fdc->sc_status[0]
   1020   1.1       oki #define	cyl	fdc->sc_status[1]
   1021   1.1       oki 	struct fd_softc *fd;
   1022   1.1       oki 	struct buf *bp;
   1023  1.25   minoura 	bus_space_tag_t iot = fdc->sc_iot;
   1024  1.25   minoura 	bus_space_handle_t ioh = fdc->sc_ioh;
   1025   1.1       oki 	int read, head, sec, pos, i, sectrac, nblks;
   1026   1.1       oki 	int	tmp;
   1027   1.1       oki 	struct fd_type *type;
   1028   1.1       oki 
   1029   1.1       oki loop:
   1030   1.1       oki 	fd = fdc->sc_drives.tqh_first;
   1031   1.1       oki 	if (fd == NULL) {
   1032   1.1       oki 		DPRINTF(("fdcintr: set DEVIDLE\n"));
   1033   1.1       oki 		if (fdc->sc_state == DEVIDLE) {
   1034  1.25   minoura 			if (intio_get_sicilian_intr() & SICILIAN_STAT_FDC) {
   1035  1.25   minoura 				out_fdc(iot, ioh, NE7CMD_SENSEI);
   1036  1.25   minoura 				if ((tmp = fdcresult(fdc)) != 2 ||
   1037  1.25   minoura 				    (st0 & 0xf8) != 0x20) {
   1038   1.1       oki 					goto loop;
   1039   1.1       oki 				}
   1040   1.1       oki 			}
   1041   1.1       oki 		}
   1042   1.1       oki 		/* no drives waiting; end */
   1043   1.1       oki 		fdc->sc_state = DEVIDLE;
   1044  1.74     isaki 		return 1;
   1045   1.1       oki 	}
   1046   1.1       oki 
   1047   1.1       oki 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1048  1.65      yamt 	bp = BUFQ_PEEK(fd->sc_q);
   1049   1.1       oki 	if (bp == NULL) {
   1050   1.1       oki 		fd->sc_ops = 0;
   1051   1.1       oki 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1052  1.29   thorpej 		fd->sc_active = 0;
   1053   1.1       oki 		goto loop;
   1054   1.1       oki 	}
   1055   1.1       oki 
   1056   1.1       oki 	switch (fdc->sc_state) {
   1057   1.1       oki 	case DEVIDLE:
   1058   1.1       oki 		DPRINTF(("fdcintr: in DEVIDLE\n"));
   1059   1.1       oki 		fdc->sc_errors = 0;
   1060   1.1       oki 		fd->sc_skip = 0;
   1061   1.1       oki 		fd->sc_bcount = bp->b_bcount;
   1062   1.1       oki 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1063  1.33   thorpej 		callout_stop(&fd->sc_motoroff_ch);
   1064   1.1       oki 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1065   1.1       oki 			fdc->sc_state = MOTORWAIT;
   1066   1.1       oki 			return 1;
   1067   1.1       oki 		}
   1068   1.1       oki 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1069   1.5       oki 			/* Turn on the motor */
   1070   1.5       oki 			/* being careful about other drives. */
   1071   1.5       oki 			for (i = 0; i < 4; i++) {
   1072   1.5       oki 				struct fd_softc *ofd = fdc->sc_fd[i];
   1073   1.5       oki 				if (ofd && ofd->sc_flags & FD_MOTOR) {
   1074  1.33   thorpej 					callout_stop(&ofd->sc_motoroff_ch);
   1075  1.48     isaki 					ofd->sc_flags &=
   1076  1.48     isaki 						~(FD_MOTOR | FD_MOTOR_WAIT);
   1077   1.5       oki 					break;
   1078   1.5       oki 				}
   1079   1.1       oki 			}
   1080   1.1       oki 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1081   1.1       oki 			fd_set_motor(fdc, 0);
   1082   1.1       oki 			fdc->sc_state = MOTORWAIT;
   1083   1.1       oki 			/* allow .5s for motor to stabilize */
   1084  1.33   thorpej 			callout_reset(&fd->sc_motoron_ch, hz / 2,
   1085  1.31   thorpej 			    fd_motor_on, fd);
   1086   1.1       oki 			return 1;
   1087   1.1       oki 		}
   1088   1.1       oki 		/* Make sure the right drive is selected. */
   1089   1.1       oki 		fd_set_motor(fdc, 0);
   1090   1.1       oki 
   1091   1.1       oki 		/* fall through */
   1092   1.1       oki 	case DOSEEK:
   1093   1.1       oki 	doseek:
   1094   1.1       oki 		DPRINTF(("fdcintr: in DOSEEK\n"));
   1095  1.29   thorpej 		if (fd->sc_cylin == bp->b_cylinder)
   1096   1.1       oki 			goto doio;
   1097   1.1       oki 
   1098  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
   1099  1.25   minoura 		out_fdc(iot, ioh, 0xd0);	/* XXX const */
   1100  1.25   minoura 		out_fdc(iot, ioh, 0x10);
   1101  1.25   minoura 
   1102  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
   1103  1.25   minoura 		out_fdc(iot, ioh, fd->sc_drive);	/* drive number */
   1104  1.29   thorpej 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
   1105   1.1       oki 
   1106   1.1       oki 		fd->sc_cylin = -1;
   1107   1.1       oki 		fdc->sc_state = SEEKWAIT;
   1108   1.1       oki 
   1109  1.70     blymn 		iostat_seek(fd->sc_dk.dk_stats);
   1110   1.1       oki 		disk_busy(&fd->sc_dk);
   1111   1.1       oki 
   1112  1.32   minoura 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1113   1.1       oki 		return 1;
   1114   1.1       oki 
   1115   1.1       oki 	case DOIO:
   1116   1.1       oki 	doio:
   1117   1.1       oki 		DPRINTF(("fdcintr: DOIO: "));
   1118   1.1       oki 		type = fd->sc_type;
   1119   1.1       oki 		sectrac = type->sectrac;
   1120   1.1       oki 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1121   1.1       oki 		sec = pos / (1 << (type->secsize - 2));
   1122   1.1       oki 		if (type->secsize == 2) {
   1123   1.1       oki 			fd->sc_part = SEC_P11;
   1124   1.1       oki 			nblks = (sectrac - sec) << (type->secsize - 2);
   1125   1.1       oki 			nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1126   1.1       oki 			DPRINTF(("nblks(0)"));
   1127   1.1       oki 		} else if ((fd->sc_blkno % 2) == 0) {
   1128   1.1       oki 			if (fd->sc_bcount & 0x00000200) {
   1129   1.1       oki 				if (fd->sc_bcount == FDC_BSIZE) {
   1130   1.1       oki 					fd->sc_part = SEC_P10;
   1131   1.1       oki 					nblks = 1;
   1132   1.1       oki 					DPRINTF(("nblks(1)"));
   1133   1.1       oki 				} else {
   1134   1.1       oki 					fd->sc_part = SEC_P11;
   1135   1.1       oki 					nblks = (sectrac - sec) * 2;
   1136   1.1       oki 					nblks = min(nblks, fd->sc_bcount
   1137   1.1       oki 						    / FDC_BSIZE - 1);
   1138   1.1       oki 					DPRINTF(("nblks(2)"));
   1139   1.1       oki 				}
   1140   1.1       oki 			} else {
   1141   1.1       oki 				fd->sc_part = SEC_P11;
   1142   1.1       oki 				nblks = (sectrac - sec)
   1143   1.1       oki 					<< (type->secsize - 2);
   1144   1.1       oki 				nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1145   1.1       oki 				DPRINTF(("nblks(3)"));
   1146   1.1       oki 			}
   1147   1.1       oki 		} else {
   1148   1.1       oki 			fd->sc_part = SEC_P01;
   1149   1.1       oki 			nblks = 1;
   1150   1.1       oki 			DPRINTF(("nblks(4)"));
   1151   1.1       oki 		}
   1152   1.1       oki 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1153   1.1       oki 		DPRINTF((" %d\n", nblks));
   1154   1.1       oki 		fd->sc_nblks = nblks;
   1155   1.1       oki 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1156   1.1       oki 		head = (fd->sc_blkno
   1157   1.1       oki 			% (type->seccyl * (1 << (type->secsize - 2))))
   1158   1.1       oki 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1159   1.1       oki 
   1160   1.1       oki #ifdef DIAGNOSTIC
   1161   1.1       oki 		{int block;
   1162   1.1       oki 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac
   1163   1.1       oki 			  + sec) * (1 << (type->secsize - 2));
   1164   1.1       oki 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1165   1.1       oki 		 if (block != fd->sc_blkno) {
   1166  1.48     isaki 			 printf("C H R N: %d %d %d %d\n",
   1167  1.48     isaki 				fd->sc_cylin, head, sec, type->secsize);
   1168  1.53     isaki 			 printf("fdcintr: doio: block %d != blkno %" PRId64 "\n",
   1169  1.48     isaki 				block, fd->sc_blkno);
   1170   1.1       oki #ifdef DDB
   1171   1.1       oki 			 Debugger();
   1172   1.1       oki #endif
   1173  1.48     isaki 		 }
   1174  1.48     isaki 		}
   1175   1.1       oki #endif
   1176   1.1       oki 		read = bp->b_flags & B_READ;
   1177  1.48     isaki 		DPRINTF(("fdcintr: %s drive %d track %d "
   1178  1.48     isaki 		         "head %d sec %d nblks %d, skip %d\n",
   1179   1.1       oki 			 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1180   1.1       oki 			 head, sec, nblks, fd->sc_skip));
   1181   1.1       oki 		DPRINTF(("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec,
   1182   1.1       oki 			 type->secsize));
   1183  1.74     isaki 
   1184   1.1       oki 		if (fd->sc_part != SEC_P11)
   1185   1.1       oki 			goto docopy;
   1186   1.1       oki 
   1187  1.73     isaki 		fdc_dmastart(fdc, read, (char *)bp->b_data + fd->sc_skip,
   1188  1.72        he 			     fd->sc_nbytes);
   1189   1.1       oki 		if (read)
   1190  1.25   minoura 			out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
   1191   1.1       oki 		else
   1192  1.25   minoura 			out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
   1193  1.25   minoura 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1194  1.29   thorpej 		out_fdc(iot, ioh, bp->b_cylinder);	/* cylinder */
   1195  1.25   minoura 		out_fdc(iot, ioh, head);
   1196  1.25   minoura 		out_fdc(iot, ioh, sec + 1);		/* sector +1 */
   1197  1.25   minoura 		out_fdc(iot, ioh, type->secsize);	/* sector size */
   1198  1.25   minoura 		out_fdc(iot, ioh, type->sectrac);	/* sectors/track */
   1199  1.25   minoura 		out_fdc(iot, ioh, type->gap1);		/* gap1 size */
   1200  1.25   minoura 		out_fdc(iot, ioh, type->datalen);	/* data length */
   1201   1.1       oki 		fdc->sc_state = IOCOMPLETE;
   1202   1.1       oki 
   1203   1.1       oki 		disk_busy(&fd->sc_dk);
   1204   1.1       oki 
   1205   1.1       oki 		/* allow 2 seconds for operation */
   1206  1.31   thorpej 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1207   1.1       oki 		return 1;				/* will return later */
   1208   1.1       oki 
   1209   1.1       oki 	case DOCOPY:
   1210   1.1       oki 	docopy:
   1211   1.1       oki 		DPRINTF(("fdcintr: DOCOPY:\n"));
   1212  1.59  jdolecek 		type = fd->sc_type;
   1213  1.59  jdolecek 		head = (fd->sc_blkno
   1214  1.59  jdolecek 			% (type->seccyl * (1 << (type->secsize - 2))))
   1215  1.59  jdolecek 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1216  1.59  jdolecek 		pos = fd->sc_blkno % (type->sectrac * (1 << (type->secsize - 2)));
   1217  1.59  jdolecek 		sec = pos / (1 << (type->secsize - 2));
   1218  1.25   minoura 		fdc_dmastart(fdc, B_READ, fd->sc_copybuf, 1024);
   1219  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_READ);		/* READ */
   1220  1.25   minoura 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1221  1.29   thorpej 		out_fdc(iot, ioh, bp->b_cylinder);	/* cylinder */
   1222  1.25   minoura 		out_fdc(iot, ioh, head);
   1223  1.25   minoura 		out_fdc(iot, ioh, sec + 1);		/* sector +1 */
   1224  1.25   minoura 		out_fdc(iot, ioh, type->secsize);	/* sector size */
   1225  1.25   minoura 		out_fdc(iot, ioh, type->sectrac);	/* sectors/track */
   1226  1.25   minoura 		out_fdc(iot, ioh, type->gap1);		/* gap1 size */
   1227  1.25   minoura 		out_fdc(iot, ioh, type->datalen);	/* data length */
   1228   1.1       oki 		fdc->sc_state = COPYCOMPLETE;
   1229   1.1       oki 		/* allow 2 seconds for operation */
   1230  1.31   thorpej 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1231   1.1       oki 		return 1;				/* will return later */
   1232   1.1       oki 
   1233   1.1       oki 	case DOIOHALF:
   1234   1.1       oki 	doiohalf:
   1235   1.1       oki 		DPRINTF((" DOIOHALF:\n"));
   1236   1.1       oki 
   1237   1.1       oki 		type = fd->sc_type;
   1238   1.1       oki 		sectrac = type->sectrac;
   1239   1.1       oki 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1240   1.1       oki 		sec = pos / (1 << (type->secsize - 2));
   1241   1.1       oki 		head = (fd->sc_blkno
   1242   1.1       oki 			% (type->seccyl * (1 << (type->secsize - 2))))
   1243   1.1       oki 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1244  1.59  jdolecek #ifdef DIAGNOSTIC
   1245   1.1       oki 		{int block;
   1246  1.48     isaki 		 block = ((fd->sc_cylin * type->heads + head) *
   1247  1.48     isaki 			 type->sectrac + sec)
   1248   1.1       oki 			 * (1 << (type->secsize - 2));
   1249   1.1       oki 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1250   1.1       oki 		 if (block != fd->sc_blkno) {
   1251  1.53     isaki 			 printf("fdcintr: block %d != blkno %" PRId64 "\n",
   1252  1.48     isaki 				block, fd->sc_blkno);
   1253   1.1       oki #ifdef DDB
   1254   1.1       oki 			 Debugger();
   1255   1.1       oki #endif
   1256  1.48     isaki 		 }
   1257  1.48     isaki 		}
   1258   1.1       oki #endif
   1259  1.28   minoura 		if ((read = bp->b_flags & B_READ)) {
   1260  1.73     isaki 			memcpy((char *)bp->b_data + fd->sc_skip, fd->sc_copybuf
   1261  1.40       wiz 			    + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1262  1.40       wiz 			    FDC_BSIZE);
   1263   1.1       oki 			fdc->sc_state = IOCOMPLETE;
   1264   1.1       oki 			goto iocomplete2;
   1265   1.1       oki 		} else {
   1266  1.73     isaki 			memcpy((char *)fd->sc_copybuf
   1267  1.40       wiz 			    + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1268  1.73     isaki 			    (char *)bp->b_data + fd->sc_skip, FDC_BSIZE);
   1269  1.25   minoura 			fdc_dmastart(fdc, read, fd->sc_copybuf, 1024);
   1270   1.1       oki 		}
   1271  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_WRITE);	/* WRITE */
   1272  1.25   minoura 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1273  1.29   thorpej 		out_fdc(iot, ioh, bp->b_cylinder);	/* cylinder */
   1274  1.25   minoura 		out_fdc(iot, ioh, head);
   1275  1.25   minoura 		out_fdc(iot, ioh, sec + 1);		/* sector +1 */
   1276  1.25   minoura 		out_fdc(iot, ioh, fd->sc_type->secsize); /* sector size */
   1277  1.25   minoura 		out_fdc(iot, ioh, sectrac);		/* sectors/track */
   1278  1.25   minoura 		out_fdc(iot, ioh, fd->sc_type->gap1);	/* gap1 size */
   1279  1.25   minoura 		out_fdc(iot, ioh, fd->sc_type->datalen); /* data length */
   1280   1.1       oki 		fdc->sc_state = IOCOMPLETE;
   1281   1.1       oki 		/* allow 2 seconds for operation */
   1282  1.31   thorpej 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1283   1.1       oki 		return 1;				/* will return later */
   1284   1.1       oki 
   1285   1.1       oki 	case SEEKWAIT:
   1286  1.31   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1287   1.1       oki 		fdc->sc_state = SEEKCOMPLETE;
   1288   1.1       oki 		/* allow 1/50 second for heads to settle */
   1289  1.25   minoura #if 0
   1290  1.31   thorpej 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
   1291  1.25   minoura #endif
   1292   1.1       oki 		return 1;
   1293   1.1       oki 
   1294   1.1       oki 	case SEEKCOMPLETE:
   1295   1.1       oki 		/* Make sure seek really happened */
   1296   1.1       oki 		DPRINTF(("fdcintr: SEEKCOMPLETE: FDC status = %x\n",
   1297  1.25   minoura 			 bus_space_read_1(fdc->sc_iot, fdc->sc_ioh, fdsts)));
   1298  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1299   1.1       oki 		tmp = fdcresult(fdc);
   1300   1.1       oki 		if ((st0 & 0xf8) == 0xc0) {
   1301   1.1       oki 			DPRINTF(("fdcintr: first seek!\n"));
   1302   1.1       oki 			fdc->sc_state = DORECAL;
   1303   1.1       oki 			goto loop;
   1304  1.25   minoura 		} else if (tmp != 2 ||
   1305  1.25   minoura 			   (st0 & 0xf8) != 0x20 ||
   1306  1.29   thorpej 			   cyl != bp->b_cylinder) {
   1307   1.1       oki #ifdef FDDEBUG
   1308   1.1       oki 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1309   1.1       oki #endif
   1310   1.1       oki 			fdcretry(fdc);
   1311   1.1       oki 			goto loop;
   1312   1.1       oki 		}
   1313  1.29   thorpej 		fd->sc_cylin = bp->b_cylinder;
   1314   1.1       oki 		goto doio;
   1315   1.1       oki 
   1316   1.1       oki 	case IOTIMEDOUT:
   1317   1.1       oki #if 0
   1318   1.1       oki 		isa_dmaabort(fdc->sc_drq);
   1319   1.1       oki #endif
   1320   1.1       oki 	case SEEKTIMEDOUT:
   1321   1.1       oki 	case RECALTIMEDOUT:
   1322   1.1       oki 	case RESETTIMEDOUT:
   1323   1.1       oki 		fdcretry(fdc);
   1324   1.1       oki 		goto loop;
   1325   1.1       oki 
   1326   1.1       oki 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1327  1.31   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1328   1.1       oki 		DPRINTF(("fdcintr: in IOCOMPLETE\n"));
   1329   1.1       oki 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1330   1.8  christos 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1331   1.1       oki #if 0
   1332   1.1       oki 			isa_dmaabort(fdc->sc_drq);
   1333   1.1       oki #endif
   1334   1.1       oki 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1335   1.1       oki 				  "read failed" : "write failed");
   1336  1.53     isaki 			printf("blkno %" PRId64 " nblks %d\n",
   1337   1.1       oki 			    fd->sc_blkno, fd->sc_nblks);
   1338   1.1       oki 			fdcretry(fdc);
   1339   1.1       oki 			goto loop;
   1340   1.1       oki 		}
   1341   1.1       oki #if 0
   1342   1.1       oki 		isa_dmadone(bp->b_flags & B_READ, bp->b_data + fd->sc_skip,
   1343   1.1       oki 		    nblks * FDC_BSIZE, fdc->sc_drq);
   1344   1.1       oki #endif
   1345   1.1       oki 	iocomplete2:
   1346   1.1       oki 		if (fdc->sc_errors) {
   1347  1.25   minoura 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
   1348   1.1       oki 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1349   1.8  christos 			printf("\n");
   1350   1.1       oki 			fdc->sc_errors = 0;
   1351   1.1       oki 		}
   1352   1.1       oki 		fd->sc_blkno += fd->sc_nblks;
   1353   1.1       oki 		fd->sc_skip += fd->sc_nbytes;
   1354   1.1       oki 		fd->sc_bcount -= fd->sc_nbytes;
   1355   1.1       oki 		DPRINTF(("fd->sc_bcount = %d\n", fd->sc_bcount));
   1356   1.1       oki 		if (fd->sc_bcount > 0) {
   1357  1.29   thorpej 			bp->b_cylinder = fd->sc_blkno
   1358   1.1       oki 				/ (fd->sc_type->seccyl
   1359   1.1       oki 				   * (1 << (fd->sc_type->secsize - 2)));
   1360   1.1       oki 			goto doseek;
   1361   1.1       oki 		}
   1362   1.1       oki 		fdfinish(fd, bp);
   1363   1.1       oki 		goto loop;
   1364   1.1       oki 
   1365   1.1       oki 	case COPYCOMPLETE: /* IO DONE, post-analyze */
   1366   1.1       oki 		DPRINTF(("fdcintr: COPYCOMPLETE:"));
   1367  1.31   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1368   1.1       oki 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1369   1.8  christos 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1370   1.1       oki #if 0
   1371   1.1       oki 			isa_dmaabort(fdc->sc_drq);
   1372   1.1       oki #endif
   1373   1.1       oki 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1374   1.1       oki 				  "read failed" : "write failed");
   1375  1.53     isaki 			printf("blkno %" PRId64 " nblks %d\n",
   1376   1.1       oki 			    fd->sc_blkno, fd->sc_nblks);
   1377   1.1       oki 			fdcretry(fdc);
   1378   1.1       oki 			goto loop;
   1379   1.1       oki 		}
   1380   1.1       oki 		goto doiohalf;
   1381   1.1       oki 
   1382   1.1       oki 	case DORESET:
   1383   1.1       oki 		DPRINTF(("fdcintr: in DORESET\n"));
   1384   1.1       oki 		/* try a reset, keep motor on */
   1385   1.1       oki 		fd_set_motor(fdc, 1);
   1386   1.1       oki 		DELAY(100);
   1387   1.1       oki 		fd_set_motor(fdc, 0);
   1388   1.1       oki 		fdc->sc_state = RESETCOMPLETE;
   1389  1.31   thorpej 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1390   1.1       oki 		return 1;			/* will return later */
   1391   1.1       oki 
   1392   1.1       oki 	case RESETCOMPLETE:
   1393   1.1       oki 		DPRINTF(("fdcintr: in RESETCOMPLETE\n"));
   1394  1.31   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1395   1.1       oki 		/* clear the controller output buffer */
   1396   1.1       oki 		for (i = 0; i < 4; i++) {
   1397  1.25   minoura 			out_fdc(iot, ioh, NE7CMD_SENSEI);
   1398   1.1       oki 			(void) fdcresult(fdc);
   1399   1.1       oki 		}
   1400   1.1       oki 
   1401   1.1       oki 		/* fall through */
   1402   1.1       oki 	case DORECAL:
   1403   1.1       oki 		DPRINTF(("fdcintr: in DORECAL\n"));
   1404  1.48     isaki 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
   1405  1.25   minoura 		out_fdc(iot, ioh, fd->sc_drive);
   1406   1.1       oki 		fdc->sc_state = RECALWAIT;
   1407  1.31   thorpej 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1408   1.1       oki 		return 1;			/* will return later */
   1409   1.1       oki 
   1410   1.1       oki 	case RECALWAIT:
   1411   1.1       oki 		DPRINTF(("fdcintr: in RECALWAIT\n"));
   1412  1.31   thorpej 		callout_stop(&fdc->sc_timo_ch);
   1413   1.1       oki 		fdc->sc_state = RECALCOMPLETE;
   1414   1.1       oki 		/* allow 1/30 second for heads to settle */
   1415  1.25   minoura #if 0
   1416  1.31   thorpej 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
   1417  1.25   minoura #endif
   1418   1.1       oki 		return 1;			/* will return later */
   1419   1.1       oki 
   1420   1.1       oki 	case RECALCOMPLETE:
   1421   1.1       oki 		DPRINTF(("fdcintr: in RECALCOMPLETE\n"));
   1422  1.25   minoura 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1423   1.1       oki 		tmp = fdcresult(fdc);
   1424   1.1       oki 		if ((st0 & 0xf8) == 0xc0) {
   1425   1.1       oki 			DPRINTF(("fdcintr: first seek!\n"));
   1426   1.1       oki 			fdc->sc_state = DORECAL;
   1427   1.1       oki 			goto loop;
   1428   1.1       oki 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1429   1.1       oki #ifdef FDDEBUG
   1430   1.1       oki 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1431   1.1       oki #endif
   1432   1.1       oki 			fdcretry(fdc);
   1433   1.1       oki 			goto loop;
   1434   1.1       oki 		}
   1435   1.1       oki 		fd->sc_cylin = 0;
   1436   1.1       oki 		goto doseek;
   1437   1.1       oki 
   1438   1.1       oki 	case MOTORWAIT:
   1439   1.1       oki 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1440   1.1       oki 			return 1;		/* time's not up yet */
   1441   1.1       oki 		goto doseek;
   1442   1.1       oki 
   1443   1.1       oki 	default:
   1444   1.1       oki 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1445   1.1       oki 		return 1;
   1446   1.1       oki 	}
   1447   1.1       oki #ifdef DIAGNOSTIC
   1448   1.1       oki 	panic("fdcintr: impossible");
   1449   1.1       oki #endif
   1450   1.1       oki #undef	st0
   1451   1.1       oki #undef	cyl
   1452   1.1       oki }
   1453   1.1       oki 
   1454   1.1       oki void
   1455  1.63       chs fdcretry(struct fdc_softc *fdc)
   1456   1.1       oki {
   1457   1.1       oki 	struct fd_softc *fd;
   1458   1.1       oki 	struct buf *bp;
   1459  1.11       oki 	char bits[64];
   1460   1.1       oki 
   1461   1.1       oki 	DPRINTF(("fdcretry:\n"));
   1462   1.1       oki 	fd = fdc->sc_drives.tqh_first;
   1463  1.65      yamt 	bp = BUFQ_PEEK(fd->sc_q);
   1464   1.1       oki 
   1465   1.1       oki 	switch (fdc->sc_errors) {
   1466   1.1       oki 	case 0:
   1467   1.1       oki 		/* try again */
   1468   1.1       oki 		fdc->sc_state = SEEKCOMPLETE;
   1469   1.1       oki 		break;
   1470   1.1       oki 
   1471   1.1       oki 	case 1: case 2: case 3:
   1472   1.1       oki 		/* didn't work; try recalibrating */
   1473   1.1       oki 		fdc->sc_state = DORECAL;
   1474   1.1       oki 		break;
   1475   1.1       oki 
   1476   1.1       oki 	case 4:
   1477   1.1       oki 		/* still no go; reset the bastard */
   1478   1.1       oki 		fdc->sc_state = DORESET;
   1479   1.1       oki 		break;
   1480   1.1       oki 
   1481   1.1       oki 	default:
   1482   1.1       oki 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1483   1.1       oki 			fd->sc_skip, (struct disklabel *)NULL);
   1484  1.10       oki 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1485  1.10       oki 						    NE7_ST0BITS, bits,
   1486  1.10       oki 						    sizeof(bits)));
   1487  1.10       oki 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1488  1.10       oki 						   NE7_ST1BITS, bits,
   1489  1.10       oki 						   sizeof(bits)));
   1490  1.10       oki 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1491  1.10       oki 						   NE7_ST2BITS, bits,
   1492  1.10       oki 						   sizeof(bits)));
   1493  1.10       oki 		printf(" cyl %d head %d sec %d)\n",
   1494  1.10       oki 		       fdc->sc_status[3],
   1495  1.10       oki 		       fdc->sc_status[4],
   1496  1.10       oki 		       fdc->sc_status[5]);
   1497   1.1       oki 
   1498   1.1       oki 		bp->b_error = EIO;
   1499   1.1       oki 		fdfinish(fd, bp);
   1500   1.1       oki 	}
   1501   1.1       oki 	fdc->sc_errors++;
   1502   1.1       oki }
   1503   1.1       oki 
   1504   1.1       oki int
   1505  1.71  christos fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1506   1.1       oki {
   1507   1.1       oki 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1508  1.73     isaki 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
   1509   1.1       oki 	int unit = FDUNIT(dev);
   1510  1.24    bouyer 	int part = DISKPART(dev);
   1511   1.1       oki 	struct disklabel buffer;
   1512   1.1       oki 	int error;
   1513   1.1       oki 
   1514   1.1       oki 	DPRINTF(("fdioctl:\n"));
   1515   1.1       oki 	switch (cmd) {
   1516   1.1       oki 	case DIOCGDINFO:
   1517   1.1       oki #if 1
   1518   1.1       oki 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   1519   1.1       oki 		return(0);
   1520   1.1       oki #else
   1521  1.25   minoura 		memset(&buffer, 0, sizeof(buffer));
   1522   1.1       oki 
   1523   1.1       oki 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1524   1.1       oki 		buffer.d_type = DTYPE_FLOPPY;
   1525   1.1       oki 		buffer.d_secsize = 128 << fd->sc_type->secsize;
   1526   1.1       oki 
   1527   1.1       oki 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1528   1.1       oki 			return EINVAL;
   1529   1.1       oki 
   1530   1.1       oki 		*(struct disklabel *)addr = buffer;
   1531   1.1       oki 		return 0;
   1532   1.1       oki #endif
   1533   1.1       oki 
   1534   1.1       oki 	case DIOCGPART:
   1535   1.1       oki 		((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
   1536   1.1       oki 		((struct partinfo *)addr)->part =
   1537  1.24    bouyer 		    &fd->sc_dk.dk_label->d_partitions[part];
   1538   1.1       oki 		return(0);
   1539   1.1       oki 
   1540   1.1       oki 	case DIOCWLABEL:
   1541   1.1       oki 		if ((flag & FWRITE) == 0)
   1542   1.1       oki 			return EBADF;
   1543   1.1       oki 		/* XXX do something */
   1544   1.1       oki 		return 0;
   1545   1.1       oki 
   1546   1.1       oki 	case DIOCWDINFO:
   1547   1.1       oki 		if ((flag & FWRITE) == 0)
   1548   1.1       oki 			return EBADF;
   1549   1.1       oki 
   1550  1.48     isaki 		error = setdisklabel(&buffer, (struct disklabel *)addr,
   1551  1.48     isaki 		                     0, NULL);
   1552   1.1       oki 		if (error)
   1553   1.1       oki 			return error;
   1554   1.1       oki 
   1555   1.1       oki 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1556   1.1       oki 		return error;
   1557   1.1       oki 
   1558   1.1       oki 	case DIOCLOCK:
   1559   1.1       oki 		/*
   1560   1.1       oki 		 * Nothing to do here, really.
   1561   1.1       oki 		 */
   1562   1.1       oki 		return 0; /* XXX */
   1563   1.1       oki 
   1564   1.1       oki 	case DIOCEJECT:
   1565  1.24    bouyer 		if (*(int *)addr == 0) {
   1566  1.24    bouyer 			/*
   1567  1.24    bouyer 			 * Don't force eject: check that we are the only
   1568  1.24    bouyer 			 * partition open. If so, unlock it.
   1569  1.24    bouyer 			 */
   1570  1.24    bouyer 			if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
   1571  1.24    bouyer 			    fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
   1572  1.24    bouyer 			    fd->sc_dk.dk_openmask) {
   1573  1.24    bouyer 				return (EBUSY);
   1574  1.24    bouyer 			}
   1575  1.24    bouyer 		}
   1576  1.24    bouyer 		/* FALLTHROUGH */
   1577  1.24    bouyer 	case ODIOCEJECT:
   1578  1.25   minoura 		fd_do_eject(fdc, unit);
   1579   1.1       oki 		return 0;
   1580   1.1       oki 
   1581   1.1       oki 	default:
   1582   1.1       oki 		return ENOTTY;
   1583   1.1       oki 	}
   1584   1.1       oki 
   1585   1.1       oki #ifdef DIAGNOSTIC
   1586   1.1       oki 	panic("fdioctl: impossible");
   1587   1.1       oki #endif
   1588   1.1       oki }
   1589   1.1       oki 
   1590   1.1       oki void
   1591  1.63       chs fd_do_eject(struct fdc_softc *fdc, int unit)
   1592   1.1       oki {
   1593  1.25   minoura 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout,
   1594  1.25   minoura 			  0x20 | ( 1 << unit));
   1595   1.1       oki 	DELAY(1); /* XXX */
   1596  1.25   minoura 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, 0x20);
   1597   1.1       oki }
   1598   1.1       oki 
   1599   1.1       oki /*
   1600   1.1       oki  * Build disk label. For now we only create a label from what we know
   1601   1.1       oki  * from 'sc'.
   1602   1.1       oki  */
   1603   1.1       oki static int
   1604  1.63       chs fdgetdisklabel(struct fd_softc *sc, dev_t dev)
   1605   1.1       oki {
   1606   1.1       oki 	struct disklabel *lp;
   1607   1.1       oki 	int part;
   1608   1.1       oki 
   1609  1.25   minoura 	DPRINTF(("fdgetdisklabel()\n"));
   1610   1.1       oki 
   1611   1.1       oki 	part = DISKPART(dev);
   1612   1.1       oki 	lp = sc->sc_dk.dk_label;
   1613  1.40       wiz 	memset(lp, 0, sizeof(struct disklabel));
   1614   1.1       oki 
   1615   1.1       oki 	lp->d_secsize     = 128 << sc->sc_type->secsize;
   1616   1.1       oki 	lp->d_ntracks     = sc->sc_type->heads;
   1617   1.1       oki 	lp->d_nsectors    = sc->sc_type->sectrac;
   1618   1.1       oki 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1619   1.1       oki 	lp->d_ncylinders  = sc->sc_type->size / lp->d_secpercyl;
   1620   1.1       oki 	lp->d_secperunit  = sc->sc_type->size;
   1621   1.1       oki 
   1622   1.1       oki 	lp->d_type        = DTYPE_FLOPPY;
   1623   1.1       oki 	lp->d_rpm         = 300; 	/* XXX */
   1624   1.1       oki 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1625   1.1       oki 	lp->d_bbsize      = 0;
   1626   1.1       oki 	lp->d_sbsize      = 0;
   1627   1.1       oki 	lp->d_npartitions = part + 1;
   1628   1.1       oki #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
   1629   1.1       oki 	lp->d_trkseek     = STEP_DELAY; /* XXX */
   1630   1.1       oki 	lp->d_magic       = DISKMAGIC;
   1631   1.1       oki 	lp->d_magic2      = DISKMAGIC;
   1632   1.1       oki 	lp->d_checksum    = dkcksum(lp);
   1633   1.1       oki 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1634   1.1       oki 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1635   1.1       oki 	lp->d_partitions[part].p_fsize  = 1024;
   1636   1.1       oki 	lp->d_partitions[part].p_frag   = 8;
   1637   1.1       oki 
   1638   1.1       oki 	return(0);
   1639   1.1       oki }
   1640  1.28   minoura 
   1641  1.28   minoura #include <dev/cons.h>
   1642   1.1       oki 
   1643  1.14       oki /*
   1644  1.14       oki  * Mountroot hook: prompt the user to enter the root file system
   1645  1.14       oki  * floppy.
   1646  1.14       oki  */
   1647   1.4       oki void
   1648  1.63       chs fd_mountroot_hook(struct device *dev)
   1649   1.4       oki {
   1650  1.73     isaki 	struct fd_softc *fd = (void *)dev;
   1651  1.73     isaki 	struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
   1652   1.4       oki 	int c;
   1653   1.4       oki 
   1654  1.69   thorpej 	/* XXX device_unit() abuse */
   1655  1.69   thorpej 	fd_do_eject(fdc, device_unit(dev));
   1656   1.8  christos 	printf("Insert filesystem floppy and press return.");
   1657   1.4       oki 	for (;;) {
   1658   1.4       oki 		c = cngetc();
   1659   1.4       oki 		if ((c == '\r') || (c == '\n')) {
   1660   1.8  christos 			printf("\n");
   1661  1.14       oki 			break;
   1662   1.4       oki 		}
   1663   1.4       oki 	}
   1664   1.4       oki }
   1665