Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.83
      1  1.83  christos /*	$NetBSD: fd.c,v 1.83 2015/01/02 19:42:05 christos Exp $	*/
      2   1.1       leo 
      3   1.1       leo /*
      4   1.1       leo  * Copyright (c) 1995 Leo Weppelman.
      5   1.1       leo  * All rights reserved.
      6   1.1       leo  *
      7   1.1       leo  * Redistribution and use in source and binary forms, with or without
      8   1.1       leo  * modification, are permitted provided that the following conditions
      9   1.1       leo  * are met:
     10   1.1       leo  * 1. Redistributions of source code must retain the above copyright
     11   1.1       leo  *    notice, this list of conditions and the following disclaimer.
     12   1.1       leo  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       leo  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       leo  *    documentation and/or other materials provided with the distribution.
     15   1.1       leo  *
     16   1.1       leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1       leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1       leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1       leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1       leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1       leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1       leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1       leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1       leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1       leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1       leo  */
     27   1.1       leo 
     28   1.1       leo /*
     29   1.1       leo  * This file contains a driver for the Floppy Disk Controller (FDC)
     30   1.1       leo  * on the Atari TT. It uses the WD 1772 chip, modified for steprates.
     31   1.1       leo  *
     32   1.1       leo  * The ST floppy disk controller shares the access to the DMA circuitry
     33   1.1       leo  * with other devices. For this reason the floppy disk controller makes
     34   1.1       leo  * use of some special DMA accessing code.
     35   1.1       leo  *
     36   1.1       leo  * Interrupts from the FDC are in fact DMA interrupts which get their
     37   1.1       leo  * first level handling in 'dma.c' . If the floppy driver is currently
     38   1.1       leo  * using DMA the interrupt is signalled to 'fdcint'.
     39   1.1       leo  *
     40   1.1       leo  * TODO:
     41   1.1       leo  *   - Test it with 2 drives (I don't have them)
     42   1.1       leo  *   - Test it with an HD-drive (Don't have that either)
     43   1.1       leo  *   - Finish ioctl's
     44   1.1       leo  */
     45  1.47     lukem 
     46  1.47     lukem #include <sys/cdefs.h>
     47  1.83  christos __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.83 2015/01/02 19:42:05 christos Exp $");
     48   1.1       leo 
     49  1.14   mycroft #include <sys/param.h>
     50  1.14   mycroft #include <sys/systm.h>
     51  1.35   thorpej #include <sys/callout.h>
     52  1.14   mycroft #include <sys/kernel.h>
     53  1.14   mycroft #include <sys/malloc.h>
     54  1.14   mycroft #include <sys/buf.h>
     55  1.49      yamt #include <sys/bufq.h>
     56  1.15       leo #include <sys/proc.h>
     57  1.14   mycroft #include <sys/device.h>
     58  1.14   mycroft #include <sys/ioctl.h>
     59  1.14   mycroft #include <sys/fcntl.h>
     60  1.14   mycroft #include <sys/conf.h>
     61  1.14   mycroft #include <sys/disklabel.h>
     62  1.14   mycroft #include <sys/disk.h>
     63  1.14   mycroft #include <sys/dkbad.h>
     64  1.14   mycroft #include <atari/atari/device.h>
     65  1.19       leo #include <atari/atari/stalloc.h>
     66  1.14   mycroft #include <machine/disklabel.h>
     67  1.14   mycroft #include <machine/iomap.h>
     68  1.14   mycroft #include <machine/mfp.h>
     69  1.14   mycroft #include <machine/dma.h>
     70  1.14   mycroft #include <machine/video.h>
     71  1.20       leo #include <machine/cpu.h>
     72  1.18       leo #include <atari/dev/ym2149reg.h>
     73  1.14   mycroft #include <atari/dev/fdreg.h>
     74   1.1       leo 
     75  1.75   tsutsui #include "ioconf.h"
     76  1.75   tsutsui 
     77   1.1       leo /*
     78   1.1       leo  * Be verbose for debugging
     79   1.1       leo  */
     80   1.4       leo /*#define FLP_DEBUG	1 */
     81   1.1       leo 
     82   1.1       leo #define	FDC_MAX_DMA_AD	0x1000000	/* No DMA possible beyond	*/
     83   1.1       leo 
     84   1.1       leo /* Parameters for the disk drive. */
     85   1.1       leo #define SECTOR_SIZE	512	/* physical sector size in bytes	*/
     86   1.1       leo #define NR_DRIVES	2	/* maximum number of drives		*/
     87   1.1       leo #define NR_TYPES	3	/* number of diskette/drive combinations*/
     88   1.1       leo #define MAX_ERRORS	10	/* how often to try rd/wt before quitting*/
     89   1.1       leo #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
     90   1.1       leo 
     91   1.1       leo 
     92   1.1       leo #define	INV_TRK		32000	/* Should fit in unsigned short		*/
     93   1.1       leo #define	INV_PART	NR_TYPES
     94   1.1       leo 
     95   1.1       leo /*
     96   1.1       leo  * Driver states
     97   1.1       leo  */
     98   1.1       leo #define	FLP_IDLE	0x00	/* floppy is idle			*/
     99   1.1       leo #define	FLP_MON		0x01	/* idle with motor on			*/
    100   1.1       leo #define	FLP_STAT	0x02	/* determine floppy status		*/
    101   1.1       leo #define	FLP_XFER	0x04	/* read/write data from floppy		*/
    102   1.1       leo 
    103   1.1       leo /*
    104   1.1       leo  * Timer delay's
    105   1.1       leo  */
    106   1.1       leo #define	FLP_MONDELAY	(3 * hz)	/* motor-on delay		*/
    107   1.1       leo #define	FLP_XFERDELAY	(2 * hz)	/* timeout on transfer		*/
    108   1.1       leo 
    109   1.8       leo /*
    110   1.8       leo  * The density codes
    111   1.8       leo  */
    112   1.8       leo #define	FLP_DD		0		/* Double density		*/
    113   1.8       leo #define	FLP_HD		1		/* High density			*/
    114   1.8       leo 
    115   1.1       leo 
    116   1.1       leo #define	b_block		b_resid		/* FIXME: this is not the place	*/
    117   1.1       leo 
    118   1.1       leo /*
    119   1.1       leo  * Global data for all physical floppy devices
    120   1.1       leo  */
    121   1.1       leo static short	selected = 0;		/* drive/head currently selected*/
    122   1.1       leo static short	motoron  = 0;		/* motor is spinning		*/
    123   1.1       leo static short	nopens   = 0;		/* Number of opens executed	*/
    124   1.1       leo 
    125   1.4       leo static short	fd_state = FLP_IDLE;	/* Current driver state		*/
    126  1.75   tsutsui static int	lock_stat = 0;		/* DMA locking status		*/
    127   1.1       leo static short	fd_cmd   = 0;		/* command being executed	*/
    128  1.75   tsutsui static const char *fd_error = NULL;	/* error from fd_xfer_ok()	*/
    129   1.1       leo 
    130   1.1       leo /*
    131   1.1       leo  * Private per device data
    132   1.1       leo  */
    133   1.1       leo struct fd_softc {
    134  1.74   tsutsui 	device_t	sc_dev;		/* generic device info		*/
    135  1.13   thorpej 	struct disk	dkdev;		/* generic disk info		*/
    136  1.51      yamt 	struct bufq_state *bufq;	/* queue of buf's		*/
    137  1.35   thorpej 	struct callout	sc_motor_ch;
    138   1.1       leo 	int		unit;		/* unit for atari controlling hw*/
    139   1.1       leo 	int		nheads;		/* number of heads in use	*/
    140   1.1       leo 	int		nsectors;	/* number of sectors/track	*/
    141   1.8       leo 	int		density;	/* density code			*/
    142   1.1       leo 	int		nblocks;	/* number of blocks on disk	*/
    143   1.1       leo 	int		curtrk;		/* track head positioned on	*/
    144   1.1       leo 	short		flags;		/* misc flags			*/
    145   1.1       leo 	short		part;		/* Current open partition	*/
    146   1.1       leo 	int		sector;		/* logical sector for I/O	*/
    147  1.75   tsutsui 	uint8_t		*io_data;	/* KVA for data transfer	*/
    148   1.1       leo 	int		io_bytes;	/* bytes left for I/O		*/
    149   1.1       leo 	int		io_dir;		/* B_READ/B_WRITE		*/
    150   1.1       leo 	int		errcnt;		/* current error count		*/
    151  1.75   tsutsui 	uint8_t		*bounceb;	/* Bounce buffer		*/
    152  1.10   mycroft 
    153   1.1       leo };
    154   1.1       leo 
    155   1.1       leo /*
    156   1.1       leo  * Flags in fd_softc:
    157   1.1       leo  */
    158   1.4       leo #define FLPF_NOTRESP	0x001		/* Unit not responding		*/
    159   1.4       leo #define FLPF_ISOPEN	0x002		/* Unit is open			*/
    160   1.8       leo #define FLPF_SPARE	0x004		/* Not used			*/
    161   1.4       leo #define FLPF_HAVELAB	0x008		/* We have a valid label	*/
    162   1.4       leo #define FLPF_BOUNCE	0x010		/* Now using the bounce buffer	*/
    163   1.4       leo #define FLPF_WRTPROT	0x020		/* Unit is write-protected	*/
    164   1.4       leo #define FLPF_EMPTY	0x040		/* Unit is empty		*/
    165   1.4       leo #define FLPF_INOPEN	0x080		/* Currently being opened	*/
    166   1.4       leo #define FLPF_GETSTAT	0x100		/* Getting unit status		*/
    167   1.1       leo 
    168   1.1       leo struct fd_types {
    169   1.1       leo 	int		nheads;		/* Heads in use			*/
    170   1.1       leo 	int		nsectors;	/* sectors per track		*/
    171   1.1       leo 	int		nblocks;	/* number of blocks		*/
    172   1.8       leo 	int		density;	/* density code			*/
    173  1.24       leo 	const char	*descr;		/* type description		*/
    174   1.1       leo } fdtypes[NR_TYPES] = {
    175  1.24       leo 		{ 1,  9,  720 , FLP_DD , "360KB" },	/* 360  Kb	*/
    176  1.24       leo 		{ 2,  9, 1440 , FLP_DD , "720KB" },	/* 720  Kb	*/
    177  1.24       leo 		{ 2, 18, 2880 , FLP_HD , "1.44MB" },	/* 1.44 Mb	*/
    178   1.1       leo };
    179   1.1       leo 
    180  1.30       leo #define	FLP_TYPE_360	0		/* XXX: Please keep these in	*/
    181  1.30       leo #define	FLP_TYPE_720	1		/* sync with the numbering in	*/
    182  1.30       leo #define	FLP_TYPE_144	2		/* 'fdtypes' right above!	*/
    183  1.30       leo 
    184  1.30       leo /*
    185  1.30       leo  * This is set only once at attach time. The value is determined by reading
    186  1.30       leo  * the configuration switches and is one of the FLP_TYPE_*'s.
    187  1.30       leo  * This is simular to the way Atari handles the _FLP cookie.
    188  1.30       leo  */
    189  1.30       leo static short	def_type = 0;		/* Reflects config-switches	*/
    190  1.30       leo 
    191  1.24       leo #define	FLP_DEFTYPE	1		/* 720Kb, reasonable default	*/
    192  1.30       leo #define	FLP_TYPE(dev)	( DISKPART(dev) == 0 ? def_type : DISKPART(dev) - 1 )
    193  1.24       leo 
    194  1.64       dsl typedef void	(*FPV)(void *);
    195   1.1       leo 
    196  1.24       leo dev_type_open(fdopen);
    197  1.15       leo dev_type_close(fdclose);
    198  1.15       leo dev_type_read(fdread);
    199  1.15       leo dev_type_write(fdwrite);
    200  1.15       leo dev_type_ioctl(fdioctl);
    201  1.39   gehenna dev_type_strategy(fdstrategy);
    202  1.15       leo 
    203  1.15       leo /*
    204   1.1       leo  * Private drive functions....
    205   1.1       leo  */
    206  1.64       dsl static void	fdstart(struct fd_softc *);
    207  1.64       dsl static void	fddone(struct fd_softc *);
    208  1.64       dsl static void	fdstatus(struct fd_softc *);
    209  1.64       dsl static void	fd_xfer(struct fd_softc *);
    210  1.64       dsl static void	fdcint(struct fd_softc *);
    211  1.64       dsl static int	fd_xfer_ok(struct fd_softc *);
    212  1.64       dsl static void	fdmotoroff(struct fd_softc *);
    213  1.64       dsl static void	fdminphys(struct buf *);
    214  1.64       dsl static void	fdtestdrv(struct fd_softc *);
    215  1.64       dsl static void	fdgetdefaultlabel(struct fd_softc *, struct disklabel *,
    216  1.64       dsl 		    int);
    217  1.64       dsl static int	fdgetdisklabel(struct fd_softc *, dev_t);
    218  1.64       dsl static int	fdselect(int, int, int);
    219  1.64       dsl static void	fddeselect(void);
    220  1.64       dsl static void	fdmoff(struct fd_softc *);
    221   1.1       leo 
    222  1.75   tsutsui static u_short rd_cfg_switch(void);
    223  1.75   tsutsui 
    224  1.75   tsutsui static inline uint8_t	read_fdreg(u_short);
    225  1.73   tsutsui static inline void	write_fdreg(u_short, u_short);
    226  1.75   tsutsui static inline uint8_t	read_dmastat(void);
    227  1.73   tsutsui 
    228  1.75   tsutsui static inline
    229  1.75   tsutsui uint8_t read_fdreg(u_short regno)
    230   1.4       leo {
    231  1.75   tsutsui 
    232   1.4       leo 	DMA->dma_mode = regno;
    233  1.75   tsutsui 	return DMA->dma_data;
    234   1.4       leo }
    235   1.4       leo 
    236  1.75   tsutsui static inline
    237  1.75   tsutsui void write_fdreg(u_short regno, u_short val)
    238   1.4       leo {
    239  1.75   tsutsui 
    240   1.4       leo 	DMA->dma_mode = regno;
    241   1.4       leo 	DMA->dma_data = val;
    242   1.4       leo }
    243   1.4       leo 
    244  1.75   tsutsui static inline
    245  1.75   tsutsui uint8_t read_dmastat(void)
    246   1.4       leo {
    247  1.75   tsutsui 
    248   1.4       leo 	DMA->dma_mode = FDC_CS | DMA_SCREG;
    249  1.75   tsutsui 	return DMA->dma_stat;
    250   1.4       leo }
    251   1.4       leo 
    252   1.1       leo /*
    253  1.30       leo  * Config switch stuff. Used only for the floppy type for now. That's
    254  1.30       leo  * why it's here...
    255  1.80       snj  * XXX: If needed in more places, it should be moved to its own include file.
    256  1.30       leo  * Note: This location _must_ be read as an u_short. Failure to do so
    257  1.30       leo  *       will return garbage!
    258  1.30       leo  */
    259  1.75   tsutsui static u_short
    260  1.75   tsutsui rd_cfg_switch(void)
    261  1.30       leo {
    262  1.75   tsutsui 
    263  1.75   tsutsui 	return *(volatile u_short *)AD_CFG_SWITCH;
    264  1.30       leo }
    265  1.30       leo 
    266  1.30       leo /*
    267  1.30       leo  * Switch definitions.
    268  1.30       leo  * Note: ON reads as a zero bit!
    269  1.30       leo  */
    270  1.30       leo #define	CFG_SWITCH_NOHD	0x4000
    271  1.30       leo 
    272  1.30       leo /*
    273   1.1       leo  * Autoconfig stuff....
    274   1.1       leo  */
    275  1.74   tsutsui static int	fdcmatch(device_t, cfdata_t, void *);
    276  1.64       dsl static int	fdcprint(void *, const char *);
    277  1.74   tsutsui static void	fdcattach(device_t, device_t, void *);
    278   1.1       leo 
    279  1.74   tsutsui CFATTACH_DECL_NEW(fdc, 0,
    280  1.42   thorpej     fdcmatch, fdcattach, NULL, NULL);
    281  1.16   thorpej 
    282  1.39   gehenna const struct bdevsw fd_bdevsw = {
    283  1.77  dholland 	.d_open = fdopen,
    284  1.77  dholland 	.d_close = fdclose,
    285  1.77  dholland 	.d_strategy = fdstrategy,
    286  1.77  dholland 	.d_ioctl = fdioctl,
    287  1.77  dholland 	.d_dump = nodump,
    288  1.77  dholland 	.d_psize = nosize,
    289  1.78  dholland 	.d_discard = nodiscard,
    290  1.77  dholland 	.d_flag = D_DISK
    291  1.39   gehenna };
    292  1.39   gehenna 
    293  1.39   gehenna const struct cdevsw fd_cdevsw = {
    294  1.77  dholland 	.d_open = fdopen,
    295  1.77  dholland 	.d_close = fdclose,
    296  1.77  dholland 	.d_read = fdread,
    297  1.77  dholland 	.d_write = fdwrite,
    298  1.77  dholland 	.d_ioctl = fdioctl,
    299  1.77  dholland 	.d_stop = nostop,
    300  1.77  dholland 	.d_tty = notty,
    301  1.77  dholland 	.d_poll = nopoll,
    302  1.77  dholland 	.d_mmap = nommap,
    303  1.77  dholland 	.d_kqfilter = nokqfilter,
    304  1.79  dholland 	.d_discard = nodiscard,
    305  1.77  dholland 	.d_flag = D_DISK
    306  1.39   gehenna };
    307  1.39   gehenna 
    308   1.1       leo static int
    309  1.74   tsutsui fdcmatch(device_t parent, cfdata_t match, void *aux)
    310   1.1       leo {
    311  1.75   tsutsui 	static int fdc_matched = 0;
    312  1.36       leo 
    313  1.36       leo 	/* Match only once */
    314  1.75   tsutsui 	if (strcmp("fdc", aux) || fdc_matched)
    315  1.75   tsutsui 		return 0;
    316  1.36       leo 	fdc_matched = 1;
    317  1.75   tsutsui 	return 1;
    318   1.1       leo }
    319   1.1       leo 
    320   1.1       leo static void
    321  1.74   tsutsui fdcattach(device_t parent, device_t self, void *aux)
    322   1.1       leo {
    323   1.1       leo 	struct fd_softc	fdsoftc;
    324  1.75   tsutsui 	int i, nfound, first_found;
    325   1.1       leo 
    326  1.15       leo 	nfound = first_found = 0;
    327  1.23  christos 	printf("\n");
    328   1.8       leo 	fddeselect();
    329  1.75   tsutsui 	for (i = 0; i < NR_DRIVES; i++) {
    330   1.1       leo 
    331   1.1       leo 		/*
    332   1.1       leo 		 * Test if unit is present
    333   1.1       leo 		 */
    334   1.1       leo 		fdsoftc.unit  = i;
    335   1.1       leo 		fdsoftc.flags = 0;
    336  1.15       leo 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdtestdrv, &fdsoftc,
    337  1.75   tsutsui 		    &lock_stat, 0);
    338   1.5       leo 		st_dmafree(&fdsoftc, &lock_stat);
    339   1.1       leo 
    340  1.75   tsutsui 		if ((fdsoftc.flags & FLPF_NOTRESP) == 0) {
    341  1.75   tsutsui 			if (nfound == 0)
    342  1.12       leo 				first_found = i;
    343   1.1       leo 			nfound++;
    344  1.75   tsutsui 			config_found(self, (void *)i, fdcprint);
    345   1.1       leo 		}
    346   1.1       leo 	}
    347   1.1       leo 
    348  1.75   tsutsui 	if (nfound != 0) {
    349  1.74   tsutsui 		struct fd_softc *fdsc =
    350  1.74   tsutsui 		    device_lookup_private(&fd_cd, first_found);
    351  1.12       leo 
    352  1.12       leo 		/*
    353  1.12       leo 		 * Make sure motor will be turned of when a floppy is
    354  1.12       leo 		 * inserted in the first selected drive.
    355  1.12       leo 		 */
    356  1.12       leo 		fdselect(first_found, 0, FLP_DD);
    357  1.12       leo 		fd_state = FLP_MON;
    358  1.35   thorpej 		callout_reset(&fdsc->sc_motor_ch, 0, (FPV)fdmotoroff, fdsc);
    359  1.12       leo 
    360   1.1       leo 		/*
    361   1.1       leo 		 * enable disk related interrupts
    362   1.1       leo 		 */
    363  1.29       leo 		MFP->mf_ierb |= IB_DINT;
    364  1.75   tsutsui 		MFP->mf_iprb  = (uint8_t)~IB_DINT;
    365  1.29       leo 		MFP->mf_imrb |= IB_DINT;
    366   1.1       leo 	}
    367   1.1       leo }
    368   1.1       leo 
    369   1.1       leo static int
    370  1.74   tsutsui fdcprint(void *aux, const char *pnp)
    371   1.1       leo {
    372  1.75   tsutsui 
    373  1.24       leo 	if (pnp != NULL)
    374  1.74   tsutsui 		aprint_normal("fd%d at %s:", (int)aux, pnp);
    375  1.24       leo 
    376  1.75   tsutsui 	return UNCONF;
    377   1.1       leo }
    378   1.1       leo 
    379  1.74   tsutsui static int	fdmatch(device_t, cfdata_t, void *);
    380  1.74   tsutsui static void	fdattach(device_t, device_t, void *);
    381  1.24       leo 
    382   1.1       leo struct dkdriver fddkdriver = { fdstrategy };
    383   1.1       leo 
    384  1.74   tsutsui CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
    385  1.42   thorpej     fdmatch, fdattach, NULL, NULL);
    386  1.16   thorpej 
    387   1.1       leo static int
    388  1.74   tsutsui fdmatch(device_t parent, cfdata_t match, void *aux)
    389   1.1       leo {
    390  1.75   tsutsui 
    391  1.75   tsutsui 	return 1;
    392   1.1       leo }
    393   1.1       leo 
    394   1.1       leo static void
    395  1.74   tsutsui fdattach(device_t parent, device_t self, void *aux)
    396   1.1       leo {
    397   1.1       leo 	struct fd_softc	*sc;
    398  1.30       leo 	struct fd_types *type;
    399  1.30       leo 	u_short		swtch;
    400   1.1       leo 
    401  1.74   tsutsui 	sc = device_private(self);
    402  1.74   tsutsui 	sc->sc_dev = self;
    403  1.30       leo 
    404  1.57        ad 	callout_init(&sc->sc_motor_ch, 0);
    405  1.35   thorpej 
    406  1.30       leo 	/*
    407  1.30       leo 	 * Find out if an Ajax chip might be installed. Set the default
    408  1.30       leo 	 * floppy type accordingly.
    409  1.30       leo 	 */
    410  1.30       leo 	swtch    = rd_cfg_switch();
    411  1.30       leo 	def_type = (swtch & CFG_SWITCH_NOHD) ? FLP_TYPE_720 : FLP_TYPE_144;
    412  1.30       leo 	type     = &fdtypes[def_type];
    413   1.1       leo 
    414  1.74   tsutsui 	aprint_normal(": %s %d cyl, %d head, %d sec\n", type->descr,
    415  1.75   tsutsui 	    type->nblocks / (type->nsectors * type->nheads), type->nheads,
    416  1.75   tsutsui 	    type->nsectors);
    417   1.1       leo 
    418  1.13   thorpej 	/*
    419  1.13   thorpej 	 * Initialize and attach the disk structure.
    420  1.13   thorpej 	 */
    421  1.74   tsutsui 	disk_init(&sc->dkdev, device_xname(sc->sc_dev), &fddkdriver);
    422  1.13   thorpej 	disk_attach(&sc->dkdev);
    423   1.1       leo }
    424   1.1       leo 
    425  1.15       leo int
    426  1.65       dsl fdioctl(dev_t dev, u_long cmd, void * addr, int flag, struct lwp *l)
    427   1.1       leo {
    428   1.1       leo 	struct fd_softc *sc;
    429  1.82  christos 	int error;
    430   1.1       leo 
    431  1.74   tsutsui 	sc = device_lookup_private(&fd_cd, DISKUNIT(dev));
    432  1.10   mycroft 
    433  1.75   tsutsui 	if ((sc->flags & FLPF_HAVELAB) == 0)
    434  1.75   tsutsui 		return EBADF;
    435   1.1       leo 
    436  1.81  christos 	error = disk_ioctl(&sc->dkdev, RAW_PART, cmd, addr, flag, l);
    437  1.81  christos 	if (error != EPASSTHROUGH)
    438  1.81  christos 		return error;
    439  1.81  christos 
    440  1.75   tsutsui 	switch (cmd) {
    441  1.75   tsutsui 	case DIOCSBAD:
    442  1.75   tsutsui 		return EINVAL;
    443   1.1       leo #ifdef notyet /* XXX LWP */
    444  1.75   tsutsui 	case DIOCSRETRIES:
    445  1.75   tsutsui 	case DIOCSSTEP:
    446  1.75   tsutsui 	case DIOCSDINFO:
    447  1.75   tsutsui 	case DIOCWDINFO:
    448  1.75   tsutsui 	case DIOCWLABEL:
    449  1.75   tsutsui 		break;
    450   1.1       leo #endif /* notyet */
    451  1.75   tsutsui 	case DIOCGDEFLABEL:
    452  1.75   tsutsui 		fdgetdefaultlabel(sc, (struct disklabel *)addr, RAW_PART);
    453  1.75   tsutsui 		return 0;
    454   1.1       leo 	}
    455  1.75   tsutsui 	return ENOTTY;
    456   1.1       leo }
    457   1.1       leo 
    458   1.1       leo /*
    459   1.1       leo  * Open the device. If this is the first open on both the floppy devices,
    460   1.1       leo  * intialize the controller.
    461   1.1       leo  * Note that partition info on the floppy device is used to distinguise
    462   1.1       leo  * between 780Kb and 360Kb floppy's.
    463   1.1       leo  *	partition 0: 360Kb
    464   1.3       leo  *	partition 1: 780Kb
    465   1.1       leo  */
    466  1.15       leo int
    467  1.66       dsl fdopen(dev_t dev, int flags, int devtype, struct lwp *l)
    468   1.1       leo {
    469   1.1       leo 	struct fd_softc	*sc;
    470  1.75   tsutsui 	int s;
    471   1.1       leo 
    472   1.1       leo #ifdef FLP_DEBUG
    473  1.24       leo 	printf("fdopen dev=0x%x\n", dev);
    474   1.1       leo #endif
    475   1.1       leo 
    476  1.75   tsutsui 	if (FLP_TYPE(dev) >= NR_TYPES)
    477  1.75   tsutsui 		return ENXIO;
    478   1.1       leo 
    479  1.75   tsutsui 	if ((sc = device_lookup_private(&fd_cd, DISKUNIT(dev))) == NULL)
    480  1.75   tsutsui 		return ENXIO;
    481   1.1       leo 
    482   1.1       leo 	/*
    483   1.1       leo 	 * If no floppy currently open, reset the controller and select
    484   1.1       leo 	 * floppy type.
    485   1.1       leo 	 */
    486  1.75   tsutsui 	if (nopens == 0) {
    487   1.1       leo 
    488   1.1       leo #ifdef FLP_DEBUG
    489  1.24       leo 		printf("fdopen device not yet open\n");
    490   1.1       leo #endif
    491   1.1       leo 		nopens++;
    492   1.4       leo 		write_fdreg(FDC_CS, IRUPT);
    493   1.8       leo 		delay(40);
    494   1.1       leo 	}
    495   1.1       leo 
    496   1.4       leo 	/*
    497   1.4       leo 	 * Sleep while other process is opening the device
    498   1.4       leo 	 */
    499  1.75   tsutsui 	s = splbio();
    500  1.75   tsutsui 	while (sc->flags & FLPF_INOPEN)
    501  1.54  christos 		tsleep((void *)sc, PRIBIO, "fdopen", 0);
    502  1.75   tsutsui 	splx(s);
    503   1.4       leo 
    504  1.76   tsutsui 	if ((sc->flags & FLPF_ISOPEN) == 0) {
    505   1.1       leo 		/*
    506   1.1       leo 		 * Initialise some driver values.
    507   1.1       leo 		 */
    508  1.75   tsutsui 		int type;
    509  1.75   tsutsui 		void *addr;
    510   1.1       leo 
    511  1.24       leo 		type = FLP_TYPE(dev);
    512  1.24       leo 
    513  1.51      yamt 		bufq_alloc(&sc->bufq, "disksort", BUFQ_SORT_RAWBLOCK);
    514   1.1       leo 		sc->unit        = DISKUNIT(dev);
    515  1.24       leo 		sc->part        = RAW_PART;
    516  1.24       leo 		sc->nheads	= fdtypes[type].nheads;
    517  1.24       leo 		sc->nsectors	= fdtypes[type].nsectors;
    518  1.24       leo 		sc->nblocks     = fdtypes[type].nblocks;
    519  1.24       leo 		sc->density	= fdtypes[type].density;
    520   1.1       leo 		sc->curtrk	= INV_TRK;
    521   1.1       leo 		sc->sector	= 0;
    522   1.1       leo 		sc->errcnt	= 0;
    523  1.75   tsutsui 		sc->bounceb	= alloc_stmem(SECTOR_SIZE, &addr);
    524  1.75   tsutsui 		if (sc->bounceb == NULL)
    525  1.75   tsutsui 			return ENOMEM; /* XXX */
    526   1.1       leo 
    527   1.4       leo 		/*
    528   1.4       leo 		 * Go get write protect + loaded status
    529   1.4       leo 		 */
    530   1.6       leo 		sc->flags |= FLPF_INOPEN|FLPF_GETSTAT;
    531  1.75   tsutsui 		s = splbio();
    532  1.15       leo 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstatus, sc,
    533  1.75   tsutsui 		    &lock_stat, 0);
    534  1.75   tsutsui 		while ((sc->flags & FLPF_GETSTAT) != 0)
    535  1.54  christos 			tsleep((void *)sc, PRIBIO, "fdopen", 0);
    536  1.75   tsutsui 		splx(s);
    537  1.54  christos 		wakeup((void *)sc);
    538   1.4       leo 
    539  1.75   tsutsui 		if ((sc->flags & FLPF_WRTPROT) != 0 &&
    540  1.75   tsutsui 		    (flags & FWRITE) != 0) {
    541   1.4       leo 			sc->flags = 0;
    542  1.75   tsutsui 			return EPERM;
    543   1.4       leo 		}
    544  1.75   tsutsui 		if ((sc->flags & FLPF_EMPTY) != 0) {
    545   1.4       leo 			sc->flags = 0;
    546  1.75   tsutsui 			return ENXIO;
    547   1.4       leo 		}
    548   1.6       leo 		sc->flags &= ~(FLPF_INOPEN|FLPF_GETSTAT);
    549   1.6       leo 		sc->flags |= FLPF_ISOPEN;
    550  1.75   tsutsui 	} else {
    551   1.1       leo 		/*
    552   1.1       leo 		 * Multiply opens are granted when accessing the same type of
    553   1.1       leo 		 * floppy (eq. the same partition).
    554   1.1       leo 		 */
    555  1.75   tsutsui 		if (sc->density != fdtypes[DISKPART(dev)].density)
    556  1.75   tsutsui 			return ENXIO;	/* XXX temporarely out of business */
    557   1.1       leo 	}
    558   1.1       leo 	fdgetdisklabel(sc, dev);
    559   1.1       leo #ifdef FLP_DEBUG
    560  1.24       leo 	printf("fdopen open succeeded on type %d\n", sc->part);
    561   1.1       leo #endif
    562  1.75   tsutsui 	return 0;
    563   1.1       leo }
    564   1.1       leo 
    565  1.15       leo int
    566  1.66       dsl fdclose(dev_t dev, int flags, int devtype, struct lwp *l)
    567   1.1       leo {
    568   1.1       leo 	struct fd_softc	*sc;
    569   1.1       leo 
    570  1.74   tsutsui 	sc = device_lookup_private(&fd_cd, DISKUNIT(dev));
    571   1.1       leo 	free_stmem(sc->bounceb);
    572   1.1       leo 	sc->flags = 0;
    573   1.1       leo 	nopens--;
    574   1.1       leo 
    575   1.1       leo #ifdef FLP_DEBUG
    576  1.23  christos 	printf("Closed floppy device -- nopens: %d\n", nopens);
    577   1.1       leo #endif
    578  1.75   tsutsui 	return 0;
    579   1.1       leo }
    580   1.1       leo 
    581   1.1       leo void
    582  1.65       dsl fdstrategy(struct buf *bp)
    583   1.1       leo {
    584  1.75   tsutsui 	struct fd_softc *sc;
    585  1.11       leo 	struct disklabel *lp;
    586  1.75   tsutsui 	int s, sz;
    587   1.1       leo 
    588  1.74   tsutsui 	sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev));
    589   1.1       leo 
    590   1.1       leo #ifdef FLP_DEBUG
    591  1.24       leo 	printf("fdstrategy: %p, b_bcount: %ld\n", bp, bp->b_bcount);
    592   1.1       leo #endif
    593   1.1       leo 
    594   1.1       leo 	/*
    595   1.1       leo 	 * check for valid partition and bounds
    596   1.1       leo 	 */
    597  1.13   thorpej 	lp = sc->dkdev.dk_label;
    598  1.11       leo 	if ((sc->flags & FLPF_HAVELAB) == 0) {
    599  1.11       leo 		bp->b_error = EIO;
    600  1.58        ad 		goto done;
    601   1.1       leo 	}
    602  1.75   tsutsui 	if (bp->b_blkno < 0 || (bp->b_bcount % SECTOR_SIZE) != 0) {
    603  1.24       leo 		bp->b_error = EINVAL;
    604  1.58        ad 		goto done;
    605  1.24       leo 	}
    606  1.24       leo 	if (bp->b_bcount == 0)
    607   1.1       leo 		goto done;
    608   1.1       leo 
    609  1.24       leo 	sz = howmany(bp->b_bcount, SECTOR_SIZE);
    610  1.24       leo 
    611  1.24       leo 	if (bp->b_blkno + sz > sc->nblocks) {
    612  1.24       leo 		sz = sc->nblocks - bp->b_blkno;
    613  1.24       leo 		if (sz == 0) /* Exactly at EndOfDisk */
    614  1.24       leo 			goto done;
    615  1.24       leo 		if (sz < 0) { /* Past EndOfDisk */
    616  1.24       leo 			bp->b_error = EINVAL;
    617  1.58        ad 			goto done;
    618  1.24       leo 		}
    619  1.24       leo 		/* Trucate it */
    620  1.24       leo 		if (bp->b_flags & B_RAW)
    621  1.24       leo 			bp->b_bcount = sz << DEV_BSHIFT;
    622  1.75   tsutsui 		else
    623  1.75   tsutsui 			bp->b_bcount = sz * lp->d_secsize;
    624  1.24       leo 	}
    625  1.32   thorpej 
    626  1.32   thorpej 	/* No partition translation. */
    627  1.32   thorpej 	bp->b_rawblkno = bp->b_blkno;
    628   1.1       leo 
    629   1.1       leo 	/*
    630   1.1       leo 	 * queue the buf and kick the low level code
    631   1.1       leo 	 */
    632  1.75   tsutsui 	s = splbio();
    633  1.63      yamt 	bufq_put(sc->bufq, bp);	/* XXX disksort_cylinder */
    634  1.11       leo 	if (!lock_stat) {
    635  1.11       leo 		if (fd_state & FLP_MON)
    636  1.35   thorpej 			callout_stop(&sc->sc_motor_ch);
    637   1.1       leo 		fd_state = FLP_IDLE;
    638  1.15       leo 		st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc,
    639  1.75   tsutsui 		    &lock_stat, 0);
    640   1.1       leo 	}
    641  1.75   tsutsui 	splx(s);
    642   1.1       leo 
    643   1.1       leo 	return;
    644   1.1       leo done:
    645   1.1       leo 	bp->b_resid = bp->b_bcount;
    646   1.1       leo 	biodone(bp);
    647   1.1       leo }
    648   1.1       leo 
    649   1.1       leo int
    650  1.65       dsl fdread(dev_t dev, struct uio *uio, int flags)
    651   1.1       leo {
    652  1.75   tsutsui 
    653  1.75   tsutsui 	return physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio);
    654   1.1       leo }
    655   1.1       leo 
    656   1.1       leo int
    657  1.65       dsl fdwrite(dev_t dev, struct uio *uio, int flags)
    658   1.1       leo {
    659  1.75   tsutsui 
    660  1.75   tsutsui 	return physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio);
    661   1.1       leo }
    662   1.1       leo 
    663   1.1       leo /*
    664   1.4       leo  * Called through DMA-dispatcher, get status.
    665   1.4       leo  */
    666   1.4       leo static void
    667  1.65       dsl fdstatus(struct fd_softc *sc)
    668   1.4       leo {
    669  1.75   tsutsui 
    670   1.4       leo #ifdef FLP_DEBUG
    671  1.23  christos 	printf("fdstatus\n");
    672   1.4       leo #endif
    673   1.4       leo 	sc->errcnt = 0;
    674   1.4       leo 	fd_state   = FLP_STAT;
    675   1.4       leo 	fd_xfer(sc);
    676   1.4       leo }
    677   1.4       leo 
    678   1.4       leo /*
    679  1.46       wiz  * Called through the DMA-dispatcher. So we know we are the only ones
    680  1.48       wiz  * messing with the floppy-controller.
    681   1.1       leo  * Initialize some fields in the fdsoftc for the state-machine and get
    682   1.1       leo  * it going.
    683   1.1       leo  */
    684   1.1       leo static void
    685  1.65       dsl fdstart(struct fd_softc *sc)
    686   1.1       leo {
    687  1.75   tsutsui 	struct buf *bp;
    688   1.1       leo 
    689  1.63      yamt 	bp	     = bufq_peek(sc->bufq);
    690   1.1       leo 	sc->sector   = bp->b_blkno;	/* Start sector for I/O		*/
    691   1.1       leo 	sc->io_data  = bp->b_data;	/* KVA base for I/O		*/
    692   1.1       leo 	sc->io_bytes = bp->b_bcount;	/* Transfer size in bytes	*/
    693   1.1       leo 	sc->io_dir   = bp->b_flags & B_READ;/* Direction of transfer	*/
    694   1.1       leo 	sc->errcnt   = 0;		/* No errors yet		*/
    695   1.1       leo 	fd_state     = FLP_XFER;	/* Yes, we're going to transfer	*/
    696   1.1       leo 
    697  1.13   thorpej 	/* Instrumentation. */
    698  1.13   thorpej 	disk_busy(&sc->dkdev);
    699  1.13   thorpej 
    700   1.1       leo 	fd_xfer(sc);
    701   1.1       leo }
    702   1.1       leo 
    703   1.1       leo /*
    704   1.1       leo  * The current transaction is finished (for good or bad). Let go of
    705  1.46       wiz  * the DMA-resources. Call biodone() to finish the transaction.
    706   1.1       leo  * Find a new transaction to work on.
    707   1.1       leo  */
    708   1.1       leo static void
    709  1.65       dsl fddone(register struct fd_softc *sc)
    710   1.1       leo {
    711  1.75   tsutsui 	struct buf *bp;
    712   1.1       leo 	struct fd_softc	*sc1;
    713  1.75   tsutsui 	int i, s;
    714   1.1       leo 
    715   1.1       leo 	/*
    716  1.46       wiz 	 * Give others a chance to use the DMA.
    717   1.1       leo 	 */
    718   1.5       leo 	st_dmafree(sc, &lock_stat);
    719   1.4       leo 
    720   1.1       leo 
    721  1.75   tsutsui 	if (fd_state != FLP_STAT) {
    722   1.4       leo 		/*
    723   1.4       leo 		 * Finish current transaction.
    724   1.4       leo 		 */
    725  1.75   tsutsui 		s = splbio();
    726  1.63      yamt 		bp = bufq_get(sc->bufq);
    727  1.31   thorpej 		if (bp == NULL)
    728   1.4       leo 			panic("fddone");
    729  1.75   tsutsui 		splx(s);
    730   1.4       leo 
    731   1.4       leo #ifdef FLP_DEBUG
    732  1.75   tsutsui 		printf("fddone: unit: %d, buf: %p, resid: %d\n",sc->unit, bp,
    733  1.75   tsutsui 		    sc->io_bytes);
    734   1.4       leo #endif
    735   1.4       leo 		bp->b_resid = sc->io_bytes;
    736  1.13   thorpej 
    737  1.44       mrg 		disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid),
    738  1.44       mrg 		    (bp->b_flags & B_READ));
    739  1.13   thorpej 
    740   1.4       leo 		biodone(bp);
    741   1.4       leo 	}
    742   1.4       leo 	fd_state = FLP_MON;
    743   1.1       leo 
    744  1.75   tsutsui 	if (lock_stat)
    745   1.1       leo 		return;		/* XXX Is this possible?	*/
    746   1.1       leo 
    747   1.1       leo 	/*
    748   1.1       leo 	 * Find a new transaction on round-robin basis.
    749   1.1       leo 	 */
    750  1.75   tsutsui 	for (i = sc->unit + 1;; i++) {
    751  1.75   tsutsui 		if (i >= fd_cd.cd_ndevs)
    752   1.1       leo 			i = 0;
    753  1.75   tsutsui 		if ((sc1 = device_lookup_private(&fd_cd, i)) == NULL)
    754   1.1       leo 			continue;
    755  1.63      yamt 		if (bufq_peek(sc1->bufq) != NULL)
    756   1.1       leo 			break;
    757  1.75   tsutsui 		if (i == sc->unit) {
    758  1.35   thorpej 			callout_reset(&sc->sc_motor_ch, FLP_MONDELAY,
    759  1.35   thorpej 			    (FPV)fdmotoroff, sc);
    760   1.1       leo #ifdef FLP_DEBUG
    761  1.23  christos 			printf("fddone: Nothing to do\n");
    762   1.1       leo #endif
    763   1.1       leo 			return;	/* No work */
    764   1.1       leo 		}
    765   1.1       leo 	}
    766   1.1       leo 	fd_state = FLP_IDLE;
    767   1.1       leo #ifdef FLP_DEBUG
    768  1.23  christos 	printf("fddone: Staring job on unit %d\n", sc1->unit);
    769   1.1       leo #endif
    770  1.15       leo 	st_dmagrab((dma_farg)fdcint, (dma_farg)fdstart, sc1, &lock_stat, 0);
    771   1.1       leo }
    772   1.1       leo 
    773   1.8       leo static int
    774  1.66       dsl fdselect(int drive, int head, int dense)
    775   1.8       leo {
    776  1.75   tsutsui 	int i, spinning;
    777  1.75   tsutsui 
    778   1.8       leo #ifdef FLP_DEBUG
    779  1.23  christos 	printf("fdselect: drive=%d, head=%d, dense=%d\n", drive, head, dense);
    780   1.8       leo #endif
    781   1.8       leo 	i = ((drive == 1) ? PA_FLOP1 : PA_FLOP0) | head;
    782   1.8       leo 	spinning = motoron;
    783   1.8       leo 	motoron  = 1;
    784   1.8       leo 
    785  1.75   tsutsui 	switch (dense) {
    786  1.75   tsutsui 	case FLP_DD:
    787  1.75   tsutsui 		DMA->dma_drvmode = 0;
    788  1.75   tsutsui 		break;
    789  1.75   tsutsui 	case FLP_HD:
    790  1.75   tsutsui 		DMA->dma_drvmode = (FDC_HDSET|FDC_HDSIG);
    791  1.75   tsutsui 		break;
    792  1.75   tsutsui 	default:
    793  1.75   tsutsui 		panic("fdselect: unknown density code");
    794   1.8       leo 	}
    795  1.75   tsutsui 	if (i != selected) {
    796   1.8       leo 		selected = i;
    797  1.20       leo 		ym2149_fd_select((i ^ PA_FDSEL));
    798   1.8       leo 	}
    799  1.75   tsutsui 	return spinning;
    800   1.8       leo }
    801   1.8       leo 
    802   1.8       leo static void
    803  1.67    cegger fddeselect(void)
    804   1.8       leo {
    805  1.75   tsutsui 
    806  1.18       leo 	ym2149_fd_select(PA_FDSEL);
    807   1.8       leo 	motoron = selected = 0;
    808   1.8       leo 	DMA->dma_drvmode   = 0;
    809   1.8       leo }
    810   1.8       leo 
    811   1.1       leo /****************************************************************************
    812   1.1       leo  * The following functions assume to be running as a result of a            *
    813   1.1       leo  * disk-interrupt (e.q. spl = splbio).				            *
    814   1.1       leo  * They form the finit-state machine, the actual driver.                    *
    815   1.1       leo  *                                                                          *
    816   1.1       leo  *	fdstart()/ --> fd_xfer() -> activate hardware                       *
    817   1.1       leo  *  fdopen()          ^                                                     *
    818   1.1       leo  *                    |                                                     *
    819   1.1       leo  *                    +-- not ready -<------------+                         *
    820   1.1       leo  *                                                |                         *
    821   1.1       leo  *  fdmotoroff()/ --> fdcint() -> fd_xfer_ok() ---+                         *
    822   1.1       leo  *  h/w interrupt                 |                                         *
    823   1.1       leo  *                               \|/                                        *
    824   1.1       leo  *                            finished ---> fdone()                         *
    825   1.1       leo  *                                                                          *
    826   1.1       leo  ****************************************************************************/
    827   1.1       leo static void
    828  1.65       dsl fd_xfer(struct fd_softc *sc)
    829   1.1       leo {
    830  1.75   tsutsui 	int head;
    831  1.75   tsutsui 	int track, sector, hbit;
    832  1.75   tsutsui 	paddr_t phys_addr;
    833   1.1       leo 
    834  1.15       leo 	head = track = 0;
    835  1.75   tsutsui 	switch (fd_state) {
    836  1.75   tsutsui 	case FLP_XFER:
    837   1.4       leo 		/*
    838   1.4       leo 		 * Calculate head/track values
    839   1.4       leo 		 */
    840   1.4       leo 		track  = sc->sector / sc->nsectors;
    841   1.4       leo 		head   = track % sc->nheads;
    842   1.4       leo 		track  = track / sc->nheads;
    843   1.1       leo #ifdef FLP_DEBUG
    844  1.75   tsutsui 		printf("fd_xfer: sector:%d,head:%d,track:%d\n",
    845  1.75   tsutsui 		    sc->sector, head, track);
    846   1.1       leo #endif
    847   1.4       leo 		break;
    848   1.4       leo 
    849  1.75   tsutsui 	case FLP_STAT:
    850   1.4       leo 		/*
    851   1.4       leo 		 * FLP_STAT only wants to recalibrate
    852   1.4       leo 		 */
    853   1.4       leo 		sc->curtrk = INV_TRK;
    854   1.4       leo 		break;
    855  1.75   tsutsui 	default:
    856   1.4       leo 		panic("fd_xfer: wrong state (0x%x)", fd_state);
    857   1.4       leo 	}
    858   1.1       leo 
    859   1.1       leo 	/*
    860   1.8       leo 	 * Select the drive.
    861   1.1       leo 	 */
    862   1.8       leo 	hbit = fdselect(sc->unit, head, sc->density) ? HBIT : 0;
    863   1.1       leo 
    864  1.75   tsutsui 	if (sc->curtrk == INV_TRK) {
    865  1.10   mycroft 		/*
    866   1.1       leo 		 * Recalibrate, since we lost track of head positioning.
    867   1.1       leo 		 * The floppy disk controller has no way of determining its
    868   1.1       leo 		 * absolute arm position (track).  Instead, it steps the
    869   1.1       leo 		 * arm a track at a time and keeps track of where it
    870   1.1       leo 		 * thinks it is (in software).  However, after a SEEK, the
    871   1.1       leo 		 * hardware reads information from the diskette telling
    872   1.1       leo 		 * where the arm actually is.  If the arm is in the wrong place,
    873   1.1       leo 		 * a recalibration is done, which forces the arm to track 0.
    874   1.1       leo 		 * This way the controller can get back into sync with reality.
    875   1.1       leo 		 */
    876   1.8       leo 		fd_cmd = RESTORE;
    877   1.4       leo 		write_fdreg(FDC_CS, RESTORE|VBIT|hbit);
    878  1.35   thorpej 		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
    879  1.35   thorpej 		    (FPV)fdmotoroff, sc);
    880   1.1       leo 
    881   1.1       leo #ifdef FLP_DEBUG
    882  1.23  christos 		printf("fd_xfer:Recalibrating drive %d\n", sc->unit);
    883   1.1       leo #endif
    884   1.1       leo 		return;
    885   1.1       leo 	}
    886   1.1       leo 
    887   1.4       leo 	write_fdreg(FDC_TR, sc->curtrk);
    888   1.1       leo 
    889   1.1       leo 	/*
    890   1.1       leo 	 * Issue a SEEK command on the indicated drive unless the arm is
    891   1.1       leo 	 * already positioned on the correct track.
    892   1.1       leo 	 */
    893  1.75   tsutsui 	if (track != sc->curtrk) {
    894   1.1       leo 		sc->curtrk = track;	/* be optimistic */
    895   1.4       leo 		write_fdreg(FDC_DR, track);
    896   1.4       leo 		write_fdreg(FDC_CS, SEEK|RATE6|VBIT|hbit);
    897  1.35   thorpej 		callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY,
    898  1.35   thorpej 		    (FPV)fdmotoroff, sc);
    899   1.1       leo 		fd_cmd = SEEK;
    900   1.1       leo #ifdef FLP_DEBUG
    901  1.75   tsutsui 		printf("fd_xfer:Seek to track %d on drive %d\n",
    902  1.75   tsutsui 		    track, sc->unit);
    903   1.1       leo #endif
    904   1.1       leo 		return;
    905   1.1       leo 	}
    906   1.1       leo 
    907   1.1       leo 	/*
    908   1.1       leo 	 * The drive is now on the proper track. Read or write 1 block.
    909   1.1       leo 	 */
    910   1.1       leo 	sector = sc->sector % sc->nsectors;
    911   1.1       leo 	sector++;	/* start numbering at 1 */
    912   1.1       leo 
    913   1.4       leo 	write_fdreg(FDC_SR, sector);
    914   1.1       leo 
    915  1.75   tsutsui 	phys_addr = (paddr_t)kvtop(sc->io_data);
    916  1.75   tsutsui 	if (phys_addr >= FDC_MAX_DMA_AD) {
    917   1.1       leo 		/*
    918   1.1       leo 		 * We _must_ bounce this address
    919   1.1       leo 		 */
    920  1.75   tsutsui 		phys_addr = (paddr_t)kvtop(sc->bounceb);
    921  1.75   tsutsui 		if (sc->io_dir == B_WRITE)
    922  1.70   tsutsui 			memcpy(sc->bounceb, sc->io_data, SECTOR_SIZE);
    923   1.1       leo 		sc->flags |= FLPF_BOUNCE;
    924   1.1       leo 	}
    925  1.54  christos 	st_dmaaddr_set((void *)phys_addr);	/* DMA address setup */
    926   1.1       leo 
    927   1.1       leo #ifdef FLP_DEBUG
    928  1.24       leo 	printf("fd_xfer:Start io (io_addr:%lx)\n", (u_long)kvtop(sc->io_data));
    929   1.1       leo #endif
    930   1.1       leo 
    931  1.75   tsutsui 	if (sc->io_dir == B_READ) {
    932   1.1       leo 		/* Issue the command */
    933   1.4       leo 		st_dmacomm(DMA_FDC | DMA_SCREG, 1);
    934   1.4       leo 		write_fdreg(FDC_CS, F_READ|hbit);
    935   1.1       leo 		fd_cmd = F_READ;
    936  1.75   tsutsui 	} else {
    937   1.1       leo 		/* Issue the command */
    938   1.4       leo 		st_dmacomm(DMA_WRBIT | DMA_FDC | DMA_SCREG, 1);
    939   1.4       leo 		write_fdreg(DMA_WRBIT | FDC_CS, F_WRITE|hbit|EBIT|PBIT);
    940   1.1       leo 		fd_cmd = F_WRITE;
    941   1.1       leo 	}
    942  1.35   thorpej 	callout_reset(&sc->sc_motor_ch, FLP_XFERDELAY, (FPV)fdmotoroff, sc);
    943   1.1       leo }
    944   1.1       leo 
    945   1.1       leo /* return values of fd_xfer_ok(): */
    946   1.1       leo #define X_OK			0
    947   1.1       leo #define X_AGAIN			1
    948   1.1       leo #define X_ERROR			2
    949   1.1       leo #define X_FAIL			3
    950   1.1       leo 
    951   1.1       leo /*
    952   1.1       leo  * Hardware interrupt function.
    953   1.1       leo  */
    954   1.4       leo static void
    955  1.65       dsl fdcint(struct fd_softc *sc)
    956   1.1       leo {
    957  1.75   tsutsui 	struct buf *bp;
    958   1.1       leo 
    959   1.1       leo #ifdef FLP_DEBUG
    960  1.23  christos 	printf("fdcint: unit = %d\n", sc->unit);
    961   1.1       leo #endif
    962   1.1       leo 
    963   1.1       leo 	/*
    964   1.1       leo 	 * Cancel timeout (we made it, didn't we)
    965   1.1       leo 	 */
    966  1.35   thorpej 	callout_stop(&sc->sc_motor_ch);
    967   1.1       leo 
    968  1.75   tsutsui 	switch (fd_xfer_ok(sc)) {
    969  1.75   tsutsui 	case X_ERROR:
    970  1.75   tsutsui 		if (++sc->errcnt < MAX_ERRORS) {
    971   1.1       leo 			/*
    972  1.75   tsutsui 			 * Command failed but still retries left.
    973   1.1       leo 			 */
    974  1.75   tsutsui 			break;
    975  1.75   tsutsui 		}
    976  1.75   tsutsui 		/* FALL THROUGH */
    977  1.75   tsutsui 	case X_FAIL:
    978  1.75   tsutsui 		/*
    979  1.75   tsutsui 		 * Non recoverable error. Fall back to motor-on
    980  1.75   tsutsui 		 * idle-state.
    981  1.75   tsutsui 		 */
    982  1.75   tsutsui 		if (fd_error != NULL) {
    983  1.75   tsutsui 			printf("Floppy error: %s\n", fd_error);
    984  1.75   tsutsui 			fd_error = NULL;
    985  1.75   tsutsui 		}
    986   1.4       leo 
    987  1.75   tsutsui 		if (fd_state == FLP_STAT) {
    988  1.75   tsutsui 			sc->flags |= FLPF_EMPTY;
    989  1.75   tsutsui 			sc->flags &= ~FLPF_GETSTAT;
    990  1.75   tsutsui 			wakeup((void *)sc);
    991  1.75   tsutsui 			fddone(sc);
    992  1.75   tsutsui 			return;
    993  1.75   tsutsui 		}
    994   1.1       leo 
    995  1.75   tsutsui 		bp = bufq_peek(sc->bufq);
    996  1.75   tsutsui 
    997  1.75   tsutsui 		bp->b_error  = EIO;
    998  1.75   tsutsui 		fd_state     = FLP_MON;
    999  1.75   tsutsui 
   1000  1.75   tsutsui 		break;
   1001  1.75   tsutsui 	case X_AGAIN:
   1002  1.75   tsutsui 		/*
   1003  1.75   tsutsui 		 * Start next part of state machine.
   1004  1.75   tsutsui 		 */
   1005  1.75   tsutsui 		break;
   1006  1.75   tsutsui 	case X_OK:
   1007  1.75   tsutsui 		/*
   1008  1.75   tsutsui 		 * Command ok and finished. Reset error-counter.
   1009  1.75   tsutsui 		 * If there are no more bytes to transfer fall back
   1010  1.75   tsutsui 		 * to motor-on idle state.
   1011  1.75   tsutsui 		 */
   1012  1.75   tsutsui 		sc->errcnt = 0;
   1013   1.1       leo 
   1014  1.75   tsutsui 		if (fd_state == FLP_STAT) {
   1015  1.75   tsutsui 			sc->flags &= ~FLPF_GETSTAT;
   1016  1.75   tsutsui 			wakeup((void *)sc);
   1017  1.75   tsutsui 			fddone(sc);
   1018  1.75   tsutsui 			return;
   1019  1.75   tsutsui 		}
   1020   1.4       leo 
   1021  1.75   tsutsui 		if ((sc->flags & FLPF_BOUNCE) != 0 &&
   1022  1.75   tsutsui 		    sc->io_dir == B_READ)
   1023  1.75   tsutsui 			memcpy(sc->io_data, sc->bounceb, SECTOR_SIZE);
   1024  1.75   tsutsui 		sc->flags &= ~FLPF_BOUNCE;
   1025  1.75   tsutsui 
   1026  1.75   tsutsui 		sc->sector++;
   1027  1.75   tsutsui 		sc->io_data  += SECTOR_SIZE;
   1028  1.75   tsutsui 		sc->io_bytes -= SECTOR_SIZE;
   1029  1.75   tsutsui 		if (sc->io_bytes <= 0)
   1030  1.75   tsutsui 			fd_state = FLP_MON;
   1031   1.1       leo 	}
   1032  1.75   tsutsui 	if (fd_state == FLP_MON)
   1033   1.1       leo 		fddone(sc);
   1034  1.75   tsutsui 	else
   1035  1.75   tsutsui 		fd_xfer(sc);
   1036   1.1       leo }
   1037   1.1       leo 
   1038   1.1       leo /*
   1039   1.1       leo  * Determine status of last command. Should only be called through
   1040   1.1       leo  * 'fdcint()'.
   1041   1.1       leo  * Returns:
   1042   1.1       leo  *	X_ERROR : Error on command; might succeed next time.
   1043   1.1       leo  *	X_FAIL  : Error on command; will never succeed.
   1044   1.1       leo  *	X_AGAIN : Part of a command succeeded, call 'fd_xfer()' to complete.
   1045   1.1       leo  *	X_OK	: Command succeeded and is complete.
   1046   1.1       leo  *
   1047   1.1       leo  * This function only affects sc->curtrk.
   1048   1.1       leo  */
   1049   1.1       leo static int
   1050  1.65       dsl fd_xfer_ok(register struct fd_softc *sc)
   1051   1.1       leo {
   1052  1.75   tsutsui 	int status;
   1053   1.1       leo 
   1054   1.4       leo #ifdef FLP_DEBUG
   1055  1.23  christos 	printf("fd_xfer_ok: cmd: 0x%x, state: 0x%x\n", fd_cmd, fd_state);
   1056   1.4       leo #endif
   1057  1.75   tsutsui 	switch (fd_cmd) {
   1058  1.75   tsutsui 	case IRUPT:
   1059  1.75   tsutsui 		/*
   1060  1.75   tsutsui 		 * Timeout. Force a recalibrate before we try again.
   1061  1.75   tsutsui 		 */
   1062  1.75   tsutsui 		status = read_fdreg(FDC_CS);
   1063   1.8       leo 
   1064  1.75   tsutsui 		fd_error = "Timeout";
   1065  1.75   tsutsui 		sc->curtrk = INV_TRK;
   1066  1.75   tsutsui 		return X_ERROR;
   1067  1.75   tsutsui 	case F_READ:
   1068  1.75   tsutsui 		/*
   1069  1.75   tsutsui 		 * Test for DMA error
   1070  1.75   tsutsui 		 */
   1071  1.75   tsutsui 		status = read_dmastat();
   1072  1.75   tsutsui 		if ((status & DMAOK) == 0) {
   1073  1.75   tsutsui 			fd_error = "DMA error";
   1074  1.75   tsutsui 			return X_ERROR;
   1075  1.75   tsutsui 		}
   1076  1.75   tsutsui 		/*
   1077  1.75   tsutsui 		 * Get controller status and check for errors.
   1078  1.75   tsutsui 		 */
   1079  1.75   tsutsui 		status = read_fdreg(FDC_CS);
   1080  1.75   tsutsui 		if ((status & (RNF | CRCERR | LD_T00)) != 0) {
   1081  1.75   tsutsui 			fd_error = "Read error";
   1082  1.75   tsutsui 			if ((status & RNF) != 0)
   1083  1.75   tsutsui 				sc->curtrk = INV_TRK;
   1084  1.75   tsutsui 			return X_ERROR;
   1085  1.75   tsutsui 		}
   1086  1.75   tsutsui 		break;
   1087  1.75   tsutsui 	case F_WRITE:
   1088  1.75   tsutsui 		/*
   1089  1.75   tsutsui 		 * Test for DMA error
   1090  1.75   tsutsui 		 */
   1091  1.75   tsutsui 		status = read_dmastat();
   1092  1.75   tsutsui 		if ((status & DMAOK) == 0) {
   1093  1.75   tsutsui 			fd_error = "DMA error";
   1094  1.75   tsutsui 			return X_ERROR;
   1095  1.75   tsutsui 		}
   1096  1.75   tsutsui 		/*
   1097  1.75   tsutsui 		 * Get controller status and check for errors.
   1098  1.75   tsutsui 		 */
   1099  1.75   tsutsui 		status = read_fdreg(FDC_CS);
   1100  1.75   tsutsui 		if ((status & WRI_PRO) != 0) {
   1101  1.75   tsutsui 			fd_error = "Write protected";
   1102  1.75   tsutsui 			return X_FAIL;
   1103  1.75   tsutsui 		}
   1104  1.75   tsutsui 		if ((status & (RNF | CRCERR | LD_T00)) != 0) {
   1105  1.75   tsutsui 			fd_error = "Write error";
   1106  1.75   tsutsui 			sc->curtrk = INV_TRK;
   1107  1.75   tsutsui 			return X_ERROR;
   1108  1.75   tsutsui 		}
   1109  1.75   tsutsui 		break;
   1110  1.75   tsutsui 	case SEEK:
   1111  1.75   tsutsui 		status = read_fdreg(FDC_CS);
   1112  1.75   tsutsui 		if ((status & (RNF | CRCERR)) != 0) {
   1113  1.75   tsutsui 			fd_error = "Seek error";
   1114  1.75   tsutsui 			sc->curtrk = INV_TRK;
   1115  1.75   tsutsui 			return X_ERROR;
   1116  1.75   tsutsui 		}
   1117  1.75   tsutsui 		return X_AGAIN;
   1118  1.75   tsutsui 	case RESTORE:
   1119  1.75   tsutsui 		/*
   1120  1.75   tsutsui 		 * Determine if the recalibration succeeded.
   1121  1.75   tsutsui 		 */
   1122  1.75   tsutsui 		status = read_fdreg(FDC_CS);
   1123  1.75   tsutsui 		if ((status & RNF) != 0) {
   1124  1.75   tsutsui 			fd_error = "Recalibrate error";
   1125  1.75   tsutsui 			/* reset controller */
   1126  1.75   tsutsui 			write_fdreg(FDC_CS, IRUPT);
   1127   1.1       leo 			sc->curtrk = INV_TRK;
   1128  1.75   tsutsui 			return X_ERROR;
   1129  1.75   tsutsui 		}
   1130  1.75   tsutsui 		sc->curtrk = 0;
   1131  1.75   tsutsui 		if (fd_state == FLP_STAT) {
   1132  1.75   tsutsui 			if ((status & WRI_PRO) != 0)
   1133  1.75   tsutsui 				sc->flags |= FLPF_WRTPROT;
   1134   1.1       leo 			break;
   1135  1.75   tsutsui 		}
   1136  1.75   tsutsui 		return X_AGAIN;
   1137  1.75   tsutsui 	default:
   1138  1.75   tsutsui 		fd_error = "Driver error: fd_xfer_ok : Unknown state";
   1139  1.75   tsutsui 		return X_FAIL;
   1140   1.1       leo 	}
   1141  1.75   tsutsui 	return X_OK;
   1142   1.1       leo }
   1143   1.1       leo 
   1144   1.1       leo /*
   1145   1.1       leo  * All timeouts will call this function.
   1146   1.1       leo  */
   1147   1.1       leo static void
   1148  1.65       dsl fdmotoroff(struct fd_softc *sc)
   1149   1.1       leo {
   1150  1.75   tsutsui 	int s;
   1151   1.1       leo 
   1152   1.1       leo 	/*
   1153   1.1       leo 	 * Get at harware interrupt level
   1154   1.1       leo 	 */
   1155  1.75   tsutsui 	s = splbio();
   1156   1.1       leo 
   1157   1.1       leo #if FLP_DEBUG
   1158  1.23  christos 	printf("fdmotoroff, state = 0x%x\n", fd_state);
   1159   1.1       leo #endif
   1160   1.1       leo 
   1161  1.75   tsutsui 	switch (fd_state) {
   1162  1.75   tsutsui 	case FLP_STAT:
   1163  1.75   tsutsui 	case FLP_XFER:
   1164  1.75   tsutsui 		/*
   1165  1.75   tsutsui 		 * Timeout during a transfer; cancel transaction
   1166  1.75   tsutsui 		 * set command to 'IRUPT'.
   1167  1.75   tsutsui 		 * A drive-interrupt is simulated to trigger the state
   1168  1.75   tsutsui 		 * machine.
   1169  1.75   tsutsui 		 */
   1170  1.75   tsutsui 		/*
   1171  1.75   tsutsui 		 * Cancel current transaction
   1172  1.75   tsutsui 		 */
   1173  1.75   tsutsui 		fd_cmd = IRUPT;
   1174  1.75   tsutsui 		write_fdreg(FDC_CS, IRUPT);
   1175  1.75   tsutsui 		delay(20);
   1176  1.75   tsutsui 		(void)read_fdreg(FDC_CS);
   1177  1.75   tsutsui 		write_fdreg(FDC_CS, RESTORE);
   1178  1.75   tsutsui 		break;
   1179   1.1       leo 
   1180  1.75   tsutsui 	case FLP_MON:
   1181  1.75   tsutsui 		/*
   1182  1.75   tsutsui 		 * Turn motor off.
   1183  1.75   tsutsui 		 */
   1184  1.75   tsutsui 		if (selected) {
   1185  1.75   tsutsui 			int tmp;
   1186  1.12       leo 
   1187  1.75   tsutsui 			st_dmagrab((dma_farg)fdcint, (dma_farg)fdmoff, sc,
   1188  1.75   tsutsui 			    &tmp, 0);
   1189  1.75   tsutsui 		} else
   1190  1.75   tsutsui 			fd_state = FLP_IDLE;
   1191  1.75   tsutsui 		break;
   1192   1.1       leo 	}
   1193  1.75   tsutsui 	splx(s);
   1194   1.1       leo }
   1195   1.1       leo 
   1196   1.1       leo /*
   1197   1.1       leo  * min byte count to whats left of the track in question
   1198   1.1       leo  */
   1199  1.10   mycroft static void
   1200  1.65       dsl fdminphys(struct buf *bp)
   1201   1.1       leo {
   1202   1.1       leo 	struct fd_softc	*sc;
   1203  1.75   tsutsui 	int sec, toff, tsz;
   1204   1.1       leo 
   1205  1.75   tsutsui 	if ((sc = device_lookup_private(&fd_cd, DISKUNIT(bp->b_dev))) == NULL)
   1206   1.9       cgd 		panic("fdminphys: couldn't get softc");
   1207   1.1       leo 
   1208   1.1       leo 	sec  = bp->b_blkno % (sc->nsectors * sc->nheads);
   1209   1.1       leo 	toff = sec * SECTOR_SIZE;
   1210   1.1       leo 	tsz  = sc->nsectors * sc->nheads * SECTOR_SIZE;
   1211   1.1       leo 
   1212   1.1       leo #ifdef FLP_DEBUG
   1213  1.24       leo 	printf("fdminphys: before %ld", bp->b_bcount);
   1214   1.1       leo #endif
   1215   1.1       leo 
   1216   1.1       leo 	bp->b_bcount = min(bp->b_bcount, tsz - toff);
   1217   1.1       leo 
   1218   1.1       leo #ifdef FLP_DEBUG
   1219  1.24       leo 	printf(" after %ld\n", bp->b_bcount);
   1220   1.1       leo #endif
   1221   1.1       leo 
   1222  1.10   mycroft 	minphys(bp);
   1223  1.12       leo }
   1224  1.12       leo 
   1225  1.12       leo /*
   1226  1.12       leo  * Called from fdmotoroff to turn the motor actually off....
   1227  1.12       leo  * This can't be done in fdmotoroff itself, because exclusive access to the
   1228  1.12       leo  * DMA controller is needed to read the FDC-status register. The function
   1229  1.12       leo  * 'fdmoff()' always runs as the result of a 'dmagrab()'.
   1230  1.12       leo  * We need to test the status-register because we want to be sure that the
   1231  1.12       leo  * drive motor is really off before deselecting the drive. The FDC only
   1232  1.12       leo  * turns off the drive motor after having seen 10 index-pulses. You only
   1233  1.12       leo  * get index-pulses when a drive is selected....This means that if the
   1234  1.12       leo  * drive is deselected when the motor is still spinning, it will continue
   1235  1.12       leo  * to spin _even_ when you insert a floppy later on...
   1236  1.12       leo  */
   1237  1.12       leo static void
   1238  1.65       dsl fdmoff(struct fd_softc *fdsoftc)
   1239  1.12       leo {
   1240  1.12       leo 	int tmp;
   1241  1.12       leo 
   1242  1.12       leo 	if ((fd_state == FLP_MON) && selected) {
   1243  1.12       leo 		tmp = read_fdreg(FDC_CS);
   1244  1.75   tsutsui 		if ((tmp & MOTORON) == 0) {
   1245  1.12       leo 			fddeselect();
   1246  1.12       leo 			fd_state = FLP_IDLE;
   1247  1.75   tsutsui 		} else
   1248  1.75   tsutsui 			callout_reset(&fdsoftc->sc_motor_ch, 10 * FLP_MONDELAY,
   1249  1.35   thorpej 			    (FPV)fdmotoroff, fdsoftc);
   1250  1.12       leo 	}
   1251  1.12       leo 	st_dmafree(fdsoftc, &tmp);
   1252   1.1       leo }
   1253   1.1       leo 
   1254   1.1       leo /*
   1255  1.37       wiz  * Used to find out wich drives are actually connected. We do this by issuing
   1256   1.1       leo  * is 'RESTORE' command and check if the 'track-0' bit is set. This also works
   1257   1.1       leo  * if the drive is present but no floppy is inserted.
   1258   1.1       leo  */
   1259   1.1       leo static void
   1260  1.65       dsl fdtestdrv(struct fd_softc *fdsoftc)
   1261   1.1       leo {
   1262  1.75   tsutsui 	int status;
   1263   1.1       leo 
   1264   1.1       leo 	/*
   1265   1.1       leo 	 * Select the right unit and head.
   1266   1.1       leo 	 */
   1267   1.8       leo 	fdselect(fdsoftc->unit, 0, FLP_DD);
   1268   1.1       leo 
   1269   1.8       leo 	write_fdreg(FDC_CS, RESTORE|HBIT);
   1270   1.1       leo 
   1271   1.1       leo 	/*
   1272   1.1       leo 	 * Wait for about 2 seconds.
   1273   1.1       leo 	 */
   1274   1.1       leo 	delay(2000000);
   1275   1.1       leo 
   1276   1.4       leo 	status = read_fdreg(FDC_CS);
   1277  1.75   tsutsui 	if ((status & (RNF|BUSY)) != 0) {
   1278   1.4       leo 		write_fdreg(FDC_CS, IRUPT);	/* reset controller */
   1279   1.8       leo 		delay(40);
   1280   1.8       leo 	}
   1281   1.1       leo 
   1282  1.75   tsutsui 	if ((status & LD_T00) == 0)
   1283   1.1       leo 		fdsoftc->flags |= FLPF_NOTRESP;
   1284   1.8       leo 
   1285   1.8       leo 	fddeselect();
   1286   1.1       leo }
   1287   1.1       leo 
   1288  1.26   thorpej static void
   1289  1.65       dsl fdgetdefaultlabel(struct fd_softc *sc, struct disklabel *lp, int part)
   1290   1.1       leo {
   1291   1.1       leo 
   1292  1.68    cegger 	memset(lp, 0, sizeof(struct disklabel));
   1293  1.10   mycroft 
   1294   1.1       leo 	lp->d_secsize     = SECTOR_SIZE;
   1295   1.1       leo 	lp->d_ntracks     = sc->nheads;
   1296   1.1       leo 	lp->d_nsectors    = sc->nsectors;
   1297   1.1       leo 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1298   1.1       leo 	lp->d_ncylinders  = sc->nblocks / lp->d_secpercyl;
   1299   1.1       leo 	lp->d_secperunit  = sc->nblocks;
   1300   1.1       leo 
   1301  1.83  christos 	lp->d_type        = DKTYPE_FLOPPY;
   1302   1.1       leo 	lp->d_rpm         = 300; 	/* good guess I suppose.	*/
   1303   1.1       leo 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1304   1.1       leo 	lp->d_bbsize      = 0;
   1305   1.1       leo 	lp->d_sbsize      = 0;
   1306   1.1       leo 	lp->d_npartitions = part + 1;
   1307  1.10   mycroft 	lp->d_trkseek     = STEP_DELAY;
   1308   1.1       leo 	lp->d_magic       = DISKMAGIC;
   1309   1.1       leo 	lp->d_magic2      = DISKMAGIC;
   1310   1.1       leo 	lp->d_checksum    = dkcksum(lp);
   1311   1.1       leo 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1312   1.1       leo 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1313   1.1       leo 	lp->d_partitions[part].p_fsize  = 1024;
   1314   1.1       leo 	lp->d_partitions[part].p_frag   = 8;
   1315  1.26   thorpej }
   1316  1.26   thorpej 
   1317  1.26   thorpej /*
   1318  1.26   thorpej  * Build disk label. For now we only create a label from what we know
   1319  1.26   thorpej  * from 'sc'.
   1320  1.26   thorpej  */
   1321  1.26   thorpej static int
   1322  1.65       dsl fdgetdisklabel(struct fd_softc *sc, dev_t dev)
   1323  1.26   thorpej {
   1324  1.75   tsutsui 	struct disklabel *lp;
   1325  1.75   tsutsui 	int part;
   1326  1.26   thorpej 
   1327  1.26   thorpej 	/*
   1328  1.26   thorpej 	 * If we already got one, get out.
   1329  1.26   thorpej 	 */
   1330  1.75   tsutsui 	if ((sc->flags & FLPF_HAVELAB) != 0)
   1331  1.75   tsutsui 		return 0;
   1332  1.26   thorpej 
   1333  1.26   thorpej #ifdef FLP_DEBUG
   1334  1.26   thorpej 	printf("fdgetdisklabel()\n");
   1335  1.26   thorpej #endif
   1336  1.26   thorpej 
   1337  1.26   thorpej 	part = RAW_PART;
   1338  1.26   thorpej 	lp   = sc->dkdev.dk_label;
   1339  1.26   thorpej 	fdgetdefaultlabel(sc, lp, part);
   1340  1.75   tsutsui 	sc->flags |= FLPF_HAVELAB;
   1341  1.10   mycroft 
   1342  1.75   tsutsui 	return 0;
   1343   1.1       leo }
   1344