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