xref: /freebsd/sys/dev/fdc/fdc.c (revision e774b2511115cabb3df633cd09027291ae036110)
187f6c662SJulian Elischer /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
65b81b6b3SRodney W. Grimes  * Don Ahn.
75b81b6b3SRodney W. Grimes  *
869acd21dSWarner Losh  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
969acd21dSWarner Losh  * aided by the Linux floppy driver modifications from David Bateman
1069acd21dSWarner Losh  * (dbateman@eng.uts.edu.au).
1169acd21dSWarner Losh  *
12dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
133a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
143a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
153a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
16dc16046fSJoerg Wunsch  *
17dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
183a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
203a2f7427SDavid Greenman  *
212995d110SJoerg Wunsch  * Copyright (c) 2001 Joerg Wunsch,
2206740f7bSJoerg Wunsch  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
232995d110SJoerg Wunsch  *
245b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
255b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
265b81b6b3SRodney W. Grimes  * are met:
275b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
285b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
295b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
305b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
315b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
345b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
355b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
365b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
385b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
405b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
415b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
425b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
435b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
445b81b6b3SRodney W. Grimes  *
45dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
46c3aac50fSPeter Wemm  * $FreeBSD$
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50d2fb4892SJoerg Wunsch #include "opt_fdc.h"
515f830ea2SJoerg Wunsch #include "card.h"
525b81b6b3SRodney W. Grimes 
53b99f0a4aSAndrew Moore #include <sys/param.h>
54b99f0a4aSAndrew Moore #include <sys/systm.h>
559626b608SPoul-Henning Kamp #include <sys/bio.h>
566182fdbdSPeter Wemm #include <sys/bus.h>
576182fdbdSPeter Wemm #include <sys/conf.h>
58b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
59fb919e4dSMark Murray #include <sys/disklabel.h>
606182fdbdSPeter Wemm #include <sys/fcntl.h>
61e774b251SJoerg Wunsch #include <sys/fdcio.h>
62fb919e4dSMark Murray #include <sys/kernel.h>
63fb919e4dSMark Murray #include <sys/lock.h>
64b99f0a4aSAndrew Moore #include <sys/malloc.h>
656182fdbdSPeter Wemm #include <sys/module.h>
66fb919e4dSMark Murray #include <sys/mutex.h>
673a2f7427SDavid Greenman #include <sys/proc.h>
68b99f0a4aSAndrew Moore #include <sys/syslog.h>
696182fdbdSPeter Wemm 
706182fdbdSPeter Wemm #include <sys/bus.h>
716182fdbdSPeter Wemm #include <machine/bus.h>
726182fdbdSPeter Wemm #include <sys/rman.h>
736182fdbdSPeter Wemm 
746182fdbdSPeter Wemm #include <machine/clock.h>
756182fdbdSPeter Wemm #include <machine/resource.h>
76dc5df763SJoerg Wunsch #include <machine/stdarg.h>
776182fdbdSPeter Wemm 
786182fdbdSPeter Wemm #include <isa/isavar.h>
79a97c75b7SDoug Rabson #include <isa/isareg.h>
80a97c75b7SDoug Rabson #include <isa/fdreg.h>
81a97c75b7SDoug Rabson #include <isa/fdc.h>
82a97c75b7SDoug Rabson #include <isa/rtc.h>
836182fdbdSPeter Wemm 
84b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
855b81b6b3SRodney W. Grimes 
860722d6abSJoerg Wunsch /* configuration flags */
870722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
88e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
890722d6abSJoerg Wunsch 
900722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
910722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
920722d6abSJoerg Wunsch 
93dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
94dc5df763SJoerg Wunsch #define FD_FAILED -1
95dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
96dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
97dc5df763SJoerg Wunsch 
986fb89845SKATO Takenori #define NUMTYPES 17
996fb89845SKATO Takenori #define NUMDENS  (NUMTYPES - 7)
1007ca0641bSAndrey A. Chernov 
1013a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
102b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
103b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
104b99f0a4aSAndrew Moore #define FD_1720         1
105b99f0a4aSAndrew Moore #define FD_1480         2
106b99f0a4aSAndrew Moore #define FD_1440         3
107b99f0a4aSAndrew Moore #define FD_1200         4
108b99f0a4aSAndrew Moore #define FD_820          5
109b99f0a4aSAndrew Moore #define FD_800          6
110b99f0a4aSAndrew Moore #define FD_720          7
111b99f0a4aSAndrew Moore #define FD_360          8
1126fb89845SKATO Takenori #define FD_640          9
1136fb89845SKATO Takenori #define FD_1232         10
114ed2fa05eSAndrey A. Chernov 
1156fb89845SKATO Takenori #define FD_1480in5_25   11
1166fb89845SKATO Takenori #define FD_1440in5_25   12
1176fb89845SKATO Takenori #define FD_820in5_25    13
1186fb89845SKATO Takenori #define FD_800in5_25    14
1196fb89845SKATO Takenori #define FD_720in5_25    15
1206fb89845SKATO Takenori #define FD_360in5_25    16
1216fb89845SKATO Takenori #define FD_640in5_25    17
122b99f0a4aSAndrew Moore 
1237ca0641bSAndrey A. Chernov 
1246f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1255b81b6b3SRodney W. Grimes {
126126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
127126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
128126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
129126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
130126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
131126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
132126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
133b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
1346fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /*  640K in DD 5.25in */
1356fb89845SKATO Takenori {  8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
136ed2fa05eSAndrey A. Chernov 
137126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
138126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
139126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
140126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
141126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
142126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1436fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /*  640K in HD 5.25in */
1445b81b6b3SRodney W. Grimes };
1455b81b6b3SRodney W. Grimes 
146b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
147dc16046fSJoerg Wunsch 
1485b81b6b3SRodney W. Grimes /***********************************************************************\
1495b81b6b3SRodney W. Grimes * Per controller structure.						*
1505b81b6b3SRodney W. Grimes \***********************************************************************/
1516182fdbdSPeter Wemm static devclass_t fdc_devclass;
1525b81b6b3SRodney W. Grimes 
1535b81b6b3SRodney W. Grimes /***********************************************************************\
1545b81b6b3SRodney W. Grimes * Per drive structure.							*
155b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1565b81b6b3SRodney W. Grimes \***********************************************************************/
1576182fdbdSPeter Wemm struct fd_data {
158b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1595b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1603a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1615b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1625b81b6b3SRodney W. Grimes 	int	flags;
1635b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1645b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1655b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1665b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1675b81b6b3SRodney W. Grimes 	int	skip;
1685b81b6b3SRodney W. Grimes 	int	hddrv;
169dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1705b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1713a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
17202a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
17302a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
174b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
1756182fdbdSPeter Wemm 	device_t dev;
1766182fdbdSPeter Wemm 	fdu_t	fdu;
1776182fdbdSPeter Wemm };
17837286586SPeter Wemm 
17937286586SPeter Wemm struct fdc_ivars {
18037286586SPeter Wemm 	int	fdunit;
18137286586SPeter Wemm };
1826182fdbdSPeter Wemm static devclass_t fd_devclass;
1835b81b6b3SRodney W. Grimes 
1845b81b6b3SRodney W. Grimes /***********************************************************************\
1855b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1865b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1875b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1885b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1895b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1905b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1915b81b6b3SRodney W. Grimes \***********************************************************************/
192b99f0a4aSAndrew Moore 
1933a2f7427SDavid Greenman /* needed for ft driver, thus exported */
1946182fdbdSPeter Wemm int in_fdc(struct fdc_data *);
1956182fdbdSPeter Wemm int out_fdc(struct fdc_data *, int);
1963a2f7427SDavid Greenman 
1973a2f7427SDavid Greenman /* internal functions */
1986182fdbdSPeter Wemm static	void fdc_intr(void *);
1996182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
2003a2f7427SDavid Greenman #  define TURNON 1
2013a2f7427SDavid Greenman #  define TURNOFF 0
2023a2f7427SDavid Greenman static timeout_t fd_turnoff;
2033a2f7427SDavid Greenman static timeout_t fd_motor_on;
2046182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
2053a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2066182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
2076182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
2085c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2093a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2106182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
2116182fdbdSPeter Wemm static int retrier(struct fdc_data *);
2123a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2133a2f7427SDavid Greenman 
214d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
215d66c374fSTor Egge 
216d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
217d66c374fSTor Egge 
218d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
219cba2a7c6SBruce Evans static char const * const fdstates[] =
2205b81b6b3SRodney W. Grimes {
2215b81b6b3SRodney W. Grimes "DEVIDLE",
2225b81b6b3SRodney W. Grimes "FINDWORK",
2235b81b6b3SRodney W. Grimes "DOSEEK",
2245b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2255b81b6b3SRodney W. Grimes "IOCOMPLETE",
2265b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2275b81b6b3SRodney W. Grimes "STARTRECAL",
2285b81b6b3SRodney W. Grimes "RESETCTLR",
2295b81b6b3SRodney W. Grimes "SEEKWAIT",
2305b81b6b3SRodney W. Grimes "RECALWAIT",
2315b81b6b3SRodney W. Grimes "MOTORWAIT",
2325c1a1eaeSBruce Evans "IOTIMEDOUT",
2335c1a1eaeSBruce Evans "RESETCOMPLETE",
234ff9607b0SBruce Evans "PIOREAD",
2355b81b6b3SRodney W. Grimes };
2365b81b6b3SRodney W. Grimes 
2373a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
238cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2395b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2405b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
241d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2425b81b6b3SRodney W. Grimes #define TRACE0(arg)
2435b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
244d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2455b81b6b3SRodney W. Grimes 
246427ccf00SDoug Rabson static void
247427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
248427ccf00SDoug Rabson {
249427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
250427ccf00SDoug Rabson }
251427ccf00SDoug Rabson 
252427ccf00SDoug Rabson static u_int8_t
253427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
254427ccf00SDoug Rabson {
255427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
256427ccf00SDoug Rabson }
257427ccf00SDoug Rabson 
258427ccf00SDoug Rabson static void
259427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
260427ccf00SDoug Rabson {
261427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
262427ccf00SDoug Rabson }
263427ccf00SDoug Rabson 
264427ccf00SDoug Rabson static u_int8_t
265427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
266427ccf00SDoug Rabson {
267427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
268427ccf00SDoug Rabson }
269427ccf00SDoug Rabson 
270427ccf00SDoug Rabson static void
2715f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
272427ccf00SDoug Rabson {
273427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
274427ccf00SDoug Rabson }
275427ccf00SDoug Rabson 
27658c9d623SPeter Wemm #if NCARD > 0
2775f830ea2SJoerg Wunsch static void
2785f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
2795f830ea2SJoerg Wunsch {
2805f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
2815f830ea2SJoerg Wunsch }
28258c9d623SPeter Wemm #endif
2835f830ea2SJoerg Wunsch 
284427ccf00SDoug Rabson #if 0
285427ccf00SDoug Rabson 
286427ccf00SDoug Rabson static u_int8_t
287427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
288427ccf00SDoug Rabson {
289427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
290427ccf00SDoug Rabson }
291427ccf00SDoug Rabson 
292427ccf00SDoug Rabson #endif
293427ccf00SDoug Rabson 
29487f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
29587f6c662SJulian Elischer static	d_close_t	fdclose;
29687f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
29787f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
29887f6c662SJulian Elischer 
29987f6c662SJulian Elischer #define CDEV_MAJOR 9
300f7ea2f55SJulian Elischer 
3014e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
3024e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
3034e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
3044e2f199eSPoul-Henning Kamp 	/* read */	physread,
3054e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
3064e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
3074e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
3084e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
3094e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
3104e2f199eSPoul-Henning Kamp 	/* name */	"fd",
3114e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
3124e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
3134e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
3144e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
3154e2f199eSPoul-Henning Kamp };
3164e2f199eSPoul-Henning Kamp 
317dc5df763SJoerg Wunsch static int
3186182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
319dc5df763SJoerg Wunsch {
3206182fdbdSPeter Wemm 	fdc->fdc_errs++;
32116b04b6aSJoerg Wunsch 	if (s) {
322b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
323b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
324b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
325b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
326b6e5f28eSPeter Wemm 						    "logging any more\n");
32716b04b6aSJoerg Wunsch 	}
328dc5df763SJoerg Wunsch 
329dc5df763SJoerg Wunsch 	return FD_FAILED;
330dc5df763SJoerg Wunsch }
331dc5df763SJoerg Wunsch 
332dc5df763SJoerg Wunsch /*
333dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
334dc5df763SJoerg Wunsch  * Unit number,
335dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
336dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
337dc5df763SJoerg Wunsch  */
3386f4e0bebSPoul-Henning Kamp static int
3396182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
340dc5df763SJoerg Wunsch {
341dc5df763SJoerg Wunsch 	u_char cmd;
342dc5df763SJoerg Wunsch 	int n_in;
343dc5df763SJoerg Wunsch 	int n;
344dc5df763SJoerg Wunsch 	va_list ap;
345dc5df763SJoerg Wunsch 
346dc5df763SJoerg Wunsch 	va_start(ap, n_out);
347dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
348dc5df763SJoerg Wunsch 	va_end(ap);
349dc5df763SJoerg Wunsch 	va_start(ap, n_out);
350dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
351dc5df763SJoerg Wunsch 	{
3526182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
353dc5df763SJoerg Wunsch 		{
354dc5df763SJoerg Wunsch 			char msg[50];
3552127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
356dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
357dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
3586182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
359dc5df763SJoerg Wunsch 		}
360dc5df763SJoerg Wunsch 	}
361dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
362dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
363dc5df763SJoerg Wunsch 	{
364dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
3656182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
366dc5df763SJoerg Wunsch 		{
367dc5df763SJoerg Wunsch 			char msg[50];
3682127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
369dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
370dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
3716182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
372dc5df763SJoerg Wunsch 		}
373dc5df763SJoerg Wunsch 	}
374dc5df763SJoerg Wunsch 
375dc5df763SJoerg Wunsch 	return 0;
376dc5df763SJoerg Wunsch }
377dc5df763SJoerg Wunsch 
3786f4e0bebSPoul-Henning Kamp static int
379d66c374fSTor Egge enable_fifo(fdc_p fdc)
380d66c374fSTor Egge {
381d66c374fSTor Egge 	int i, j;
382d66c374fSTor Egge 
383d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
384d66c374fSTor Egge 
385d66c374fSTor Egge 		/*
386d66c374fSTor Egge 		 * XXX:
387d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
388d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
389d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
390d66c374fSTor Egge 		 */
391d66c374fSTor Egge 
3926182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
3936182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
394d66c374fSTor Egge 
395d66c374fSTor Egge 		/* If command is invalid, return */
396d66c374fSTor Egge 		j = 100000;
397427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
398d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
399d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
400d66c374fSTor Egge 				fdc_reset(fdc);
401d66c374fSTor Egge 				return FD_FAILED;
402d66c374fSTor Egge 			}
403d66c374fSTor Egge 		if (j<0 ||
4046182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
405d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
406d66c374fSTor Egge 			fdc_reset(fdc);
4076182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
408d66c374fSTor Egge 		}
409d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
410d66c374fSTor Egge 		return 0;
411d66c374fSTor Egge 	}
4126182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
413d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
4146182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
415d66c374fSTor Egge 	return 0;
416d66c374fSTor Egge }
417d66c374fSTor Egge 
418d66c374fSTor Egge static int
419dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
420dc5df763SJoerg Wunsch {
421dc5df763SJoerg Wunsch 	int st3;
422dc5df763SJoerg Wunsch 
4236182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
424dc5df763SJoerg Wunsch 	{
4256182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
426dc5df763SJoerg Wunsch 	}
427dc5df763SJoerg Wunsch 	if (st3p)
428dc5df763SJoerg Wunsch 		*st3p = st3;
429dc5df763SJoerg Wunsch 
430dc5df763SJoerg Wunsch 	return 0;
431dc5df763SJoerg Wunsch }
432dc5df763SJoerg Wunsch 
4336f4e0bebSPoul-Henning Kamp static int
434dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
435dc5df763SJoerg Wunsch {
4366182fdbdSPeter Wemm 	int cyl, st0, ret;
437dc5df763SJoerg Wunsch 
4386182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
4396182fdbdSPeter Wemm 	if (ret) {
4406182fdbdSPeter Wemm 		(void)fdc_err(fdc,
441dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
442dc5df763SJoerg Wunsch 		return ret;
443dc5df763SJoerg Wunsch 	}
444dc5df763SJoerg Wunsch 
445dc5df763SJoerg Wunsch 	if (st0p)
446dc5df763SJoerg Wunsch 		*st0p = st0;
447dc5df763SJoerg Wunsch 
4486182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
449dc5df763SJoerg Wunsch 		/*
450dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
451dc5df763SJoerg Wunsch 		 */
452dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
453dc5df763SJoerg Wunsch 	}
454dc5df763SJoerg Wunsch 
4556182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
4566182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
457dc5df763SJoerg Wunsch 	}
458dc5df763SJoerg Wunsch 
459dc5df763SJoerg Wunsch 	if (cylp)
460dc5df763SJoerg Wunsch 		*cylp = cyl;
461dc5df763SJoerg Wunsch 
462dc5df763SJoerg Wunsch 	return 0;
463dc5df763SJoerg Wunsch }
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 
4666f4e0bebSPoul-Henning Kamp static int
467dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
468dc5df763SJoerg Wunsch {
469dc5df763SJoerg Wunsch 	int i, ret;
470b5e8ce9fSBruce Evans 
4716182fdbdSPeter Wemm 	for (i = 0; i < 7; i++) {
472b5e8ce9fSBruce Evans 		/*
473b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
474a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
475b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
476b5e8ce9fSBruce Evans 		 */
477b5e8ce9fSBruce Evans 		int status;
478b5e8ce9fSBruce Evans 
4796182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
480b5e8ce9fSBruce Evans 		fdc->status[i] = status;
481b5e8ce9fSBruce Evans 		if (ret != 0)
482dc5df763SJoerg Wunsch 			break;
483dc5df763SJoerg Wunsch 	}
484dc5df763SJoerg Wunsch 
485dc5df763SJoerg Wunsch 	if (ret == 0)
486dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
487dc5df763SJoerg Wunsch 	else
488dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
489dc5df763SJoerg Wunsch 
490dc5df763SJoerg Wunsch 	return ret;
491dc5df763SJoerg Wunsch }
492dc5df763SJoerg Wunsch 
4935b81b6b3SRodney W. Grimes /****************************************************************************/
4945b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
4955b81b6b3SRodney W. Grimes /****************************************************************************/
496dc5df763SJoerg Wunsch 
4973a2f7427SDavid Greenman static int
49837286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
4995b81b6b3SRodney W. Grimes {
50037286586SPeter Wemm 	device_t dev;
5015f830ea2SJoerg Wunsch 	int ispnp, ispcmcia;
5025b81b6b3SRodney W. Grimes 
50337286586SPeter Wemm 	dev = fdc->fdc_dev;
5045f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
5055f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
5066182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
5076182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
5086182fdbdSPeter Wemm 
509b6e5f28eSPeter Wemm 	/*
5105f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
5115f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
5125f830ea2SJoerg Wunsch 	 * register at 0x3f6.
5135f830ea2SJoerg Wunsch 	 *
514b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
5155f830ea2SJoerg Wunsch 	 *
5165f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
5175f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
5185f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
519b6e5f28eSPeter Wemm 	 */
5206182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
5216182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
5225f830ea2SJoerg Wunsch 					     ispcmcia ? 8 : (ispnp ? 1 : 6),
5235f830ea2SJoerg Wunsch 					     RF_ACTIVE);
5246182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
525427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve I/O port range\n");
52637286586SPeter Wemm 		return ENXIO;
5276182fdbdSPeter Wemm 	}
528427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
529427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
530427ccf00SDoug Rabson 
5315f830ea2SJoerg Wunsch 	if (!ispcmcia) {
532427ccf00SDoug Rabson 		/*
5335f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
5345f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
5355f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
5365f830ea2SJoerg Wunsch 		 * accordingly.
537b6e5f28eSPeter Wemm 		 */
538b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
539b6e5f28eSPeter Wemm 			fdc->port_off = -2;
540b6e5f28eSPeter Wemm 
541b6e5f28eSPeter Wemm 		/*
5425f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
5435f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
5445f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
545a2639a18SPeter Wemm 		 *
5465f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
5475f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
5485f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
5495f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
5505f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
5515f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
5525f830ea2SJoerg Wunsch 		 * address for the control register.
553427ccf00SDoug Rabson 		 */
554b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
555b9da888fSPeter Wemm 			u_long ctlstart;
556b9da888fSPeter Wemm 
557b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
5585f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
5595f830ea2SJoerg Wunsch 				fdc->port_off + 7;
560b6e5f28eSPeter Wemm 
561b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
562b9da888fSPeter Wemm 		}
563b6e5f28eSPeter Wemm 
564b6e5f28eSPeter Wemm 		/*
565b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
566b6e5f28eSPeter Wemm 		 */
567427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
5685f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
5695f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
570b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
571427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
5725f830ea2SJoerg Wunsch 			device_printf(dev,
5735f830ea2SJoerg Wunsch 				      "cannot reserve control I/O port range\n");
57437286586SPeter Wemm 			return ENXIO;
575427ccf00SDoug Rabson 		}
576427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
577427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
5785f830ea2SJoerg Wunsch 	}
5796182fdbdSPeter Wemm 
5806182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
5816182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
5826182fdbdSPeter Wemm 					  RF_ACTIVE);
5836182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
584427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
58537286586SPeter Wemm 		return ENXIO;
5866182fdbdSPeter Wemm 	}
5875f830ea2SJoerg Wunsch 
5885f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
5896182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
5906182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
5916182fdbdSPeter Wemm 						  RF_ACTIVE);
5926182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
593427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
59437286586SPeter Wemm 			return ENXIO;
5956182fdbdSPeter Wemm 		}
5966182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
5975f830ea2SJoerg Wunsch 	}
59837286586SPeter Wemm 
59937286586SPeter Wemm 	return 0;
60037286586SPeter Wemm }
60137286586SPeter Wemm 
60237286586SPeter Wemm static void
60337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
60437286586SPeter Wemm {
60537286586SPeter Wemm 	device_t dev;
60637286586SPeter Wemm 
60737286586SPeter Wemm 	dev = fdc->fdc_dev;
60837286586SPeter Wemm 	if (fdc->res_irq != 0) {
60937286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
61037286586SPeter Wemm 					fdc->res_irq);
61137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
61237286586SPeter Wemm 				     fdc->res_irq);
61337286586SPeter Wemm 	}
61437286586SPeter Wemm 	if (fdc->res_ctl != 0) {
61537286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
61637286586SPeter Wemm 					fdc->res_ctl);
61737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
61837286586SPeter Wemm 				     fdc->res_ctl);
61937286586SPeter Wemm 	}
62037286586SPeter Wemm 	if (fdc->res_ioport != 0) {
62137286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
62237286586SPeter Wemm 					fdc->res_ioport);
62337286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
62437286586SPeter Wemm 				     fdc->res_ioport);
62537286586SPeter Wemm 	}
62637286586SPeter Wemm 	if (fdc->res_drq != 0) {
62737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
62837286586SPeter Wemm 					fdc->res_drq);
62937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
63037286586SPeter Wemm 				     fdc->res_drq);
63137286586SPeter Wemm 	}
63237286586SPeter Wemm }
63337286586SPeter Wemm 
63437286586SPeter Wemm /****************************************************************************/
63537286586SPeter Wemm /*                      autoconfiguration stuff                             */
63637286586SPeter Wemm /****************************************************************************/
63737286586SPeter Wemm 
63837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
63937286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
64037286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
64137286586SPeter Wemm 	{0}
64237286586SPeter Wemm };
64337286586SPeter Wemm 
64437286586SPeter Wemm static int
6453aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
64637286586SPeter Wemm {
64737286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
64837286586SPeter Wemm 
64937286586SPeter Wemm 	switch (which) {
65037286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
65137286586SPeter Wemm 		*result = ivars->fdunit;
65237286586SPeter Wemm 		break;
65337286586SPeter Wemm 	default:
65437286586SPeter Wemm 		return ENOENT;
65537286586SPeter Wemm 	}
65637286586SPeter Wemm 	return 0;
65737286586SPeter Wemm }
65837286586SPeter Wemm 
65937286586SPeter Wemm /*
66037286586SPeter Wemm  * fdc controller section.
66137286586SPeter Wemm  */
66237286586SPeter Wemm static int
66337286586SPeter Wemm fdc_probe(device_t dev)
66437286586SPeter Wemm {
66537286586SPeter Wemm 	int	error, ic_type;
66637286586SPeter Wemm 	struct	fdc_data *fdc;
66737286586SPeter Wemm 
66837286586SPeter Wemm 	fdc = device_get_softc(dev);
66937286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
67037286586SPeter Wemm 	fdc->fdc_dev = dev;
6715f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
67237286586SPeter Wemm 
67337286586SPeter Wemm 	/* Check pnp ids */
67437286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
67537286586SPeter Wemm 	if (error == ENXIO)
67637286586SPeter Wemm 		return ENXIO;
6775f830ea2SJoerg Wunsch 	if (error == 0)
6785f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
67937286586SPeter Wemm 
68037286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
68137286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
68237286586SPeter Wemm 	if (error)
68337286586SPeter Wemm 		goto out;
6845b81b6b3SRodney W. Grimes 
68516111cedSAndrew Moore 	/* First - lets reset the floppy controller */
686427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
68716111cedSAndrew Moore 	DELAY(100);
688427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
68916111cedSAndrew Moore 
6905b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
6916182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
6926182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
6936182fdbdSPeter Wemm 		error = ENXIO;
6946182fdbdSPeter Wemm 		goto out;
6955b81b6b3SRodney W. Grimes 	}
6966182fdbdSPeter Wemm 
6976182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
6986182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
6996182fdbdSPeter Wemm 		switch (ic_type) {
7006182fdbdSPeter Wemm 		case 0x80:
7016182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
7026182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
7036182fdbdSPeter Wemm 			break;
7046182fdbdSPeter Wemm 		case 0x81:
7056182fdbdSPeter Wemm 			device_set_desc(dev, "Intel 82077 or clone");
7066182fdbdSPeter Wemm 			fdc->fdct = FDC_I82077;
7076182fdbdSPeter Wemm 			break;
7086182fdbdSPeter Wemm 		case 0x90:
7096182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 72065B or clone");
7106182fdbdSPeter Wemm 			fdc->fdct = FDC_NE72065;
7116182fdbdSPeter Wemm 			break;
7126182fdbdSPeter Wemm 		default:
7136182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
7146182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
7156182fdbdSPeter Wemm 			break;
7166182fdbdSPeter Wemm 		}
7176182fdbdSPeter Wemm 	}
7186182fdbdSPeter Wemm 
7196182fdbdSPeter Wemm out:
72037286586SPeter Wemm 	fdc_release_resources(fdc);
7216182fdbdSPeter Wemm 	return (error);
7225b81b6b3SRodney W. Grimes }
7235b81b6b3SRodney W. Grimes 
7245f830ea2SJoerg Wunsch #if NCARD > 0
7255f830ea2SJoerg Wunsch 
7265f830ea2SJoerg Wunsch static int
7275f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
7285f830ea2SJoerg Wunsch {
7295f830ea2SJoerg Wunsch 	int	error;
7305f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7315f830ea2SJoerg Wunsch 
7325f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7335f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
7345f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
7355f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
7365f830ea2SJoerg Wunsch 
7375f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
7385f830ea2SJoerg Wunsch 
7395f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
7405f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
7415f830ea2SJoerg Wunsch 	if (error)
7425f830ea2SJoerg Wunsch 		goto out;
7435f830ea2SJoerg Wunsch 
7445f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
7455f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
7465f830ea2SJoerg Wunsch 	DELAY(100);
7475f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
7485f830ea2SJoerg Wunsch 
7495f830ea2SJoerg Wunsch 	/* see if it can handle a command */
7505f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7515f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
7525f830ea2SJoerg Wunsch 		error = ENXIO;
7535f830ea2SJoerg Wunsch 		goto out;
7545f830ea2SJoerg Wunsch 	}
7555f830ea2SJoerg Wunsch 
7565f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
7575f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
7585f830ea2SJoerg Wunsch 
7595f830ea2SJoerg Wunsch out:
7605f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7615f830ea2SJoerg Wunsch 	return (error);
7625f830ea2SJoerg Wunsch }
7635f830ea2SJoerg Wunsch 
7645f830ea2SJoerg Wunsch static int
7655f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev)
7665f830ea2SJoerg Wunsch {
7675f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7685f830ea2SJoerg Wunsch 	int	error;
7695f830ea2SJoerg Wunsch 
7705f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7715f830ea2SJoerg Wunsch 
7725f830ea2SJoerg Wunsch 	/* have our children detached first */
7735f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
7745f830ea2SJoerg Wunsch 		return (error);
7755f830ea2SJoerg Wunsch 
7765f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
7775f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
7785f830ea2SJoerg Wunsch 		return (0);
7795f830ea2SJoerg Wunsch 	}
7805f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
7815f830ea2SJoerg Wunsch 
7825f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
7835f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
7845f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7855f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
7865f830ea2SJoerg Wunsch 	return (0);
7875f830ea2SJoerg Wunsch }
7885f830ea2SJoerg Wunsch 
7895f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
7905f830ea2SJoerg Wunsch 
7915b81b6b3SRodney W. Grimes /*
79237286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
7935b81b6b3SRodney W. Grimes  */
7946182fdbdSPeter Wemm static void
79537286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
7965b81b6b3SRodney W. Grimes {
79737286586SPeter Wemm 	int	disabled;
79837286586SPeter Wemm 	struct fdc_ivars *ivar;
7996182fdbdSPeter Wemm 	device_t child;
80092200632SGarrett Wollman 
8017cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
80237286586SPeter Wemm 	if (ivar == NULL)
8036182fdbdSPeter Wemm 		return;
80437286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
80537286586SPeter Wemm 		ivar->fdunit = 0;
806fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
80737286586SPeter Wemm 	if (child == NULL)
8086182fdbdSPeter Wemm 		return;
80937286586SPeter Wemm 	device_set_ivars(child, ivar);
810a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
811a97c75b7SDoug Rabson 	    && disabled != 0)
8126182fdbdSPeter Wemm 		device_disable(child);
8136182fdbdSPeter Wemm }
8146182fdbdSPeter Wemm 
8156182fdbdSPeter Wemm static int
8166182fdbdSPeter Wemm fdc_attach(device_t dev)
8176182fdbdSPeter Wemm {
81837286586SPeter Wemm 	struct	fdc_data *fdc;
81937286586SPeter Wemm 	int	i, error;
820ada54f9eSPeter Wemm 	const char *name;
821427ccf00SDoug Rabson 
82237286586SPeter Wemm 	fdc = device_get_softc(dev);
82337286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
82437286586SPeter Wemm 	if (error) {
82537286586SPeter Wemm 		device_printf(dev, "cannot re-aquire resources\n");
82637286586SPeter Wemm 		return error;
82737286586SPeter Wemm 	}
82837286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
8293c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
8303c36743eSMark Murray 			       &fdc->fdc_intr);
83137286586SPeter Wemm 	if (error) {
83237286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
83337286586SPeter Wemm 		return error;
83437286586SPeter Wemm 	}
83537286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
8365b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
8376182fdbdSPeter Wemm 
8385f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
839100f78bbSSujal Patel 		/* Acquire the DMA channel forever, The driver will do the rest */
8406182fdbdSPeter Wemm 				/* XXX should integrate with rman */
841100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
842dd87702aSBruce Evans 		isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
8435f830ea2SJoerg Wunsch 	}
8445b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
8456182fdbdSPeter Wemm 
8463a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
847427ccf00SDoug Rabson 	fdout_wr(fdc, ((fdc->fdout = 0)));
8488177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
8495b81b6b3SRodney W. Grimes 
8506182fdbdSPeter Wemm 	/*
85137286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
85237286586SPeter Wemm 	 * devices from the BIOS unless overridden.
8536182fdbdSPeter Wemm 	 */
854ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
855ada54f9eSPeter Wemm 	i = -1;
856ada54f9eSPeter Wemm 	while ((i = resource_query_string(i, "at", name)) != -1)
85737286586SPeter Wemm 		fdc_add_child(dev, resource_query_name(i),
85837286586SPeter Wemm 			       resource_query_unit(i));
85937286586SPeter Wemm 
8606182fdbdSPeter Wemm 	return (bus_generic_attach(dev));
8616182fdbdSPeter Wemm }
8626182fdbdSPeter Wemm 
86315317dd8SMatthew N. Dodd static int
8646182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
8656182fdbdSPeter Wemm {
86615317dd8SMatthew N. Dodd 	int retval = 0;
86715317dd8SMatthew N. Dodd 
86815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
86915317dd8SMatthew N. Dodd 	retval += printf(" on %s drive %d\n", device_get_nameunit(me),
87037286586SPeter Wemm 	       fdc_get_fdunit(child));
87115317dd8SMatthew N. Dodd 
87215317dd8SMatthew N. Dodd 	return (retval);
8736182fdbdSPeter Wemm }
8746182fdbdSPeter Wemm 
87516e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
87616e68fc6SPeter Wemm 	/* Device interface */
87716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
87816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
87916e68fc6SPeter Wemm 	DEVMETHOD(device_detach,	bus_generic_detach),
88016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
88216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
88316e68fc6SPeter Wemm 
88416e68fc6SPeter Wemm 	/* Bus interface */
88516e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
88637286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
88716e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
88816e68fc6SPeter Wemm 
88916e68fc6SPeter Wemm 	{ 0, 0 }
89016e68fc6SPeter Wemm };
89116e68fc6SPeter Wemm 
89216e68fc6SPeter Wemm static driver_t fdc_driver = {
89316e68fc6SPeter Wemm 	"fdc",
89416e68fc6SPeter Wemm 	fdc_methods,
89516e68fc6SPeter Wemm 	sizeof(struct fdc_data)
89616e68fc6SPeter Wemm };
89716e68fc6SPeter Wemm 
89816e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
89916e68fc6SPeter Wemm 
9005f830ea2SJoerg Wunsch #if NCARD > 0
9015f830ea2SJoerg Wunsch 
9025f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
9035f830ea2SJoerg Wunsch 	/* Device interface */
9045f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
9055f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
9065f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_pccard_detach),
9075f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
9085f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
9095f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
9105f830ea2SJoerg Wunsch 
9115f830ea2SJoerg Wunsch 	/* Bus interface */
9125f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
9135f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
9145f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
9155f830ea2SJoerg Wunsch 
9165f830ea2SJoerg Wunsch 	{ 0, 0 }
9175f830ea2SJoerg Wunsch };
9185f830ea2SJoerg Wunsch 
9195f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
9205f830ea2SJoerg Wunsch 	"fdc",
9215f830ea2SJoerg Wunsch 	fdc_pccard_methods,
9225f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
9235f830ea2SJoerg Wunsch };
9245f830ea2SJoerg Wunsch 
9255f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
9265f830ea2SJoerg Wunsch 
9275f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
9285f830ea2SJoerg Wunsch 
9293f54a085SPoul-Henning Kamp static void fd_clone __P((void *arg, char *name, int namelen, dev_t *dev));
9303f54a085SPoul-Henning Kamp 
9313f54a085SPoul-Henning Kamp static struct {
9323f54a085SPoul-Henning Kamp 	char *match;
9333f54a085SPoul-Henning Kamp 	int minor;
9343f54a085SPoul-Henning Kamp 	int link;
9353f54a085SPoul-Henning Kamp } fd_suffix[] = {
9363f54a085SPoul-Henning Kamp 	{ "a",		0,	1 },
9373f54a085SPoul-Henning Kamp 	{ "b",		0,	1 },
9383f54a085SPoul-Henning Kamp 	{ "c",		0,	1 },
9393f54a085SPoul-Henning Kamp 	{ "d",		0,	1 },
9403f54a085SPoul-Henning Kamp 	{ "e",		0,	1 },
9413f54a085SPoul-Henning Kamp 	{ "f",		0,	1 },
9423f54a085SPoul-Henning Kamp 	{ "g",		0,	1 },
9433f54a085SPoul-Henning Kamp 	{ "h",		0,	1 },
9443f54a085SPoul-Henning Kamp 	{ ".1720",	1,	0 },
9453f54a085SPoul-Henning Kamp 	{ ".1480",	2,	0 },
9463f54a085SPoul-Henning Kamp 	{ ".1440",	3,	0 },
9473f54a085SPoul-Henning Kamp 	{ ".1200",	4,	0 },
9483f54a085SPoul-Henning Kamp 	{ ".820",	5,	0 },
9493f54a085SPoul-Henning Kamp 	{ ".800",	6,	0 },
9503f54a085SPoul-Henning Kamp 	{ ".720",	7,	0 },
9513f54a085SPoul-Henning Kamp 	{ ".360",	8,	0 },
9523f54a085SPoul-Henning Kamp 	{ ".640",	9,	0 },
9533f54a085SPoul-Henning Kamp 	{ ".1232",	10,	0 },
9543f54a085SPoul-Henning Kamp 	{ 0, 0 }
9553f54a085SPoul-Henning Kamp };
9563f54a085SPoul-Henning Kamp static void
9573f54a085SPoul-Henning Kamp fd_clone(arg, name, namelen, dev)
9583f54a085SPoul-Henning Kamp 	void *arg;
9593f54a085SPoul-Henning Kamp 	char *name;
9603f54a085SPoul-Henning Kamp 	int namelen;
9613f54a085SPoul-Henning Kamp 	dev_t *dev;
9623f54a085SPoul-Henning Kamp {
9633f54a085SPoul-Henning Kamp 	int u, d, i;
9643f54a085SPoul-Henning Kamp 	char *n;
9653f54a085SPoul-Henning Kamp 	dev_t pdev;
9663f54a085SPoul-Henning Kamp 
9673f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
9683f54a085SPoul-Henning Kamp 		return;
969db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
9703f54a085SPoul-Henning Kamp 		return;
9713f54a085SPoul-Henning Kamp 	for (i = 0; ; i++) {
9723f54a085SPoul-Henning Kamp 		if (fd_suffix[i].match == NULL)
9733f54a085SPoul-Henning Kamp 			return;
9743f54a085SPoul-Henning Kamp 		if (strcmp(n, fd_suffix[i].match))
9753f54a085SPoul-Henning Kamp 			continue;
9763f54a085SPoul-Henning Kamp 		d = fd_suffix[i].minor;
9773f54a085SPoul-Henning Kamp 		break;
9783f54a085SPoul-Henning Kamp 	}
9793f54a085SPoul-Henning Kamp 	if (fd_suffix[i].link == 0) {
9803f54a085SPoul-Henning Kamp 		*dev = make_dev(&fd_cdevsw, (u << 6) + d,
9813f54a085SPoul-Henning Kamp 			UID_ROOT, GID_OPERATOR, 0640, name);
9823f54a085SPoul-Henning Kamp 	} else {
9833f54a085SPoul-Henning Kamp 		pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d);
9843f54a085SPoul-Henning Kamp 		*dev = make_dev_alias(pdev, name);
9853f54a085SPoul-Henning Kamp 	}
9863f54a085SPoul-Henning Kamp }
9873f54a085SPoul-Henning Kamp 
98816e68fc6SPeter Wemm /******************************************************************/
98916e68fc6SPeter Wemm /*
99016e68fc6SPeter Wemm  * devices attached to the controller section.
99116e68fc6SPeter Wemm  */
9926182fdbdSPeter Wemm static int
9936182fdbdSPeter Wemm fd_probe(device_t dev)
9946182fdbdSPeter Wemm {
9956182fdbdSPeter Wemm 	int	i;
9966182fdbdSPeter Wemm 	u_int	fdt, st0, st3;
9976182fdbdSPeter Wemm 	struct	fd_data *fd;
9986182fdbdSPeter Wemm 	struct	fdc_data *fdc;
9996182fdbdSPeter Wemm 	fdsu_t	fdsu;
10006182fdbdSPeter Wemm 	static int fd_fifo = 0;
10016182fdbdSPeter Wemm 
10026182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
10036182fdbdSPeter Wemm 	fd = device_get_softc(dev);
10046182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
10056182fdbdSPeter Wemm 
10066182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
10076182fdbdSPeter Wemm 	fd->dev = dev;
10086182fdbdSPeter Wemm 	fd->fdc = fdc;
10096182fdbdSPeter Wemm 	fd->fdsu = fdsu;
10106182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
10116182fdbdSPeter Wemm 
1012a97c75b7SDoug Rabson #ifdef __i386__
1013b99f0a4aSAndrew Moore 	/* look up what bios thinks we have */
10146182fdbdSPeter Wemm 	switch (fd->fdu) {
10156182fdbdSPeter Wemm 	case 0:
10165f830ea2SJoerg Wunsch 		if ((fdc->flags & FDC_ISPCMCIA))
10175f830ea2SJoerg Wunsch 			fdt = RTCFDT_144M;
10185f830ea2SJoerg Wunsch 		else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
10190722d6abSJoerg Wunsch 			fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
10200722d6abSJoerg Wunsch 		else
10210722d6abSJoerg Wunsch 			fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1022b99f0a4aSAndrew Moore 		break;
10236182fdbdSPeter Wemm 	case 1:
10246182fdbdSPeter Wemm 		fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1025dc5df763SJoerg Wunsch 		break;
1026dc5df763SJoerg Wunsch 	default:
10276182fdbdSPeter Wemm 		fdt = RTCFDT_NONE;
1028dc5df763SJoerg Wunsch 		break;
10296b7bd95bSJoerg Wunsch 	}
1030a97c75b7SDoug Rabson #else
1031a97c75b7SDoug Rabson 	fdt = RTCFDT_144M;	/* XXX probably */
1032a97c75b7SDoug Rabson #endif
10336182fdbdSPeter Wemm 
10346182fdbdSPeter Wemm 	/* is there a unit? */
10356182fdbdSPeter Wemm 	if (fdt == RTCFDT_NONE)
10366182fdbdSPeter Wemm 		return (ENXIO);
10376182fdbdSPeter Wemm 
10386182fdbdSPeter Wemm 	/* select it */
10396182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
10406182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
10416182fdbdSPeter Wemm 
10428de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
10436182fdbdSPeter Wemm 	if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1044e34c71eaSJoerg Wunsch 	    && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
10456182fdbdSPeter Wemm 	    && enable_fifo(fdc) == 0) {
1046b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1047b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1048d66c374fSTor Egge 	}
10496182fdbdSPeter Wemm 	fd_fifo = 1;
10506182fdbdSPeter Wemm 
10516182fdbdSPeter Wemm 	if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
10526182fdbdSPeter Wemm 	    && (st3 & NE7_ST3_T0)) {
1053dc5df763SJoerg Wunsch 		/* if at track 0, first seek inwards */
1054dc5df763SJoerg Wunsch 		/* seek some steps: */
10556182fdbdSPeter Wemm 		fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1056dc5df763SJoerg Wunsch 		DELAY(300000); /* ...wait a moment... */
10576182fdbdSPeter Wemm 		fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1058dc5df763SJoerg Wunsch 	}
1059dc5df763SJoerg Wunsch 
1060dc5df763SJoerg Wunsch 	/* If we're at track 0 first seek inwards. */
10616182fdbdSPeter Wemm 	if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1062dc5df763SJoerg Wunsch 		/* Seek some steps... */
10636182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1064dc5df763SJoerg Wunsch 			/* ...wait a moment... */
1065dc5df763SJoerg Wunsch 			DELAY(300000);
1066dc5df763SJoerg Wunsch 			/* make ctrlr happy: */
10676182fdbdSPeter Wemm 			fd_sense_int(fdc, 0, 0);
1068dc5df763SJoerg Wunsch 		}
1069dc5df763SJoerg Wunsch 	}
1070dc5df763SJoerg Wunsch 
10716b7bd95bSJoerg Wunsch 	for (i = 0; i < 2; i++) {
10726b7bd95bSJoerg Wunsch 		/*
10736b7bd95bSJoerg Wunsch 		 * we must recalibrate twice, just in case the
10746b7bd95bSJoerg Wunsch 		 * heads have been beyond cylinder 76, since most
10756b7bd95bSJoerg Wunsch 		 * FDCs still barf when attempting to recalibrate
10766b7bd95bSJoerg Wunsch 		 * more than 77 steps
10776b7bd95bSJoerg Wunsch 		 */
1078dc5df763SJoerg Wunsch 		/* go back to 0: */
10796182fdbdSPeter Wemm 		if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
10806b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek*/
10816b7bd95bSJoerg Wunsch 			DELAY(i == 0 ? 1000000 : 300000);
10825b81b6b3SRodney W. Grimes 
10836b7bd95bSJoerg Wunsch 			/* anything responding? */
1084dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, 0) == 0 &&
1085dc5df763SJoerg Wunsch 			    (st0 & NE7_ST0_EC) == 0)
10866b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
10876b7bd95bSJoerg Wunsch 		}
1088dc5df763SJoerg Wunsch 	}
10896b7bd95bSJoerg Wunsch 
10906182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
10913a2f7427SDavid Greenman 
10923a2f7427SDavid Greenman 	if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
10936182fdbdSPeter Wemm 		return (ENXIO);
10945b81b6b3SRodney W. Grimes 
1095dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1096b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1097b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
10983a2f7427SDavid Greenman 	fd->options = 0;
109902a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
110002a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
11015b81b6b3SRodney W. Grimes 
1102b99f0a4aSAndrew Moore 	switch (fdt) {
11037ca0641bSAndrey A. Chernov 	case RTCFDT_12M:
11046182fdbdSPeter Wemm 		device_set_desc(dev, "1200-KB 5.25\" drive");
1105b99f0a4aSAndrew Moore 		fd->type = FD_1200;
11067ca0641bSAndrey A. Chernov 		break;
11070722d6abSJoerg Wunsch 	case RTCFDT_144M | RTCFDT_144M_PRETENDED:
11086182fdbdSPeter Wemm 		device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
11090722d6abSJoerg Wunsch 		fdt = RTCFDT_144M;
11106182fdbdSPeter Wemm 		fd->type = FD_1440;
11117ca0641bSAndrey A. Chernov 	case RTCFDT_144M:
11126182fdbdSPeter Wemm 		device_set_desc(dev, "1440-KB 3.5\" drive");
1113b99f0a4aSAndrew Moore 		fd->type = FD_1440;
11147ca0641bSAndrey A. Chernov 		break;
1115290dd077SJoerg Wunsch 	case RTCFDT_288M:
111686a727d9SJoerg Wunsch 	case RTCFDT_288M_1:
11176182fdbdSPeter Wemm 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1118290dd077SJoerg Wunsch 		fd->type = FD_1440;
1119290dd077SJoerg Wunsch 		break;
11207ca0641bSAndrey A. Chernov 	case RTCFDT_360K:
11216182fdbdSPeter Wemm 		device_set_desc(dev, "360-KB 5.25\" drive");
1122b99f0a4aSAndrew Moore 		fd->type = FD_360;
11237ca0641bSAndrey A. Chernov 		break;
1124ed2fa05eSAndrey A. Chernov 	case RTCFDT_720K:
11256182fdbdSPeter Wemm 		printf("720-KB 3.5\" drive");
1126b99f0a4aSAndrew Moore 		fd->type = FD_720;
1127ed2fa05eSAndrey A. Chernov 		break;
11287ca0641bSAndrey A. Chernov 	default:
11296182fdbdSPeter Wemm 		return (ENXIO);
11305b81b6b3SRodney W. Grimes 	}
11316182fdbdSPeter Wemm 	return (0);
11326182fdbdSPeter Wemm }
11336182fdbdSPeter Wemm 
11346182fdbdSPeter Wemm static int
11356182fdbdSPeter Wemm fd_attach(device_t dev)
11366182fdbdSPeter Wemm {
11376182fdbdSPeter Wemm 	struct	fd_data *fd;
1138db901281SPoul-Henning Kamp 	static int cdevsw_add_done = 0;
11396182fdbdSPeter Wemm 
11406182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11416182fdbdSPeter Wemm 
114202211baeSNick Hibma 	if (!cdevsw_add_done) {
1143475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
114402211baeSNick Hibma 		cdevsw_add_done++;
114502211baeSNick Hibma 	}
1146db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000);
11473f54a085SPoul-Henning Kamp 	make_dev(&fd_cdevsw, (fd->fdu << 6),
11483f54a085SPoul-Henning Kamp 		UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1149ada9bd8cSJulian Elischer 
1150671e2ceeSBruce Evans 	/*
1151b2dfb1f9SJustin T. Gibbs 	 * Export the drive to the devstat interface.
1152671e2ceeSBruce Evans 	 */
11536182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
11546182fdbdSPeter Wemm 			  device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
11552a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
11562a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
11576182fdbdSPeter Wemm 	return (0);
11585b81b6b3SRodney W. Grimes }
11595b81b6b3SRodney W. Grimes 
11605f830ea2SJoerg Wunsch static int
11615f830ea2SJoerg Wunsch fd_detach(device_t dev)
11625f830ea2SJoerg Wunsch {
11635f830ea2SJoerg Wunsch 	struct	fd_data *fd;
11645f830ea2SJoerg Wunsch 
11655f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
11665f830ea2SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
11675f830ea2SJoerg Wunsch 
11685f830ea2SJoerg Wunsch 	return (0);
11695f830ea2SJoerg Wunsch }
11705f830ea2SJoerg Wunsch 
117116e68fc6SPeter Wemm static device_method_t fd_methods[] = {
117216e68fc6SPeter Wemm 	/* Device interface */
117316e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
117416e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
11755f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
117616e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
117716e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
117816e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
117916e68fc6SPeter Wemm 
118016e68fc6SPeter Wemm 	{ 0, 0 }
118116e68fc6SPeter Wemm };
118216e68fc6SPeter Wemm 
118316e68fc6SPeter Wemm static driver_t fd_driver = {
118416e68fc6SPeter Wemm 	"fd",
118516e68fc6SPeter Wemm 	fd_methods,
118616e68fc6SPeter Wemm 	sizeof(struct fd_data)
118716e68fc6SPeter Wemm };
118816e68fc6SPeter Wemm 
1189475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
119016e68fc6SPeter Wemm 
11915b81b6b3SRodney W. Grimes /****************************************************************************/
11925b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
11935e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
11945b81b6b3SRodney W. Grimes /****************************************************************************/
11953a2f7427SDavid Greenman static void
11966182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
11975b81b6b3SRodney W. Grimes {
11986182fdbdSPeter Wemm 	int fdout = fdc->fdout;
11993a2f7427SDavid Greenman 	int needspecify = 0;
12003a2f7427SDavid Greenman 
12013a2f7427SDavid Greenman 	if(turnon) {
12023a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
12033a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
12043a2f7427SDavid Greenman 	} else
12053a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
12063a2f7427SDavid Greenman 
12075e235068SJordan K. Hubbard 	if(!turnon
12085e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
12095e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
12105e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
12115e235068SJordan K. Hubbard 	else {
12123a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
12133a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
12143a2f7427SDavid Greenman 			needspecify = 1;
12153a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
12165b81b6b3SRodney W. Grimes 	}
12175b81b6b3SRodney W. Grimes 
1218427ccf00SDoug Rabson 	fdout_wr(fdc, fdout);
12196182fdbdSPeter Wemm 	fdc->fdout = fdout;
12203a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
12213a2f7427SDavid Greenman 
12223a2f7427SDavid Greenman 	if (needspecify) {
1223dc8603e3SJoerg Wunsch 		/*
1224dc5df763SJoerg Wunsch 		 * XXX
1225dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
1226dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
1227dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
1228dc8603e3SJoerg Wunsch 		 */
12296182fdbdSPeter Wemm 		(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1230dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1231dc5df763SJoerg Wunsch 			     0);
12326182fdbdSPeter Wemm 		if (fdc->flags & FDC_HAS_FIFO)
12336182fdbdSPeter Wemm 			(void) enable_fifo(fdc);
12343a2f7427SDavid Greenman 	}
12353a2f7427SDavid Greenman }
12363a2f7427SDavid Greenman 
1237381fe1aaSGarrett Wollman static void
12386182fdbdSPeter Wemm fd_turnoff(void *xfd)
12395b81b6b3SRodney W. Grimes {
1240f5f7ba03SJordan K. Hubbard 	int	s;
12416182fdbdSPeter Wemm 	fd_p fd = xfd;
1242dc16046fSJoerg Wunsch 
12436182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
12448335c1b8SBruce Evans 
12455f830ea2SJoerg Wunsch 	s = splbio();
12468335c1b8SBruce Evans 	/*
12478335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
12485f830ea2SJoerg Wunsch 	 *
12495f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
12505f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
12515f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
12525f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
12538335c1b8SBruce Evans 	 */
12546182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
12555f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
12565f830ea2SJoerg Wunsch 		splx(s);
12578335c1b8SBruce Evans 		return;
12588335c1b8SBruce Evans 	}
12598335c1b8SBruce Evans 
12605b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
12616182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1262f5f7ba03SJordan K. Hubbard 	splx(s);
12635b81b6b3SRodney W. Grimes }
12645b81b6b3SRodney W. Grimes 
12653a2f7427SDavid Greenman static void
12666182fdbdSPeter Wemm fd_motor_on(void *xfd)
12675b81b6b3SRodney W. Grimes {
1268f5f7ba03SJordan K. Hubbard 	int	s;
12696182fdbdSPeter Wemm 	fd_p fd = xfd;
1270f5f7ba03SJordan K. Hubbard 
1271f5f7ba03SJordan K. Hubbard 	s = splbio();
12725b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
12735b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
12745b81b6b3SRodney W. Grimes 	{
12756182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
12765b81b6b3SRodney W. Grimes 	}
1277f5f7ba03SJordan K. Hubbard 	splx(s);
12785b81b6b3SRodney W. Grimes }
12795b81b6b3SRodney W. Grimes 
12803a2f7427SDavid Greenman static void
12816182fdbdSPeter Wemm fd_turnon(fd_p fd)
12825b81b6b3SRodney W. Grimes {
12835b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
12845b81b6b3SRodney W. Grimes 	{
12853a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
12866182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
12876182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
12885b81b6b3SRodney W. Grimes 	}
12895b81b6b3SRodney W. Grimes }
12905b81b6b3SRodney W. Grimes 
1291381fe1aaSGarrett Wollman static void
1292dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
12935b81b6b3SRodney W. Grimes {
12943a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1295427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
12963a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
12973a2f7427SDavid Greenman 	DELAY(100);
12983a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1299427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
13003a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
13013a2f7427SDavid Greenman 	DELAY(100);
1302427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
13033a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
13043a2f7427SDavid Greenman 
1305dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
13066182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1307dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1308dc5df763SJoerg Wunsch 		     0);
1309d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1310d66c374fSTor Egge 		(void) enable_fifo(fdc);
13115b81b6b3SRodney W. Grimes }
13125b81b6b3SRodney W. Grimes 
13135b81b6b3SRodney W. Grimes /****************************************************************************/
13145b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
13155b81b6b3SRodney W. Grimes /****************************************************************************/
13165b81b6b3SRodney W. Grimes int
13176182fdbdSPeter Wemm in_fdc(struct fdc_data *fdc)
13185b81b6b3SRodney W. Grimes {
13195b81b6b3SRodney W. Grimes 	int i, j = 100000;
1320427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
13215b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1322dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13236182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
13245b81b6b3SRodney W. Grimes 	if (j <= 0)
13256182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1326d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1327427ccf00SDoug Rabson 	i = fddata_rd(fdc);
13283a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
13295b81b6b3SRodney W. Grimes 	return(i);
1330d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1331427ccf00SDoug Rabson 	return fddata_rd(fdc);
1332d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
13335b81b6b3SRodney W. Grimes }
13345b81b6b3SRodney W. Grimes 
1335dc5df763SJoerg Wunsch /*
1336dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
1337dc5df763SJoerg Wunsch  */
1338b5e8ce9fSBruce Evans static int
13396182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1340dc5df763SJoerg Wunsch {
1341dc5df763SJoerg Wunsch 	int i, j = 100000;
1342427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1343dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1344dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13456182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1346dc5df763SJoerg Wunsch 	if (j <= 0)
13476182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1348d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1349427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1350dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1351dc5df763SJoerg Wunsch 	*ptr = i;
1352dc5df763SJoerg Wunsch 	return 0;
1353d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1354427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1355dc5df763SJoerg Wunsch 	if (ptr)
1356dc5df763SJoerg Wunsch 		*ptr = i;
1357dc5df763SJoerg Wunsch 	return 0;
1358d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1359dc5df763SJoerg Wunsch }
1360dc5df763SJoerg Wunsch 
1361dc5df763SJoerg Wunsch int
13626182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
13635b81b6b3SRodney W. Grimes {
13643b3837dbSRodney W. Grimes 	int i;
13655b81b6b3SRodney W. Grimes 
13663b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
13673b3837dbSRodney W. Grimes 	i = 100000;
1368427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
13696182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
13703b3837dbSRodney W. Grimes 
13713b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
13723b3837dbSRodney W. Grimes 	i = 100000;
1373427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
137416b04b6aSJoerg Wunsch 	if (i <= 0)
13756182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
13763b3837dbSRodney W. Grimes 
13773b3837dbSRodney W. Grimes 	/* Send the command and return */
1378427ccf00SDoug Rabson 	fddata_wr(fdc, x);
13793a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
13805b81b6b3SRodney W. Grimes 	return (0);
13815b81b6b3SRodney W. Grimes }
13825b81b6b3SRodney W. Grimes 
13835b81b6b3SRodney W. Grimes /****************************************************************************/
13845b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
13855b81b6b3SRodney W. Grimes /****************************************************************************/
1386381fe1aaSGarrett Wollman int
1387671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
13885b81b6b3SRodney W. Grimes {
13895b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
139020a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
13916182fdbdSPeter Wemm 	fd_p	fd;
1392b99f0a4aSAndrew Moore 	fdc_p	fdc;
13935b81b6b3SRodney W. Grimes 
13945b81b6b3SRodney W. Grimes 	/* check bounds */
13956182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1396b99f0a4aSAndrew Moore 		return (ENXIO);
13976182fdbdSPeter Wemm 	fdc = fd->fdc;
13986182fdbdSPeter Wemm 	if ((fdc == NULL) || (fd->type == NO_TYPE))
1399b99f0a4aSAndrew Moore 		return (ENXIO);
1400b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1401b99f0a4aSAndrew Moore 		return (ENXIO);
14027ca0641bSAndrey A. Chernov 	if (type == 0)
14036182fdbdSPeter Wemm 		type = fd->type;
14047ca0641bSAndrey A. Chernov 	else {
14053e425b96SJulian Elischer 		/*
14063e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
14073e425b96SJulian Elischer 		 * to open a type it can do,
14083e425b96SJulian Elischer 		 */
14096182fdbdSPeter Wemm 		if (type != fd->type) {
14106182fdbdSPeter Wemm 			switch (fd->type) {
14117ca0641bSAndrey A. Chernov 			case FD_360:
14127ca0641bSAndrey A. Chernov 				return (ENXIO);
1413ed2fa05eSAndrey A. Chernov 			case FD_720:
1414b39c878eSAndrey A. Chernov 				if (   type != FD_820
1415b39c878eSAndrey A. Chernov 				    && type != FD_800
14166fb89845SKATO Takenori 				    && type != FD_640
1417ed2fa05eSAndrey A. Chernov 				   )
1418ed2fa05eSAndrey A. Chernov 					return (ENXIO);
1419ed2fa05eSAndrey A. Chernov 				break;
14207ca0641bSAndrey A. Chernov 			case FD_1200:
1421b39c878eSAndrey A. Chernov 				switch (type) {
1422b39c878eSAndrey A. Chernov 				case FD_1480:
1423b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1424fa4700b4SAndrey A. Chernov 					break;
14257ca0641bSAndrey A. Chernov 				case FD_1440:
1426b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1427b39c878eSAndrey A. Chernov 					break;
14286fb89845SKATO Takenori 				case FD_1232:
14296fb89845SKATO Takenori 					break;
1430b39c878eSAndrey A. Chernov 				case FD_820:
1431b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1432b39c878eSAndrey A. Chernov 					break;
1433b39c878eSAndrey A. Chernov 				case FD_800:
1434b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1435b39c878eSAndrey A. Chernov 					break;
1436b39c878eSAndrey A. Chernov 				case FD_720:
1437b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1438b39c878eSAndrey A. Chernov 					break;
14396fb89845SKATO Takenori 				case FD_640:
14406fb89845SKATO Takenori 					type = FD_640in5_25;
14416fb89845SKATO Takenori 					break;
1442b39c878eSAndrey A. Chernov 				case FD_360:
1443b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1444b39c878eSAndrey A. Chernov 					break;
1445b39c878eSAndrey A. Chernov 				default:
1446b39c878eSAndrey A. Chernov 					return(ENXIO);
1447b39c878eSAndrey A. Chernov 				}
1448b39c878eSAndrey A. Chernov 				break;
1449b39c878eSAndrey A. Chernov 			case FD_1440:
1450b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1451b39c878eSAndrey A. Chernov 				    && type != FD_1480
1452ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1453b39c878eSAndrey A. Chernov 				    && type != FD_820
1454b39c878eSAndrey A. Chernov 				    && type != FD_800
1455b39c878eSAndrey A. Chernov 				    && type != FD_720
14566fb89845SKATO Takenori 				    && type != FD_640
14577ca0641bSAndrey A. Chernov 				    )
1458dffff499SAndrey A. Chernov 					return(ENXIO);
1459fa4700b4SAndrey A. Chernov 				break;
14607ca0641bSAndrey A. Chernov 			}
14617ca0641bSAndrey A. Chernov 		}
1462fa4700b4SAndrey A. Chernov 	}
14636182fdbdSPeter Wemm 	fd->ft = fd_types + type - 1;
14646182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
14655f830ea2SJoerg Wunsch 
14665b81b6b3SRodney W. Grimes 	return 0;
14675b81b6b3SRodney W. Grimes }
14685b81b6b3SRodney W. Grimes 
1469381fe1aaSGarrett Wollman int
1470671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
14715b81b6b3SRodney W. Grimes {
14725b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
14736182fdbdSPeter Wemm 	struct fd_data *fd;
1474b99f0a4aSAndrew Moore 
14756182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
14766182fdbdSPeter Wemm 	fd->flags &= ~FD_OPEN;
14772995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1478dc16046fSJoerg Wunsch 
14795b81b6b3SRodney W. Grimes 	return (0);
14805b81b6b3SRodney W. Grimes }
14815b81b6b3SRodney W. Grimes 
14823a2f7427SDavid Greenman /****************************************************************************/
14833a2f7427SDavid Greenman /*                               fdstrategy                                 */
14843a2f7427SDavid Greenman /****************************************************************************/
14853a2f7427SDavid Greenman void
14868177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
14873a2f7427SDavid Greenman {
1488bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
14893a2f7427SDavid Greenman  	int	s;
14903a2f7427SDavid Greenman  	fdu_t	fdu;
14913a2f7427SDavid Greenman  	fdc_p	fdc;
14923a2f7427SDavid Greenman  	fd_p	fd;
14933a2f7427SDavid Greenman 	size_t	fdblk;
14943a2f7427SDavid Greenman 
14958177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
14966182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
14976182fdbdSPeter Wemm 	if (fd == 0)
14986182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
14998177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
15003a2f7427SDavid Greenman 	fdc = fd->fdc;
150169acd21dSWarner Losh 	if (fd->type == NO_TYPE) {
15028177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
15038177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15043b178206SWarner Losh 		goto bad;
150569acd21dSWarner Losh 	};
15063a2f7427SDavid Greenman 
1507d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
15088177437dSPoul-Henning Kamp 	if (!(bp->bio_cmd & BIO_FORMAT)) {
15098177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1510dc5df763SJoerg Wunsch 			printf(
15116a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
15128177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
15138177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15148177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15153a2f7427SDavid Greenman 			goto bad;
15163a2f7427SDavid Greenman 		}
15178177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
15188177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15198177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15203a2f7427SDavid Greenman 			goto bad;
15213a2f7427SDavid Greenman 		}
15223a2f7427SDavid Greenman 	}
15233a2f7427SDavid Greenman 
15243a2f7427SDavid Greenman 	/*
15253a2f7427SDavid Greenman 	 * Set up block calculations.
15263a2f7427SDavid Greenman 	 */
15278177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1528bb6382faSJoerg Wunsch 		/*
1529bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1530bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1531bb6382faSJoerg Wunsch 		 */
15328177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
15338177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15343a2f7427SDavid Greenman 		goto bad;
15353a2f7427SDavid Greenman 	}
15368177437dSPoul-Henning Kamp 	blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk;
1537bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
15388177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
15398177437dSPoul-Henning Kamp 	if (blknum + (bp->bio_bcount / fdblk) > nblocks) {
1540bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1541bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
15428177437dSPoul-Henning Kamp 			bp->bio_resid = bp->bio_bcount - cando;
1543bb6382faSJoerg Wunsch 			if (cando == 0)
1544bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1545bb6382faSJoerg Wunsch 		} else {
15468177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15478177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
1548bb6382faSJoerg Wunsch 			goto bad;
1549bb6382faSJoerg Wunsch 		}
1550bb6382faSJoerg Wunsch 	}
15518177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
15523a2f7427SDavid Greenman 	s = splbio();
15538177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
15546182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1555b2dfb1f9SJustin T. Gibbs 
1556b2dfb1f9SJustin T. Gibbs 	/* Tell devstat we are starting on the transaction */
1557b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
15585f830ea2SJoerg Wunsch 	device_busy(fd->dev);
1559b2dfb1f9SJustin T. Gibbs 
15606182fdbdSPeter Wemm 	fdstart(fdc);
15613a2f7427SDavid Greenman 	splx(s);
15623a2f7427SDavid Greenman 	return;
15633a2f7427SDavid Greenman 
15643a2f7427SDavid Greenman bad:
15653a2f7427SDavid Greenman 	biodone(bp);
15663a2f7427SDavid Greenman }
15673a2f7427SDavid Greenman 
15685b81b6b3SRodney W. Grimes /***************************************************************\
15695b81b6b3SRodney W. Grimes *				fdstart				*
15705b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
15715b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
15725b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
15735b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
15745b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
15755b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
15765b81b6b3SRodney W. Grimes \***************************************************************/
1577381fe1aaSGarrett Wollman static void
15786182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
15795b81b6b3SRodney W. Grimes {
15805b81b6b3SRodney W. Grimes 	int s;
15815b81b6b3SRodney W. Grimes 
15825b81b6b3SRodney W. Grimes 	s = splbio();
15836182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
15845b81b6b3SRodney W. Grimes 	{
15856182fdbdSPeter Wemm 		fdc_intr(fdc);
15865b81b6b3SRodney W. Grimes 	}
15875b81b6b3SRodney W. Grimes 	splx(s);
15885b81b6b3SRodney W. Grimes }
15895b81b6b3SRodney W. Grimes 
1590381fe1aaSGarrett Wollman static void
15916182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
15925b81b6b3SRodney W. Grimes {
15935c1a1eaeSBruce Evans  	fdc_p fdc;
1594f5f7ba03SJordan K. Hubbard 	int s;
15955b81b6b3SRodney W. Grimes 
15966182fdbdSPeter Wemm 	fdc = xfdc;
15975c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
15985b81b6b3SRodney W. Grimes 
15993a2f7427SDavid Greenman 	/*
16003a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
16013a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
16023a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
16033a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
16043a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
16055c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
16065c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
16075c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
16085c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
16093a2f7427SDavid Greenman 	 */
16103a2f7427SDavid Greenman 	s = splbio();
16115c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
16125c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
16135c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
16146182fdbdSPeter Wemm 	fdc_intr(fdc);
1615f5f7ba03SJordan K. Hubbard 	splx(s);
16165b81b6b3SRodney W. Grimes }
16175b81b6b3SRodney W. Grimes 
16185b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1619381fe1aaSGarrett Wollman static void
16206182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
16215b81b6b3SRodney W. Grimes {
16225b81b6b3SRodney W. Grimes 	int	s;
16233a2f7427SDavid Greenman 
16245b81b6b3SRodney W. Grimes 	s = splbio();
16256182fdbdSPeter Wemm 	fdc_intr(xfdc);
16265b81b6b3SRodney W. Grimes 	splx(s);
16275b81b6b3SRodney W. Grimes }
16285b81b6b3SRodney W. Grimes 
16295b81b6b3SRodney W. Grimes /***********************************************************************\
16305b81b6b3SRodney W. Grimes *                                 fdintr				*
16315b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
16325b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
16335b81b6b3SRodney W. Grimes \***********************************************************************/
1634fe310de8SBruce Evans static void
16356182fdbdSPeter Wemm fdc_intr(void *xfdc)
16365b81b6b3SRodney W. Grimes {
16376182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
16386182fdbdSPeter Wemm 	while(fdstate(fdc))
1639381fe1aaSGarrett Wollman 		;
16405b81b6b3SRodney W. Grimes }
16415b81b6b3SRodney W. Grimes 
164269acd21dSWarner Losh /*
164369acd21dSWarner Losh  * magic pseudo-DMA initialization for YE FDC. Sets count and
164469acd21dSWarner Losh  * direction
164569acd21dSWarner Losh  */
16463b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
16473b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
16483b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
16493b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
16503b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
165169acd21dSWarner Losh 
165269acd21dSWarner Losh /*
165369acd21dSWarner Losh  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
165469acd21dSWarner Losh  */
16553b178206SWarner Losh static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
165669acd21dSWarner Losh {
165769acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
165869acd21dSWarner Losh 
165921144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
166069acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
166169acd21dSWarner Losh 			fdc->state = PIOREAD;
166269acd21dSWarner Losh 			return(0);
166369acd21dSWarner Losh 		};
16643b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
16653b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16663b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
166769acd21dSWarner Losh 	} else {
16683b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16693b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
16703b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
167169acd21dSWarner Losh 	};
167269acd21dSWarner Losh 	return(1);
167369acd21dSWarner Losh }
167469acd21dSWarner Losh 
16755b81b6b3SRodney W. Grimes /***********************************************************************\
16765b81b6b3SRodney W. Grimes * The controller state machine.						*
16775b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
16785b81b6b3SRodney W. Grimes \***********************************************************************/
16793a2f7427SDavid Greenman static int
16806182fdbdSPeter Wemm fdstate(fdc_p fdc)
16815b81b6b3SRodney W. Grimes {
168256a23089SPoul-Henning Kamp 	int read, format, head, i, sec = 0, sectrac, st0, cyl, st3, idf;
1683bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
16845b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
16855b81b6b3SRodney W. Grimes 	fd_p fd;
16868177437dSPoul-Henning Kamp 	register struct bio *bp;
1687b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
16883a2f7427SDavid Greenman 	size_t fdblk;
16895b81b6b3SRodney W. Grimes 
1690e93e63cbSBruce Evans 	bp = fdc->bp;
1691e93e63cbSBruce Evans 	if (bp == NULL) {
16928177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1693e93e63cbSBruce Evans 		if (bp != NULL) {
16948177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1695e93e63cbSBruce Evans 			fdc->bp = bp;
1696e93e63cbSBruce Evans 		}
1697e93e63cbSBruce Evans 	}
1698e93e63cbSBruce Evans 	if (bp == NULL) {
16995b81b6b3SRodney W. Grimes 		/***********************************************\
17005b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
17015b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
17025b81b6b3SRodney W. Grimes 		\***********************************************/
17035b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
17046182fdbdSPeter Wemm 		if (fdc->fd) {
1705b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1706b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
17075b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17085b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17095b81b6b3SRodney W. Grimes 		}
17106182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
17115b81b6b3SRodney W. Grimes  		return (0);
17125b81b6b3SRodney W. Grimes 	}
17138177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
17146182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
17153a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1716b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1717b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
17188177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
171956a23089SPoul-Henning Kamp 	if (read)
172056a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
172156a23089SPoul-Henning Kamp 	else
172256a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
17238177437dSPoul-Henning Kamp 	format = bp->bio_cmd & BIO_FORMAT;
1724bb6382faSJoerg Wunsch 	if (format) {
17258177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
1726bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1727bb6382faSJoerg Wunsch 			- (char *)finfo;
1728bb6382faSJoerg Wunsch 	}
1729bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
17308177437dSPoul-Henning Kamp 		blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk +
1731bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1732bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1733bb6382faSJoerg Wunsch 	}
17345b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
17355b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
17365b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
17376182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
17386182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
17395b81b6b3SRodney W. Grimes 	switch (fdc->state)
17405b81b6b3SRodney W. Grimes 	{
17415b81b6b3SRodney W. Grimes 	case DEVIDLE:
17425b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
17435b81b6b3SRodney W. Grimes 		fdc->retry = 0;
17445b81b6b3SRodney W. Grimes 		fd->skip = 0;
17455b81b6b3SRodney W. Grimes 		fdc->fd = fd;
17465b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
17475f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
17483a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
17495b81b6b3SRodney W. Grimes 		/*******************************************************\
17505b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1751dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
17525b81b6b3SRodney W. Grimes 		\*******************************************************/
17536182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
17545b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17555b81b6b3SRodney W. Grimes 			return (0); /* come back later */
17565b81b6b3SRodney W. Grimes 		}
17575b81b6b3SRodney W. Grimes 		/*******************************************************\
17585b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
17595b81b6b3SRodney W. Grimes 		\*******************************************************/
17605b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
17615b81b6b3SRodney W. Grimes 		{
17625b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17636182fdbdSPeter Wemm 			fd_turnon(fd);
17645b81b6b3SRodney W. Grimes 			return (0);
17655b81b6b3SRodney W. Grimes 		}
17665b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
17675b81b6b3SRodney W. Grimes 		{
17686182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
17695b81b6b3SRodney W. Grimes 		}
17705c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17715c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17725c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17735c1a1eaeSBruce Evans 		} else
17745e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
17755b81b6b3SRodney W. Grimes 		break;
17765b81b6b3SRodney W. Grimes 	case DOSEEK:
1777bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
17785b81b6b3SRodney W. Grimes 		{
17795b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
17805b81b6b3SRodney W. Grimes 			break;
17815b81b6b3SRodney W. Grimes 		}
17826182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1783bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1784dc5df763SJoerg Wunsch 			   0))
1785dc8603e3SJoerg Wunsch 		{
1786dc8603e3SJoerg Wunsch 			/*
1787dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1788dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1789dc8603e3SJoerg Wunsch 			 */
1790dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
17916182fdbdSPeter Wemm 			return(retrier(fdc));
1792dc8603e3SJoerg Wunsch 		}
1793dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
17945b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
17955b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17965b81b6b3SRodney W. Grimes 	case SEEKWAIT:
17975b81b6b3SRodney W. Grimes 		/* allow heads to settle */
17986182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
17995b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
18005b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18015b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
18025b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
18036182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
1804bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
18053a2f7427SDavid Greenman 			do {
18063a2f7427SDavid Greenman 				/*
1807dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1808dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1809dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1810dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1811dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1812dc5df763SJoerg Wunsch 				 *
1813dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1814dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1815dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1816dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1817dc5df763SJoerg Wunsch 				 * real interrupt condition.
1818dc5df763SJoerg Wunsch 				 *
1819dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1820dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1821dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1822dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1823dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1824dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1825dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
18263a2f7427SDavid Greenman 				 */
1827dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1828dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1829dc5df763SJoerg Wunsch 					return 0;
1830dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1831dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1832dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
18333a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1834dc5df763SJoerg Wunsch 
18356182fdbdSPeter Wemm 			if (0 == descyl) {
1836dc5df763SJoerg Wunsch 				int failed = 0;
18373a2f7427SDavid Greenman 				/*
18383a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
18393a2f7427SDavid Greenman 				 * really there
18403a2f7427SDavid Greenman 				 */
1841dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1842dc5df763SJoerg Wunsch 					failed = 1;
18433a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
18443a2f7427SDavid Greenman 					printf(
18453a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
18463a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1847dc5df763SJoerg Wunsch 					failed = 1;
1848dc5df763SJoerg Wunsch 				}
1849dc5df763SJoerg Wunsch 
18506182fdbdSPeter Wemm 				if (failed) {
18513a2f7427SDavid Greenman 					if(fdc->retry < 3)
18523a2f7427SDavid Greenman 						fdc->retry = 3;
18536182fdbdSPeter Wemm 					return (retrier(fdc));
18543a2f7427SDavid Greenman 				}
18553a2f7427SDavid Greenman 			}
1856dc5df763SJoerg Wunsch 
18576182fdbdSPeter Wemm 			if (cyl != descyl) {
18583a2f7427SDavid Greenman 				printf(
18593a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
18602d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1861e5d7d243SBruce Evans 				if (fdc->retry < 3)
1862e5d7d243SBruce Evans 					fdc->retry = 3;
18636182fdbdSPeter Wemm 				return (retrier(fdc));
18645b81b6b3SRodney W. Grimes 			}
18655b81b6b3SRodney W. Grimes 		}
18665b81b6b3SRodney W. Grimes 
1867bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
18683b178206SWarner Losh 		if (!(fdc->flags & FDC_NODMA))
18698177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
18708177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
18715b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
18725b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
18735b81b6b3SRodney W. Grimes 		head = sec / sectrac;
18745b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
18753a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
18763a2f7427SDavid Greenman 
18773a2f7427SDavid Greenman 		if(format || !read)
18783a2f7427SDavid Greenman 		{
18793a2f7427SDavid Greenman 			/* make sure the drive is writable */
1880dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1881dc8603e3SJoerg Wunsch 			{
1882dc8603e3SJoerg Wunsch 				/* stuck controller? */
18835f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
188456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
18858177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
18868177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
18875c1a1eaeSBruce Evans 						    fdc->dmachan);
1888dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
18896182fdbdSPeter Wemm 				return (retrier(fdc));
1890dc8603e3SJoerg Wunsch 			}
18913a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
18923a2f7427SDavid Greenman 			{
18933a2f7427SDavid Greenman 				/*
18943a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
18953a2f7427SDavid Greenman 				 * in order to force the current operation
18963a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
18973a2f7427SDavid Greenman 				 * error - all error handling is done
18983a2f7427SDavid Greenman 				 * by the retrier()
18993a2f7427SDavid Greenman 				 */
19003a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
19013a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
19023a2f7427SDavid Greenman 				fdc->status[2] = 0;
19033a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
19043a2f7427SDavid Greenman 				fdc->status[4] = head;
19053a2f7427SDavid Greenman 				fdc->status[5] = sec;
19063a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
19073a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
19083a2f7427SDavid Greenman 				return (1);
19093a2f7427SDavid Greenman 			}
19103a2f7427SDavid Greenman 		}
19115b81b6b3SRodney W. Grimes 
19126182fdbdSPeter Wemm 		if (format) {
19135f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
19145f830ea2SJoerg Wunsch 				/*
19155f830ea2SJoerg Wunsch 				 * This seems to be necessary for
19165f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
19175f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
19185f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
19195f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
19205f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
19215f830ea2SJoerg Wunsch 				 * the place when trying to read them
19225f830ea2SJoerg Wunsch 				 * back.
19235f830ea2SJoerg Wunsch 				 *
19245f830ea2SJoerg Wunsch 				 * Umpf.
19255f830ea2SJoerg Wunsch 				 */
19268177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
19275f830ea2SJoerg Wunsch 
19288177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
19298177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
19308177437dSPoul-Henning Kamp 					bp->bio_bcount);
19315f830ea2SJoerg Wunsch 
19325f830ea2SJoerg Wunsch 			}
1933b39c878eSAndrey A. Chernov 			/* formatting */
19346182fdbdSPeter Wemm 			if(fd_cmd(fdc, 6,  NE7CMD_FORMAT, head << 2 | fdu,
1935dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1936dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1937dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
19386182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
1939dc8603e3SJoerg Wunsch 				/* controller fell over */
19405f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
194156a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19428177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19438177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19445c1a1eaeSBruce Evans 						    fdc->dmachan);
1945dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19466182fdbdSPeter Wemm 				return (retrier(fdc));
1947dc8603e3SJoerg Wunsch 			}
19486182fdbdSPeter Wemm 		} else {
19493b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
195069acd21dSWarner Losh 				/*
195169acd21dSWarner Losh 				 * this seems to be necessary even when
195269acd21dSWarner Losh 				 * reading data
195369acd21dSWarner Losh 				 */
19543b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
195569acd21dSWarner Losh 
195669acd21dSWarner Losh 				/*
195769acd21dSWarner Losh 				 * perform the write pseudo-DMA before
195869acd21dSWarner Losh 				 * the WRITE command is sent
195969acd21dSWarner Losh 				 */
196069acd21dSWarner Losh 				if (!read)
19618177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
19628177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
196369acd21dSWarner Losh 					    fdblk);
196469acd21dSWarner Losh 			}
19656182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
1966dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1967dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1968dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1969dc5df763SJoerg Wunsch 				   head,
1970dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1971dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1972dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1973dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1974dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
19756182fdbdSPeter Wemm 				   0)) {
1976dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
19775f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
197856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19798177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19808177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19815c1a1eaeSBruce Evans 						    fdc->dmachan);
1982dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19836182fdbdSPeter Wemm 				return (retrier(fdc));
19845b81b6b3SRodney W. Grimes 			}
1985b39c878eSAndrey A. Chernov 		}
19863b178206SWarner Losh 		if (fdc->flags & FDC_NODMA)
198769acd21dSWarner Losh 			/*
198869acd21dSWarner Losh 			 * if this is a read, then simply await interrupt
198969acd21dSWarner Losh 			 * before performing PIO
199069acd21dSWarner Losh 			 */
19918177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
19928177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
19933b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
199469acd21dSWarner Losh 				return(0);      /* will return later */
199569acd21dSWarner Losh 			};
199669acd21dSWarner Losh 
199769acd21dSWarner Losh 		/*
199869acd21dSWarner Losh 		 * write (or format) operation will fall through and
199969acd21dSWarner Losh 		 * await completion interrupt
200069acd21dSWarner Losh 		 */
20015b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
20026182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
20035b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
200469acd21dSWarner Losh 	case PIOREAD:
200569acd21dSWarner Losh 		/*
200669acd21dSWarner Losh 		 * actually perform the PIO read.  The IOCOMPLETE case
200769acd21dSWarner Losh 		 * removes the timeout for us.
200869acd21dSWarner Losh 		 */
20098177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
201069acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
201169acd21dSWarner Losh 		/* FALLTHROUGH */
20125b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
20136182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2014dc5df763SJoerg Wunsch 
20156182fdbdSPeter Wemm 		if (fd_read_status(fdc, fd->fdsu)) {
20165f830ea2SJoerg Wunsch 			if (!(fdc->flags & FDC_NODMA))
20178177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
20188177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
20195c1a1eaeSBruce Evans 					    fdc->dmachan);
2020dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2021dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
20226182fdbdSPeter Wemm 			return (retrier(fdc));
20235b81b6b3SRodney W. Grimes   		}
2024dc5df763SJoerg Wunsch 
20253a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2026dc5df763SJoerg Wunsch 
20273a2f7427SDavid Greenman 		/* FALLTHROUGH */
2028dc5df763SJoerg Wunsch 
20293a2f7427SDavid Greenman 	case IOTIMEDOUT:
20303b178206SWarner Losh 		if (!(fdc->flags & FDC_NODMA))
20318177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
20328177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
20336182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
20343a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20353a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2036b39c878eSAndrey A. Chernov                                 /*
20373a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
20383a2f7427SDavid Greenman 				 * and didn't release it in time for the
20393a2f7427SDavid Greenman 				 * next FDC transfer.
20403a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
20413a2f7427SDavid Greenman 				 * count. (vak)
2042b39c878eSAndrey A. Chernov                                  */
2043b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
2044b39c878eSAndrey A. Chernov                                 return (1);
2045b39c878eSAndrey A. Chernov                         }
20463a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
20473a2f7427SDavid Greenman 				&& fdc->retry < 6)
20483a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
20493a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20503a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
20513a2f7427SDavid Greenman 				&& fdc->retry < 3)
20523a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
20536182fdbdSPeter Wemm 			return (retrier(fdc));
20545b81b6b3SRodney W. Grimes 		}
20555b81b6b3SRodney W. Grimes 		/* All OK */
20563a2f7427SDavid Greenman 		fd->skip += fdblk;
20578177437dSPoul-Henning Kamp 		if (!format && fd->skip < bp->bio_bcount - bp->bio_resid) {
20585b81b6b3SRodney W. Grimes 			/* set up next transfer */
20595b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
20606182fdbdSPeter Wemm 		} else {
20615b81b6b3SRodney W. Grimes 			/* ALL DONE */
20625b81b6b3SRodney W. Grimes 			fd->skip = 0;
2063e93e63cbSBruce Evans 			fdc->bp = NULL;
20645f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2065a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
20665b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
20675b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
20685b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
20695b81b6b3SRodney W. Grimes 		}
20705b81b6b3SRodney W. Grimes 		return (1);
20715b81b6b3SRodney W. Grimes 	case RESETCTLR:
20723a2f7427SDavid Greenman 		fdc_reset(fdc);
20735b81b6b3SRodney W. Grimes 		fdc->retry++;
20745c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
20755c1a1eaeSBruce Evans 		return (0);
20765c1a1eaeSBruce Evans 	case RESETCOMPLETE:
20775c1a1eaeSBruce Evans 		/*
20785c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
20795c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
20805c1a1eaeSBruce Evans 		 */
20810e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
20820e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
20835c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
20845c1a1eaeSBruce Evans 		/* Fall through. */
20855c1a1eaeSBruce Evans 	case STARTRECAL:
20866182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2087dc8603e3SJoerg Wunsch 			/* arrgl */
2088dc8603e3SJoerg Wunsch 			fdc->retry = 6;
20896182fdbdSPeter Wemm 			return (retrier(fdc));
2090dc8603e3SJoerg Wunsch 		}
20915b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
20925b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20935b81b6b3SRodney W. Grimes 	case RECALWAIT:
20945b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20956182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
20965b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
20975b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20985b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
20993a2f7427SDavid Greenman 		do {
21003a2f7427SDavid Greenman 			/*
2101dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
21023a2f7427SDavid Greenman 			 */
2103dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2104dc5df763SJoerg Wunsch 				return 0;
2105dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2106dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2107dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
21083a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
21093a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
21105b81b6b3SRodney W. Grimes 		{
2111dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2112dc8603e3SJoerg Wunsch 				/*
2113dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
2114dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2115dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2116dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2117dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2118dc8603e3SJoerg Wunsch 				 * time in a line
2119dc8603e3SJoerg Wunsch 				 */
2120dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2121dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
21223a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
21236182fdbdSPeter Wemm 			return (retrier(fdc));
21245b81b6b3SRodney W. Grimes 		}
21255b81b6b3SRodney W. Grimes 		fd->track = 0;
21265b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
21275b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
21285b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21295b81b6b3SRodney W. Grimes 	case MOTORWAIT:
21305b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
21315b81b6b3SRodney W. Grimes 		{
21325b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
21335b81b6b3SRodney W. Grimes 		}
21345c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
21355c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
21365c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
21375c1a1eaeSBruce Evans 		} else {
21385e235068SJordan K. Hubbard 			/*
21395c1a1eaeSBruce Evans 			 * If all motors were off, then the controller was
21405c1a1eaeSBruce Evans 			 * reset, so it has lost track of the current
21415c1a1eaeSBruce Evans 			 * cylinder.  Recalibrate to handle this case.
2142f86e4077SBruce Evans 			 * But first, discard the results of the reset.
21435e235068SJordan K. Hubbard 			 */
2144f86e4077SBruce Evans 			fdc->state = RESETCOMPLETE;
21455c1a1eaeSBruce Evans 		}
21465b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21475b81b6b3SRodney W. Grimes 	default:
2148b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
2149dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
2150a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
21515b81b6b3SRodney W. Grimes 			       fdc->status[0],
21525b81b6b3SRodney W. Grimes 			       fdc->status[1],
21535b81b6b3SRodney W. Grimes 			       fdc->status[2],
21545b81b6b3SRodney W. Grimes 			       fdc->status[3],
21555b81b6b3SRodney W. Grimes 			       fdc->status[4],
21565b81b6b3SRodney W. Grimes 			       fdc->status[5],
21575b81b6b3SRodney W. Grimes 			       fdc->status[6] );
21583a2f7427SDavid Greenman 		else
2159dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2160dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2161dac0f2dbSJoerg Wunsch 		{
2162dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
21635b81b6b3SRodney W. Grimes 			return (0);
21645b81b6b3SRodney W. Grimes 		}
2165dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2166dac0f2dbSJoerg Wunsch 		return (0);
2167dac0f2dbSJoerg Wunsch 	}
2168dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
21695b81b6b3SRodney W. Grimes 	return (1); /* Come back immediatly to new state */
21705b81b6b3SRodney W. Grimes }
21715b81b6b3SRodney W. Grimes 
2172aaf08d94SGarrett Wollman static int
21736182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
21745b81b6b3SRodney W. Grimes {
21758177437dSPoul-Henning Kamp 	struct bio *bp;
21766182fdbdSPeter Wemm 	struct fd_data *fd;
21776182fdbdSPeter Wemm 	int fdu;
21785b81b6b3SRodney W. Grimes 
2179e93e63cbSBruce Evans 	bp = fdc->bp;
21805b81b6b3SRodney W. Grimes 
21816182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
21828177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
21836182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
21846182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
21853a2f7427SDavid Greenman 		goto fail;
21866182fdbdSPeter Wemm 
21876182fdbdSPeter Wemm 	switch (fdc->retry) {
21885b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
21895b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
21905b81b6b3SRodney W. Grimes 		break;
21915b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
21925b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
21935b81b6b3SRodney W. Grimes 		break;
21945b81b6b3SRodney W. Grimes 	case 6:
21955b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
21965b81b6b3SRodney W. Grimes 		break;
21975b81b6b3SRodney W. Grimes 	case 7:
21985b81b6b3SRodney W. Grimes 		break;
21995b81b6b3SRodney W. Grimes 	default:
22003a2f7427SDavid Greenman 	fail:
22015b81b6b3SRodney W. Grimes 		{
22022995d110SJoerg Wunsch 			int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
22032995d110SJoerg Wunsch 
22042995d110SJoerg Wunsch 			if (printerror)
2205cb7e609aSPoul-Henning Kamp 				diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
22063a2f7427SDavid Greenman 					(struct disklabel *)NULL);
22072995d110SJoerg Wunsch 			if (printerror) {
2208dc5df763SJoerg Wunsch 				if (fdc->flags & FDC_STAT_VALID)
2209dc5df763SJoerg Wunsch 				{
2210dc5df763SJoerg Wunsch 					printf(
2211a838d83dSBruce Evans 			" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2212dc5df763SJoerg Wunsch 					       fdc->status[0], NE7_ST0BITS,
2213dc5df763SJoerg Wunsch 					       fdc->status[1], NE7_ST1BITS,
2214dc5df763SJoerg Wunsch 					       fdc->status[2], NE7_ST2BITS,
2215dc5df763SJoerg Wunsch 					       fdc->status[3], fdc->status[4],
2216dc5df763SJoerg Wunsch 					       fdc->status[5]);
2217dc5df763SJoerg Wunsch 				}
2218dc5df763SJoerg Wunsch 			else
2219dc5df763SJoerg Wunsch 					printf(" (No status)\n");
22205b81b6b3SRodney W. Grimes 			}
22212995d110SJoerg Wunsch 		}
22222995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
22238177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
22248177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
22258177437dSPoul-Henning Kamp 			bp->bio_resid += bp->bio_bcount - fdc->fd->skip;
22262995d110SJoerg Wunsch 		}
2227e93e63cbSBruce Evans 		fdc->bp = NULL;
22285b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
22295f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2230a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
223192ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
22325c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
22335b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
22345b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
223592ed385aSRodney W. Grimes 		return (1);
22365b81b6b3SRodney W. Grimes 	}
22375b81b6b3SRodney W. Grimes 	fdc->retry++;
22385b81b6b3SRodney W. Grimes 	return (1);
22395b81b6b3SRodney W. Grimes }
22405b81b6b3SRodney W. Grimes 
22411fdb6e6cSPoul-Henning Kamp static void
22421fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
22431fdb6e6cSPoul-Henning Kamp {
22441fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
22451fdb6e6cSPoul-Henning Kamp }
22461fdb6e6cSPoul-Henning Kamp 
2247b39c878eSAndrey A. Chernov static int
2248b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
2249b39c878eSAndrey A. Chernov 	dev_t dev;
2250b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
2251b39c878eSAndrey A. Chernov 	struct proc *p;
2252b39c878eSAndrey A. Chernov {
2253b39c878eSAndrey A. Chernov  	fdu_t	fdu;
2254b39c878eSAndrey A. Chernov  	fd_p	fd;
2255b39c878eSAndrey A. Chernov 
22561fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2257b39c878eSAndrey A. Chernov 	int rv = 0, s;
22583a2f7427SDavid Greenman 	size_t fdblk;
2259b39c878eSAndrey A. Chernov 
2260b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
22616182fdbdSPeter Wemm 	fd	= devclass_get_softc(fd_devclass, fdu);
22623a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2263b39c878eSAndrey A. Chernov 
2264b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
22651fdb6e6cSPoul-Henning Kamp 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2266b39c878eSAndrey A. Chernov 	if(bp == 0)
22671fdb6e6cSPoul-Henning Kamp 		return ENOMEM;
226882f5379bSJoerg Wunsch 	/*
226982f5379bSJoerg Wunsch 	 * keep the process from being swapped
227082f5379bSJoerg Wunsch 	 */
22712ada239cSPeter Wemm 	PHOLD(p);
22721fdb6e6cSPoul-Henning Kamp 	bzero((void *)bp, sizeof(*bp));
22731fdb6e6cSPoul-Henning Kamp 	bp->bio_cmd = BIO_FORMAT;
2274b39c878eSAndrey A. Chernov 
2275b39c878eSAndrey A. Chernov 	/*
2276b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2277b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2278b39c878eSAndrey A. Chernov 	 */
22791fdb6e6cSPoul-Henning Kamp 	bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
22803a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2281b39c878eSAndrey A. Chernov 
22821fdb6e6cSPoul-Henning Kamp 	bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
22831fdb6e6cSPoul-Henning Kamp 	bp->bio_data = (caddr_t)finfo;
2284b39c878eSAndrey A. Chernov 
2285b39c878eSAndrey A. Chernov 	/* now do the format */
22861fdb6e6cSPoul-Henning Kamp 	bp->bio_dev = dev;
22871fdb6e6cSPoul-Henning Kamp 	bp->bio_done = fdbiodone;
22881fdb6e6cSPoul-Henning Kamp 	fdstrategy(bp);
2289b39c878eSAndrey A. Chernov 
2290b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2291b39c878eSAndrey A. Chernov 	s = splbio();
22921fdb6e6cSPoul-Henning Kamp 	while(!(bp->bio_flags & BIO_DONE)) {
22935e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2294b39c878eSAndrey A. Chernov 		if (rv == EWOULDBLOCK)
2295b39c878eSAndrey A. Chernov 			break;
2296b39c878eSAndrey A. Chernov 	}
2297b39c878eSAndrey A. Chernov 	splx(s);
2298b39c878eSAndrey A. Chernov 
229982f5379bSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2300b39c878eSAndrey A. Chernov 		/* timed out */
2301b39c878eSAndrey A. Chernov 		rv = EIO;
23025f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
230382f5379bSJoerg Wunsch 	}
23041fdb6e6cSPoul-Henning Kamp 	if (bp->bio_flags & BIO_ERROR)
23051fdb6e6cSPoul-Henning Kamp 		rv = bp->bio_error;
230682f5379bSJoerg Wunsch 	/*
230782f5379bSJoerg Wunsch 	 * allow the process to be swapped
230882f5379bSJoerg Wunsch 	 */
23092ada239cSPeter Wemm 	PRELE(p);
2310b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2311b39c878eSAndrey A. Chernov 	return rv;
2312b39c878eSAndrey A. Chernov }
2313b39c878eSAndrey A. Chernov 
2314f5f7ba03SJordan K. Hubbard /*
2315671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2316f5f7ba03SJordan K. Hubbard  */
23175b81b6b3SRodney W. Grimes 
23183e425b96SJulian Elischer static int
2319b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
2320f5f7ba03SJordan K. Hubbard 	dev_t dev;
2321ecbb00a2SDoug Rabson 	u_long cmd;
2322f5f7ba03SJordan K. Hubbard 	caddr_t addr;
2323f5f7ba03SJordan K. Hubbard 	int flag;
2324b39c878eSAndrey A. Chernov 	struct proc *p;
2325f5f7ba03SJordan K. Hubbard {
23263a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
23276182fdbdSPeter Wemm  	fd_p	fd = devclass_get_softc(fd_devclass, fdu);
23283a2f7427SDavid Greenman 	size_t fdblk;
23293a2f7427SDavid Greenman 
2330f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2331f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
23322995d110SJoerg Wunsch 	struct fdc_status *fsp;
2333f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
23343a2f7427SDavid Greenman 	int error = 0;
2335f5f7ba03SJordan K. Hubbard 
23363a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2337f5f7ba03SJordan K. Hubbard 
23386182fdbdSPeter Wemm 	switch (cmd) {
2339f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2340f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2341f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
23423a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
23436182fdbdSPeter Wemm 		fdt = fd->ft;
2344f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2345f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2346f5f7ba03SJordan K. Hubbard 
234749ff4debSPoul-Henning Kamp 		if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2348191e1a59SBruce Evans 		    == NULL)
2349f5f7ba03SJordan K. Hubbard 			error = 0;
2350f5f7ba03SJordan K. Hubbard 		else
2351f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2352f5f7ba03SJordan K. Hubbard 
2353f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2354f5f7ba03SJordan K. Hubbard 		break;
2355f5f7ba03SJordan K. Hubbard 
2356f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2357f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2358f5f7ba03SJordan K. Hubbard 			error = EBADF;
2359f5f7ba03SJordan K. Hubbard 		break;
2360f5f7ba03SJordan K. Hubbard 
2361f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2362f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2363f5f7ba03SJordan K. Hubbard 			error = EBADF;
2364f5f7ba03SJordan K. Hubbard 		break;
2365f5f7ba03SJordan K. Hubbard 
2366f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
23676182fdbdSPeter Wemm 		if ((flag & FWRITE) == 0) {
2368f5f7ba03SJordan K. Hubbard 			error = EBADF;
2369f5f7ba03SJordan K. Hubbard 			break;
2370f5f7ba03SJordan K. Hubbard 		}
2371f5f7ba03SJordan K. Hubbard 
2372f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2373f5f7ba03SJordan K. Hubbard 
2374191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2375191e1a59SBruce Evans 					  (u_long)0)) != 0)
2376f5f7ba03SJordan K. Hubbard 			break;
2377f5f7ba03SJordan K. Hubbard 
237849ff4debSPoul-Henning Kamp 		error = writedisklabel(dev, (struct disklabel *)buffer);
2379b39c878eSAndrey A. Chernov 		break;
2380b39c878eSAndrey A. Chernov 	case FD_FORM:
2381b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2382b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2383b39c878eSAndrey A. Chernov 		else if (((struct fd_formb *)addr)->format_version !=
2384b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2385b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2386b39c878eSAndrey A. Chernov 		else
2387b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
2388b39c878eSAndrey A. Chernov 		break;
2389b39c878eSAndrey A. Chernov 
2390b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
23913e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2392f5f7ba03SJordan K. Hubbard 		break;
2393f5f7ba03SJordan K. Hubbard 
23943a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
23953a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2396f711d546SPoul-Henning Kamp 		if (suser(p) != 0)
23973a2f7427SDavid Greenman 			return EPERM;
23983e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
23993a2f7427SDavid Greenman 		break;
24003a2f7427SDavid Greenman 
24013a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
24023e425b96SJulian Elischer 		*(int *)addr = fd->options;
24033a2f7427SDavid Greenman 		break;
24043a2f7427SDavid Greenman 
24053a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
24063e425b96SJulian Elischer 		fd->options = *(int *)addr;
24073a2f7427SDavid Greenman 		break;
24083a2f7427SDavid Greenman 
24092995d110SJoerg Wunsch 	case FD_CLRERR:
24102995d110SJoerg Wunsch 		if (suser(p) != 0)
24112995d110SJoerg Wunsch 			return EPERM;
24122995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
24132995d110SJoerg Wunsch 		break;
24142995d110SJoerg Wunsch 
24152995d110SJoerg Wunsch 	case FD_GSTAT:
24162995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
24172995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
24182995d110SJoerg Wunsch 			return EINVAL;
24192995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
24202995d110SJoerg Wunsch 		break;
24212995d110SJoerg Wunsch 
2422f5f7ba03SJordan K. Hubbard 	default:
24233a2f7427SDavid Greenman 		error = ENOTTY;
2424f5f7ba03SJordan K. Hubbard 		break;
2425f5f7ba03SJordan K. Hubbard 	}
2426f5f7ba03SJordan K. Hubbard 	return (error);
2427f5f7ba03SJordan K. Hubbard }
2428f5f7ba03SJordan K. Hubbard 
24293a2f7427SDavid Greenman /*
24303a2f7427SDavid Greenman  * Hello emacs, these are the
24313a2f7427SDavid Greenman  * Local Variables:
24323a2f7427SDavid Greenman  *  c-indent-level:               8
24333a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
24343a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
24353a2f7427SDavid Greenman  *  c-brace-offset:              -8
24363a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
24373a2f7427SDavid Greenman  *  c-argdecl-indent:             8
24383a2f7427SDavid Greenman  *  c-label-offset:              -8
24393a2f7427SDavid Greenman  *  c++-hanging-braces:           1
24403a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
24413a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
24423a2f7427SDavid Greenman  *  c++-friend-offset:            0
24433a2f7427SDavid Greenman  * End:
24443a2f7427SDavid Greenman  */
2445