xref: /freebsd/sys/dev/fdc/fdc.c (revision 5d54fe91722a4108bc9b5a7b632c728a76580db1)
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.
3264860614SJoerg Wunsch  * 3. All advertising materials mentioning features or use of this software
3364860614SJoerg Wunsch  *    must display the following acknowledgement:
3464860614SJoerg Wunsch  *	This product includes software developed by the University of
3564860614SJoerg Wunsch  *	California, Berkeley and its contributors.
3664860614SJoerg Wunsch  * 4. Neither the name of the University nor the names of its contributors
3764860614SJoerg Wunsch  *    may be used to endorse or promote products derived from this software
3864860614SJoerg Wunsch  *    without specific prior written permission.
395b81b6b3SRodney W. Grimes  *
405b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
415b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
425b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
435b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
445b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
455b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
465b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
475b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
485b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
495b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
505b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
515b81b6b3SRodney W. Grimes  *
52dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
53c3aac50fSPeter Wemm  * $FreeBSD$
545b81b6b3SRodney W. Grimes  */
555b81b6b3SRodney W. Grimes 
56d2fb4892SJoerg Wunsch #include "opt_fdc.h"
575f830ea2SJoerg Wunsch #include "card.h"
585b81b6b3SRodney W. Grimes 
59b99f0a4aSAndrew Moore #include <sys/param.h>
60b99f0a4aSAndrew Moore #include <sys/systm.h>
619626b608SPoul-Henning Kamp #include <sys/bio.h>
626182fdbdSPeter Wemm #include <sys/bus.h>
636182fdbdSPeter Wemm #include <sys/conf.h>
64b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
65fb919e4dSMark Murray #include <sys/disklabel.h>
666182fdbdSPeter Wemm #include <sys/fcntl.h>
67e774b251SJoerg Wunsch #include <sys/fdcio.h>
68fb919e4dSMark Murray #include <sys/kernel.h>
69fb919e4dSMark Murray #include <sys/lock.h>
70b99f0a4aSAndrew Moore #include <sys/malloc.h>
716182fdbdSPeter Wemm #include <sys/module.h>
72fb919e4dSMark Murray #include <sys/mutex.h>
733a2f7427SDavid Greenman #include <sys/proc.h>
74b99f0a4aSAndrew Moore #include <sys/syslog.h>
756182fdbdSPeter Wemm 
766182fdbdSPeter Wemm #include <machine/bus.h>
776182fdbdSPeter Wemm #include <sys/rman.h>
786182fdbdSPeter Wemm 
796182fdbdSPeter Wemm #include <machine/clock.h>
806182fdbdSPeter Wemm #include <machine/resource.h>
81dc5df763SJoerg Wunsch #include <machine/stdarg.h>
826182fdbdSPeter Wemm 
836182fdbdSPeter Wemm #include <isa/isavar.h>
84a97c75b7SDoug Rabson #include <isa/isareg.h>
85a97c75b7SDoug Rabson #include <isa/fdreg.h>
86a97c75b7SDoug Rabson #include <isa/fdc.h>
87a97c75b7SDoug Rabson #include <isa/rtc.h>
886182fdbdSPeter Wemm 
890722d6abSJoerg Wunsch /* configuration flags */
900722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
91e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
920722d6abSJoerg Wunsch 
930722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
940722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
950722d6abSJoerg Wunsch 
96dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
97dc5df763SJoerg Wunsch #define FD_FAILED -1
98dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
99dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
1003fef646eSJoerg Wunsch /*
1013fef646eSJoerg Wunsch  * Stop retrying after this many DMA overruns.  Since each retry takes
10283edbfa5SJoerg Wunsch  * one revolution, with 300 rpm., 25 retries take approximately 5
1033fef646eSJoerg Wunsch  * seconds which the read attempt will block in case the DMA overrun
1043fef646eSJoerg Wunsch  * is persistent.
1053fef646eSJoerg Wunsch  */
1063fef646eSJoerg Wunsch #define FDC_DMAOV_MAX	25
107dc5df763SJoerg Wunsch 
1086fb89845SKATO Takenori #define NUMTYPES 17
1096fb89845SKATO Takenori #define NUMDENS  (NUMTYPES - 7)
1107ca0641bSAndrey A. Chernov 
111b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
112b99f0a4aSAndrew Moore #define FD_1720         1
113b99f0a4aSAndrew Moore #define FD_1480         2
114b99f0a4aSAndrew Moore #define FD_1440         3
115b99f0a4aSAndrew Moore #define FD_1200         4
116b99f0a4aSAndrew Moore #define FD_820          5
117b99f0a4aSAndrew Moore #define FD_800          6
118b99f0a4aSAndrew Moore #define FD_720          7
119b99f0a4aSAndrew Moore #define FD_360          8
1206fb89845SKATO Takenori #define FD_640          9
1216fb89845SKATO Takenori #define FD_1232         10
122ed2fa05eSAndrey A. Chernov 
1236fb89845SKATO Takenori #define FD_1480in5_25   11
1246fb89845SKATO Takenori #define FD_1440in5_25   12
1256fb89845SKATO Takenori #define FD_820in5_25    13
1266fb89845SKATO Takenori #define FD_800in5_25    14
1276fb89845SKATO Takenori #define FD_720in5_25    15
1286fb89845SKATO Takenori #define FD_360in5_25    16
1296fb89845SKATO Takenori #define FD_640in5_25    17
130b99f0a4aSAndrew Moore 
131f664aeeeSJoerg Wunsch #define BIO_RDSECTID	BIO_CMD1
1327ca0641bSAndrey A. Chernov 
1336f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1345b81b6b3SRodney W. Grimes {
135126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
136126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
137126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
138126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
139126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
140126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
141126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
142b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
1436fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /*  640K in DD 5.25in */
1446fb89845SKATO Takenori {  8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
145ed2fa05eSAndrey A. Chernov 
146126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
147126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
148126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
149126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
150126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
151126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1526fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /*  640K in HD 5.25in */
1535b81b6b3SRodney W. Grimes };
1545b81b6b3SRodney W. Grimes 
155b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
156dc16046fSJoerg Wunsch 
157f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
15864860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
15964860614SJoerg Wunsch 				 * up to cyl 82 */
160250300ebSJoerg Wunsch #define MAX_HEAD	1
161f8ce7dd5SJoerg Wunsch 
1625b81b6b3SRodney W. Grimes /***********************************************************************\
1635b81b6b3SRodney W. Grimes * Per controller structure.						*
1645b81b6b3SRodney W. Grimes \***********************************************************************/
1656182fdbdSPeter Wemm static devclass_t fdc_devclass;
1665b81b6b3SRodney W. Grimes 
1675b81b6b3SRodney W. Grimes /***********************************************************************\
1685b81b6b3SRodney W. Grimes * Per drive structure.							*
169b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1705b81b6b3SRodney W. Grimes \***********************************************************************/
1716182fdbdSPeter Wemm struct fd_data {
172b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1735b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1743a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1755b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1765b81b6b3SRodney W. Grimes 	int	flags;
1775b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1785b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1795b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1805b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1815b81b6b3SRodney W. Grimes 	int	skip;
1825b81b6b3SRodney W. Grimes 	int	hddrv;
183dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1845b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1853a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
18602a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
18702a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
188b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
189e219897aSJoerg Wunsch 	eventhandler_tag clonetag;
190e219897aSJoerg Wunsch 	dev_t	masterdev;
19160444853SJoerg Wunsch #define NCLONEDEVS	10	/* must match the table below */
19260444853SJoerg Wunsch 	dev_t	clonedevs[NCLONEDEVS];
1936182fdbdSPeter Wemm 	device_t dev;
1946182fdbdSPeter Wemm 	fdu_t	fdu;
1956182fdbdSPeter Wemm };
19637286586SPeter Wemm 
19737286586SPeter Wemm struct fdc_ivars {
19837286586SPeter Wemm 	int	fdunit;
19937286586SPeter Wemm };
2006182fdbdSPeter Wemm static devclass_t fd_devclass;
2015b81b6b3SRodney W. Grimes 
2025b81b6b3SRodney W. Grimes /***********************************************************************\
2035b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
2045b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
2055b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
2065b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
2075b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
2085b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
2095b81b6b3SRodney W. Grimes \***********************************************************************/
210b99f0a4aSAndrew Moore 
2113a2f7427SDavid Greenman /* internal functions */
21264860614SJoerg Wunsch static driver_intr_t fdc_intr;
2136182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
2143a2f7427SDavid Greenman #  define TURNON 1
2153a2f7427SDavid Greenman #  define TURNOFF 0
2163a2f7427SDavid Greenman static timeout_t fd_turnoff;
2173a2f7427SDavid Greenman static timeout_t fd_motor_on;
2186182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
2193a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2206182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
22180909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
2226182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
2235c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2243a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2256182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
2266182fdbdSPeter Wemm static int retrier(struct fdc_data *);
227f664aeeeSJoerg Wunsch static int fdmisccmd(dev_t, u_int, void *);
2283a2f7427SDavid Greenman 
229d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
23080909a7dSJoerg Wunsch static void fd_clone (void *arg, char *name, int namelen, dev_t *dev);
231d66c374fSTor Egge 
232d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
233d66c374fSTor Egge 
234d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
235cba2a7c6SBruce Evans static char const * const fdstates[] =
2365b81b6b3SRodney W. Grimes {
2375b81b6b3SRodney W. Grimes "DEVIDLE",
2385b81b6b3SRodney W. Grimes "FINDWORK",
2395b81b6b3SRodney W. Grimes "DOSEEK",
2405b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2415b81b6b3SRodney W. Grimes "IOCOMPLETE",
2425b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2435b81b6b3SRodney W. Grimes "STARTRECAL",
2445b81b6b3SRodney W. Grimes "RESETCTLR",
2455b81b6b3SRodney W. Grimes "SEEKWAIT",
2465b81b6b3SRodney W. Grimes "RECALWAIT",
2475b81b6b3SRodney W. Grimes "MOTORWAIT",
2485c1a1eaeSBruce Evans "IOTIMEDOUT",
2495c1a1eaeSBruce Evans "RESETCOMPLETE",
250ff9607b0SBruce Evans "PIOREAD",
2515b81b6b3SRodney W. Grimes };
2525b81b6b3SRodney W. Grimes 
2533a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
254cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2555b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2565b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
257d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2585b81b6b3SRodney W. Grimes #define TRACE0(arg)
2595b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
260d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2615b81b6b3SRodney W. Grimes 
262427ccf00SDoug Rabson static void
263427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
264427ccf00SDoug Rabson {
265427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
266427ccf00SDoug Rabson }
267427ccf00SDoug Rabson 
268427ccf00SDoug Rabson static u_int8_t
269427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
270427ccf00SDoug Rabson {
271427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
272427ccf00SDoug Rabson }
273427ccf00SDoug Rabson 
274427ccf00SDoug Rabson static void
275427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
276427ccf00SDoug Rabson {
277427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
278427ccf00SDoug Rabson }
279427ccf00SDoug Rabson 
280427ccf00SDoug Rabson static u_int8_t
281427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
282427ccf00SDoug Rabson {
283427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
284427ccf00SDoug Rabson }
285427ccf00SDoug Rabson 
286427ccf00SDoug Rabson static void
2875f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
288427ccf00SDoug Rabson {
289427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
290427ccf00SDoug Rabson }
291427ccf00SDoug Rabson 
29258c9d623SPeter Wemm #if NCARD > 0
2935f830ea2SJoerg Wunsch static void
2945f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
2955f830ea2SJoerg Wunsch {
2965f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
2975f830ea2SJoerg Wunsch }
29858c9d623SPeter Wemm #endif
2995f830ea2SJoerg Wunsch 
300427ccf00SDoug Rabson #if 0
301427ccf00SDoug Rabson 
302427ccf00SDoug Rabson static u_int8_t
303427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
304427ccf00SDoug Rabson {
305427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
306427ccf00SDoug Rabson }
307427ccf00SDoug Rabson 
308427ccf00SDoug Rabson #endif
309427ccf00SDoug Rabson 
310f8ce7dd5SJoerg Wunsch /*
31164860614SJoerg Wunsch  * The open function is named Fdopen() to avoid confusion with fdopen()
31264860614SJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
313f8ce7dd5SJoerg Wunsch  */
314f8ce7dd5SJoerg Wunsch static	d_open_t	Fdopen;
31587f6c662SJulian Elischer static	d_close_t	fdclose;
31687f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
31787f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
31887f6c662SJulian Elischer 
31987f6c662SJulian Elischer #define CDEV_MAJOR 9
3204e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
3214e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
3224e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
3234e2f199eSPoul-Henning Kamp 	/* read */	physread,
3244e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
3254e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
3264e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
3274e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
3284e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
3294e2f199eSPoul-Henning Kamp 	/* name */	"fd",
3304e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
3314e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
3324e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
3334e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
3344e2f199eSPoul-Henning Kamp };
3354e2f199eSPoul-Henning Kamp 
336dc5df763SJoerg Wunsch static int
3376182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
338dc5df763SJoerg Wunsch {
3396182fdbdSPeter Wemm 	fdc->fdc_errs++;
34016b04b6aSJoerg Wunsch 	if (s) {
341b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
342b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
343b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
344b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
345b6e5f28eSPeter Wemm 						    "logging any more\n");
34616b04b6aSJoerg Wunsch 	}
347dc5df763SJoerg Wunsch 
348dc5df763SJoerg Wunsch 	return FD_FAILED;
349dc5df763SJoerg Wunsch }
350dc5df763SJoerg Wunsch 
351dc5df763SJoerg Wunsch /*
352dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
353dc5df763SJoerg Wunsch  * Unit number,
354dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
355dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
356dc5df763SJoerg Wunsch  */
3576f4e0bebSPoul-Henning Kamp static int
3586182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
359dc5df763SJoerg Wunsch {
360dc5df763SJoerg Wunsch 	u_char cmd;
361dc5df763SJoerg Wunsch 	int n_in;
362dc5df763SJoerg Wunsch 	int n;
363dc5df763SJoerg Wunsch 	va_list ap;
364dc5df763SJoerg Wunsch 
365dc5df763SJoerg Wunsch 	va_start(ap, n_out);
366dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
367dc5df763SJoerg Wunsch 	va_end(ap);
368dc5df763SJoerg Wunsch 	va_start(ap, n_out);
369dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
370dc5df763SJoerg Wunsch 	{
3716182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
372dc5df763SJoerg Wunsch 		{
373dc5df763SJoerg Wunsch 			char msg[50];
3742127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
375dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
376dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
3776182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
378dc5df763SJoerg Wunsch 		}
379dc5df763SJoerg Wunsch 	}
380dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
381dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
382dc5df763SJoerg Wunsch 	{
383dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
3846182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
385dc5df763SJoerg Wunsch 		{
386dc5df763SJoerg Wunsch 			char msg[50];
3872127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
388dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
389dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
3906182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
391dc5df763SJoerg Wunsch 		}
392dc5df763SJoerg Wunsch 	}
393dc5df763SJoerg Wunsch 
394dc5df763SJoerg Wunsch 	return 0;
395dc5df763SJoerg Wunsch }
396dc5df763SJoerg Wunsch 
3976f4e0bebSPoul-Henning Kamp static int
398d66c374fSTor Egge enable_fifo(fdc_p fdc)
399d66c374fSTor Egge {
400d66c374fSTor Egge 	int i, j;
401d66c374fSTor Egge 
402d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
403d66c374fSTor Egge 
404d66c374fSTor Egge 		/*
405d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
406d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
407d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
408d66c374fSTor Egge 		 */
409d66c374fSTor Egge 
4106182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
4116182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
412d66c374fSTor Egge 
413d66c374fSTor Egge 		/* If command is invalid, return */
414d66c374fSTor Egge 		j = 100000;
415427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
416d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
417d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
418d66c374fSTor Egge 				fdc_reset(fdc);
419d66c374fSTor Egge 				return FD_FAILED;
420d66c374fSTor Egge 			}
421d66c374fSTor Egge 		if (j<0 ||
4226182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
423d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
424d66c374fSTor Egge 			fdc_reset(fdc);
4256182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
426d66c374fSTor Egge 		}
427d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
428d66c374fSTor Egge 		return 0;
429d66c374fSTor Egge 	}
4306182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
431d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
4326182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
433d66c374fSTor Egge 	return 0;
434d66c374fSTor Egge }
435d66c374fSTor Egge 
436d66c374fSTor Egge static int
437dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
438dc5df763SJoerg Wunsch {
439dc5df763SJoerg Wunsch 	int st3;
440dc5df763SJoerg Wunsch 
4416182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
442dc5df763SJoerg Wunsch 	{
4436182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
444dc5df763SJoerg Wunsch 	}
445dc5df763SJoerg Wunsch 	if (st3p)
446dc5df763SJoerg Wunsch 		*st3p = st3;
447dc5df763SJoerg Wunsch 
448dc5df763SJoerg Wunsch 	return 0;
449dc5df763SJoerg Wunsch }
450dc5df763SJoerg Wunsch 
4516f4e0bebSPoul-Henning Kamp static int
452dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
453dc5df763SJoerg Wunsch {
4546182fdbdSPeter Wemm 	int cyl, st0, ret;
455dc5df763SJoerg Wunsch 
4566182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
4576182fdbdSPeter Wemm 	if (ret) {
4586182fdbdSPeter Wemm 		(void)fdc_err(fdc,
459dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
460dc5df763SJoerg Wunsch 		return ret;
461dc5df763SJoerg Wunsch 	}
462dc5df763SJoerg Wunsch 
463dc5df763SJoerg Wunsch 	if (st0p)
464dc5df763SJoerg Wunsch 		*st0p = st0;
465dc5df763SJoerg Wunsch 
4666182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
467dc5df763SJoerg Wunsch 		/*
468dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
469dc5df763SJoerg Wunsch 		 */
470dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
471dc5df763SJoerg Wunsch 	}
472dc5df763SJoerg Wunsch 
4736182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
4746182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
475dc5df763SJoerg Wunsch 	}
476dc5df763SJoerg Wunsch 
477dc5df763SJoerg Wunsch 	if (cylp)
478dc5df763SJoerg Wunsch 		*cylp = cyl;
479dc5df763SJoerg Wunsch 
480dc5df763SJoerg Wunsch 	return 0;
481dc5df763SJoerg Wunsch }
482dc5df763SJoerg Wunsch 
483dc5df763SJoerg Wunsch 
4846f4e0bebSPoul-Henning Kamp static int
48564860614SJoerg Wunsch fd_read_status(fdc_p fdc)
486dc5df763SJoerg Wunsch {
487dc5df763SJoerg Wunsch 	int i, ret;
488b5e8ce9fSBruce Evans 
4896182fdbdSPeter Wemm 	for (i = 0; i < 7; i++) {
490b5e8ce9fSBruce Evans 		/*
49164860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
492a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
493b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
494b5e8ce9fSBruce Evans 		 */
495b5e8ce9fSBruce Evans 		int status;
496b5e8ce9fSBruce Evans 
4976182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
498b5e8ce9fSBruce Evans 		fdc->status[i] = status;
499b5e8ce9fSBruce Evans 		if (ret != 0)
500dc5df763SJoerg Wunsch 			break;
501dc5df763SJoerg Wunsch 	}
502dc5df763SJoerg Wunsch 
503dc5df763SJoerg Wunsch 	if (ret == 0)
504dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
505dc5df763SJoerg Wunsch 	else
506dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
507dc5df763SJoerg Wunsch 
508dc5df763SJoerg Wunsch 	return ret;
509dc5df763SJoerg Wunsch }
510dc5df763SJoerg Wunsch 
5115b81b6b3SRodney W. Grimes /****************************************************************************/
5125b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5135b81b6b3SRodney W. Grimes /****************************************************************************/
514dc5df763SJoerg Wunsch 
5153a2f7427SDavid Greenman static int
51637286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
5175b81b6b3SRodney W. Grimes {
51837286586SPeter Wemm 	device_t dev;
519e219897aSJoerg Wunsch 	int ispnp, ispcmcia, nports;
5205b81b6b3SRodney W. Grimes 
52137286586SPeter Wemm 	dev = fdc->fdc_dev;
5225f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
5235f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
5246182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
5256182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
5266182fdbdSPeter Wemm 
527b6e5f28eSPeter Wemm 	/*
5285f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
5295f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
5305f830ea2SJoerg Wunsch 	 * register at 0x3f6.
5315f830ea2SJoerg Wunsch 	 *
532b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
5335f830ea2SJoerg Wunsch 	 *
5345f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
5355f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
5365f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
537b6e5f28eSPeter Wemm 	 */
538e219897aSJoerg Wunsch 	nports = ispcmcia ? 8 : (ispnp ? 1 : 6);
5396182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
5406182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
541e219897aSJoerg Wunsch 					     nports, RF_ACTIVE);
5426182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
543e219897aSJoerg Wunsch 		device_printf(dev, "cannot reserve I/O port range (%d ports)\n",
544e219897aSJoerg Wunsch 			      nports);
54537286586SPeter Wemm 		return ENXIO;
5466182fdbdSPeter Wemm 	}
547427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
548427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
549427ccf00SDoug Rabson 
5505f830ea2SJoerg Wunsch 	if (!ispcmcia) {
551427ccf00SDoug Rabson 		/*
5525f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
5535f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
5545f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
5555f830ea2SJoerg Wunsch 		 * accordingly.
556b6e5f28eSPeter Wemm 		 */
557b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
558b6e5f28eSPeter Wemm 			fdc->port_off = -2;
559b6e5f28eSPeter Wemm 
560b6e5f28eSPeter Wemm 		/*
5615f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
5625f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
5635f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
564a2639a18SPeter Wemm 		 *
5655f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
5665f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
5675f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
5685f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
5695f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
5705f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
5715f830ea2SJoerg Wunsch 		 * address for the control register.
572427ccf00SDoug Rabson 		 */
573b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
574b9da888fSPeter Wemm 			u_long ctlstart;
575b9da888fSPeter Wemm 
576b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
5775f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
5785f830ea2SJoerg Wunsch 				fdc->port_off + 7;
579b6e5f28eSPeter Wemm 
580b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
581b9da888fSPeter Wemm 		}
582b6e5f28eSPeter Wemm 
583b6e5f28eSPeter Wemm 		/*
584b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
585b6e5f28eSPeter Wemm 		 */
586427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
5875f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
5885f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
589b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
590427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
5915f830ea2SJoerg Wunsch 			device_printf(dev,
592e219897aSJoerg Wunsch 		"cannot reserve control I/O port range (control port)\n");
59337286586SPeter Wemm 			return ENXIO;
594427ccf00SDoug Rabson 		}
595427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
596427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
5975f830ea2SJoerg Wunsch 	}
5986182fdbdSPeter Wemm 
5996182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
6006182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
6016182fdbdSPeter Wemm 					  RF_ACTIVE);
6026182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
603427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
60437286586SPeter Wemm 		return ENXIO;
6056182fdbdSPeter Wemm 	}
6065f830ea2SJoerg Wunsch 
6075f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
6086182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
6096182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
6106182fdbdSPeter Wemm 						  RF_ACTIVE);
6116182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
612427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
61337286586SPeter Wemm 			return ENXIO;
6146182fdbdSPeter Wemm 		}
6156182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
6165f830ea2SJoerg Wunsch 	}
61737286586SPeter Wemm 
61837286586SPeter Wemm 	return 0;
61937286586SPeter Wemm }
62037286586SPeter Wemm 
62137286586SPeter Wemm static void
62237286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
62337286586SPeter Wemm {
62437286586SPeter Wemm 	device_t dev;
62537286586SPeter Wemm 
62637286586SPeter Wemm 	dev = fdc->fdc_dev;
62737286586SPeter Wemm 	if (fdc->res_irq != 0) {
62837286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
62937286586SPeter Wemm 					fdc->res_irq);
63037286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
63137286586SPeter Wemm 				     fdc->res_irq);
63237286586SPeter Wemm 	}
63337286586SPeter Wemm 	if (fdc->res_ctl != 0) {
63437286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
63537286586SPeter Wemm 					fdc->res_ctl);
63637286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
63737286586SPeter Wemm 				     fdc->res_ctl);
63837286586SPeter Wemm 	}
63937286586SPeter Wemm 	if (fdc->res_ioport != 0) {
64037286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
64137286586SPeter Wemm 					fdc->res_ioport);
64237286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
64337286586SPeter Wemm 				     fdc->res_ioport);
64437286586SPeter Wemm 	}
64537286586SPeter Wemm 	if (fdc->res_drq != 0) {
64637286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
64737286586SPeter Wemm 					fdc->res_drq);
64837286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
64937286586SPeter Wemm 				     fdc->res_drq);
65037286586SPeter Wemm 	}
65137286586SPeter Wemm }
65237286586SPeter Wemm 
65337286586SPeter Wemm /****************************************************************************/
65437286586SPeter Wemm /*                      autoconfiguration stuff                             */
65537286586SPeter Wemm /****************************************************************************/
65637286586SPeter Wemm 
65737286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
65837286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
65937286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
66037286586SPeter Wemm 	{0}
66137286586SPeter Wemm };
66237286586SPeter Wemm 
66337286586SPeter Wemm static int
6643aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
66537286586SPeter Wemm {
66637286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
66737286586SPeter Wemm 
66837286586SPeter Wemm 	switch (which) {
66937286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
67037286586SPeter Wemm 		*result = ivars->fdunit;
67137286586SPeter Wemm 		break;
67237286586SPeter Wemm 	default:
67337286586SPeter Wemm 		return ENOENT;
67437286586SPeter Wemm 	}
67537286586SPeter Wemm 	return 0;
67637286586SPeter Wemm }
67737286586SPeter Wemm 
67837286586SPeter Wemm /*
67937286586SPeter Wemm  * fdc controller section.
68037286586SPeter Wemm  */
68137286586SPeter Wemm static int
68237286586SPeter Wemm fdc_probe(device_t dev)
68337286586SPeter Wemm {
68437286586SPeter Wemm 	int	error, ic_type;
68537286586SPeter Wemm 	struct	fdc_data *fdc;
68637286586SPeter Wemm 
68737286586SPeter Wemm 	fdc = device_get_softc(dev);
68837286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
68937286586SPeter Wemm 	fdc->fdc_dev = dev;
6905f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
69137286586SPeter Wemm 
69237286586SPeter Wemm 	/* Check pnp ids */
69337286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
69437286586SPeter Wemm 	if (error == ENXIO)
69537286586SPeter Wemm 		return ENXIO;
6965f830ea2SJoerg Wunsch 	if (error == 0)
6975f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
69837286586SPeter Wemm 
69937286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
70037286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
70137286586SPeter Wemm 	if (error)
70237286586SPeter Wemm 		goto out;
7035b81b6b3SRodney W. Grimes 
70416111cedSAndrew Moore 	/* First - lets reset the floppy controller */
705427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
70616111cedSAndrew Moore 	DELAY(100);
707427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
70816111cedSAndrew Moore 
7095b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
7106182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7116182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
7126182fdbdSPeter Wemm 		error = ENXIO;
7136182fdbdSPeter Wemm 		goto out;
7145b81b6b3SRodney W. Grimes 	}
7156182fdbdSPeter Wemm 
7166182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
7176182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
7186182fdbdSPeter Wemm 		switch (ic_type) {
7196182fdbdSPeter Wemm 		case 0x80:
7206182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
7216182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
7226182fdbdSPeter Wemm 			break;
7236182fdbdSPeter Wemm 		case 0x81:
7246182fdbdSPeter Wemm 			device_set_desc(dev, "Intel 82077 or clone");
7256182fdbdSPeter Wemm 			fdc->fdct = FDC_I82077;
7266182fdbdSPeter Wemm 			break;
7276182fdbdSPeter Wemm 		case 0x90:
7286182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 72065B or clone");
7296182fdbdSPeter Wemm 			fdc->fdct = FDC_NE72065;
7306182fdbdSPeter Wemm 			break;
7316182fdbdSPeter Wemm 		default:
7326182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
7336182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
7346182fdbdSPeter Wemm 			break;
7356182fdbdSPeter Wemm 		}
7366182fdbdSPeter Wemm 	}
7376182fdbdSPeter Wemm 
7386182fdbdSPeter Wemm out:
73937286586SPeter Wemm 	fdc_release_resources(fdc);
7406182fdbdSPeter Wemm 	return (error);
7415b81b6b3SRodney W. Grimes }
7425b81b6b3SRodney W. Grimes 
7435f830ea2SJoerg Wunsch #if NCARD > 0
7445f830ea2SJoerg Wunsch 
7455f830ea2SJoerg Wunsch static int
7465f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
7475f830ea2SJoerg Wunsch {
7485f830ea2SJoerg Wunsch 	int	error;
7495f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7505f830ea2SJoerg Wunsch 
7515f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7525f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
7535f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
7545f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
7555f830ea2SJoerg Wunsch 
7565f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
7575f830ea2SJoerg Wunsch 
7585f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
7595f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
7605f830ea2SJoerg Wunsch 	if (error)
7615f830ea2SJoerg Wunsch 		goto out;
7625f830ea2SJoerg Wunsch 
7635f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
7645f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
7655f830ea2SJoerg Wunsch 	DELAY(100);
7665f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
7675f830ea2SJoerg Wunsch 
7685f830ea2SJoerg Wunsch 	/* see if it can handle a command */
7695f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7705f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
7715f830ea2SJoerg Wunsch 		error = ENXIO;
7725f830ea2SJoerg Wunsch 		goto out;
7735f830ea2SJoerg Wunsch 	}
7745f830ea2SJoerg Wunsch 
7755f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
7765f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
7775f830ea2SJoerg Wunsch 
7785f830ea2SJoerg Wunsch out:
7795f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7805f830ea2SJoerg Wunsch 	return (error);
7815f830ea2SJoerg Wunsch }
7825f830ea2SJoerg Wunsch 
783e219897aSJoerg Wunsch #endif /* NCARD > 0 */
784e219897aSJoerg Wunsch 
7855f830ea2SJoerg Wunsch static int
786e219897aSJoerg Wunsch fdc_detach(device_t dev)
7875f830ea2SJoerg Wunsch {
7885f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7895f830ea2SJoerg Wunsch 	int	error;
7905f830ea2SJoerg Wunsch 
7915f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7925f830ea2SJoerg Wunsch 
7935f830ea2SJoerg Wunsch 	/* have our children detached first */
7945f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
7955f830ea2SJoerg Wunsch 		return (error);
7965f830ea2SJoerg Wunsch 
797e219897aSJoerg Wunsch 	/* reset controller, turn motor off */
798e219897aSJoerg Wunsch 	fdout_wr(fdc, 0);
799e219897aSJoerg Wunsch 
800e219897aSJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0)
801e219897aSJoerg Wunsch 		isa_dma_release(fdc->dmachan);
802e219897aSJoerg Wunsch 
8035f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
8045f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
8055f830ea2SJoerg Wunsch 		return (0);
8065f830ea2SJoerg Wunsch 	}
8075f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
8085f830ea2SJoerg Wunsch 
8095f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
8105f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
8115f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
8125f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
8135f830ea2SJoerg Wunsch 	return (0);
8145f830ea2SJoerg Wunsch }
8155f830ea2SJoerg Wunsch 
8165b81b6b3SRodney W. Grimes /*
81737286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
8185b81b6b3SRodney W. Grimes  */
8196182fdbdSPeter Wemm static void
82037286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
8215b81b6b3SRodney W. Grimes {
82237286586SPeter Wemm 	int	disabled;
82337286586SPeter Wemm 	struct fdc_ivars *ivar;
8246182fdbdSPeter Wemm 	device_t child;
82592200632SGarrett Wollman 
8267cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
82737286586SPeter Wemm 	if (ivar == NULL)
8286182fdbdSPeter Wemm 		return;
82937286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
83037286586SPeter Wemm 		ivar->fdunit = 0;
831fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
83237286586SPeter Wemm 	if (child == NULL)
8336182fdbdSPeter Wemm 		return;
83437286586SPeter Wemm 	device_set_ivars(child, ivar);
835a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
836a97c75b7SDoug Rabson 	    && disabled != 0)
8376182fdbdSPeter Wemm 		device_disable(child);
8386182fdbdSPeter Wemm }
8396182fdbdSPeter Wemm 
8406182fdbdSPeter Wemm static int
8416182fdbdSPeter Wemm fdc_attach(device_t dev)
8426182fdbdSPeter Wemm {
84337286586SPeter Wemm 	struct	fdc_data *fdc;
8442398f0cdSPeter Wemm 	const char *name, *dname;
8455d54fe91SJoerg Wunsch 	int	i, error, dunit;
846427ccf00SDoug Rabson 
84737286586SPeter Wemm 	fdc = device_get_softc(dev);
84837286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
84937286586SPeter Wemm 	if (error) {
850f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
85137286586SPeter Wemm 		return error;
85237286586SPeter Wemm 	}
85337286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
8543c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
8553c36743eSMark Murray 			       &fdc->fdc_intr);
85637286586SPeter Wemm 	if (error) {
85737286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
85837286586SPeter Wemm 		return error;
85937286586SPeter Wemm 	}
86037286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
861fb35bd37SJoerg Wunsch 	fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET;
8626182fdbdSPeter Wemm 
8635f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
864f8ce7dd5SJoerg Wunsch 		/*
865f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
866f8ce7dd5SJoerg Wunsch 		 * the rest
867f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
868f8ce7dd5SJoerg Wunsch 		 */
869100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
870f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
8715f830ea2SJoerg Wunsch 	}
8725b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
8736182fdbdSPeter Wemm 
8743a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
87564860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
8768177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
8775b81b6b3SRodney W. Grimes 
8786182fdbdSPeter Wemm 	/*
87937286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
88037286586SPeter Wemm 	 * devices from the BIOS unless overridden.
8816182fdbdSPeter Wemm 	 */
882ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
8832398f0cdSPeter Wemm 	i = 0;
8842398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
8852398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
88637286586SPeter Wemm 
88760444853SJoerg Wunsch 	if ((error = bus_generic_attach(dev)) != 0)
88860444853SJoerg Wunsch 		return (error);
88960444853SJoerg Wunsch 
89060444853SJoerg Wunsch 	return (0);
8916182fdbdSPeter Wemm }
8926182fdbdSPeter Wemm 
89315317dd8SMatthew N. Dodd static int
8946182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
8956182fdbdSPeter Wemm {
89615317dd8SMatthew N. Dodd 	int retval = 0;
89715317dd8SMatthew N. Dodd 
89815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
89915317dd8SMatthew N. Dodd 	retval += printf(" on %s drive %d\n", device_get_nameunit(me),
90037286586SPeter Wemm 	       fdc_get_fdunit(child));
90115317dd8SMatthew N. Dodd 
90215317dd8SMatthew N. Dodd 	return (retval);
9036182fdbdSPeter Wemm }
9046182fdbdSPeter Wemm 
90516e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
90616e68fc6SPeter Wemm 	/* Device interface */
90716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
90816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
909e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
91016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
91116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
91216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
91316e68fc6SPeter Wemm 
91416e68fc6SPeter Wemm 	/* Bus interface */
91516e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
91637286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
91716e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
91816e68fc6SPeter Wemm 
91916e68fc6SPeter Wemm 	{ 0, 0 }
92016e68fc6SPeter Wemm };
92116e68fc6SPeter Wemm 
92216e68fc6SPeter Wemm static driver_t fdc_driver = {
92316e68fc6SPeter Wemm 	"fdc",
92416e68fc6SPeter Wemm 	fdc_methods,
92516e68fc6SPeter Wemm 	sizeof(struct fdc_data)
92616e68fc6SPeter Wemm };
92716e68fc6SPeter Wemm 
92816e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
92916e68fc6SPeter Wemm 
9305f830ea2SJoerg Wunsch #if NCARD > 0
9315f830ea2SJoerg Wunsch 
9325f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
9335f830ea2SJoerg Wunsch 	/* Device interface */
9345f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
9355f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
936e219897aSJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_detach),
9375f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
9385f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
9395f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
9405f830ea2SJoerg Wunsch 
9415f830ea2SJoerg Wunsch 	/* Bus interface */
9425f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
9435f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
9445f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
9455f830ea2SJoerg Wunsch 
9465f830ea2SJoerg Wunsch 	{ 0, 0 }
9475f830ea2SJoerg Wunsch };
9485f830ea2SJoerg Wunsch 
9495f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
9505f830ea2SJoerg Wunsch 	"fdc",
9515f830ea2SJoerg Wunsch 	fdc_pccard_methods,
9525f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
9535f830ea2SJoerg Wunsch };
9545f830ea2SJoerg Wunsch 
9555f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
9565f830ea2SJoerg Wunsch 
9575f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
9585f830ea2SJoerg Wunsch 
9593f54a085SPoul-Henning Kamp static struct {
9603f54a085SPoul-Henning Kamp 	char *match;
9613f54a085SPoul-Henning Kamp 	int minor;
9623f54a085SPoul-Henning Kamp 	int link;
9633f54a085SPoul-Henning Kamp } fd_suffix[] = {
96460444853SJoerg Wunsch 	/*
96560444853SJoerg Wunsch 	 * Genuine clone devices must come first, and their number must
96660444853SJoerg Wunsch 	 * match NCLONEDEVS above.
96760444853SJoerg Wunsch 	 */
9683f54a085SPoul-Henning Kamp 	{ ".1720",	1,	0 },
9693f54a085SPoul-Henning Kamp 	{ ".1480",	2,	0 },
9703f54a085SPoul-Henning Kamp 	{ ".1440",	3,	0 },
9713f54a085SPoul-Henning Kamp 	{ ".1200",	4,	0 },
9723f54a085SPoul-Henning Kamp 	{ ".820",	5,	0 },
9733f54a085SPoul-Henning Kamp 	{ ".800",	6,	0 },
9743f54a085SPoul-Henning Kamp 	{ ".720",	7,	0 },
9753f54a085SPoul-Henning Kamp 	{ ".360",	8,	0 },
9763f54a085SPoul-Henning Kamp 	{ ".640",	9,	0 },
9773f54a085SPoul-Henning Kamp 	{ ".1232",	10,	0 },
97860444853SJoerg Wunsch 	{ "a",		0,	1 },
97960444853SJoerg Wunsch 	{ "b",		0,	1 },
98060444853SJoerg Wunsch 	{ "c",		0,	1 },
98160444853SJoerg Wunsch 	{ "d",		0,	1 },
98260444853SJoerg Wunsch 	{ "e",		0,	1 },
98360444853SJoerg Wunsch 	{ "f",		0,	1 },
98460444853SJoerg Wunsch 	{ "g",		0,	1 },
98560444853SJoerg Wunsch 	{ "h",		0,	1 },
9863f54a085SPoul-Henning Kamp 	{ 0, 0 }
9873f54a085SPoul-Henning Kamp };
9883f54a085SPoul-Henning Kamp static void
98980909a7dSJoerg Wunsch fd_clone(void *arg, char *name, int namelen, dev_t *dev)
9903f54a085SPoul-Henning Kamp {
99160444853SJoerg Wunsch 	struct	fd_data *fd;
9923f54a085SPoul-Henning Kamp 	int u, d, i;
9933f54a085SPoul-Henning Kamp 	char *n;
9943f54a085SPoul-Henning Kamp 
99560444853SJoerg Wunsch 	fd = (struct fd_data *)arg;
9963f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
9973f54a085SPoul-Henning Kamp 		return;
998db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
9993f54a085SPoul-Henning Kamp 		return;
10003f54a085SPoul-Henning Kamp 	for (i = 0; ; i++) {
10013f54a085SPoul-Henning Kamp 		if (fd_suffix[i].match == NULL)
10023f54a085SPoul-Henning Kamp 			return;
10033f54a085SPoul-Henning Kamp 		if (strcmp(n, fd_suffix[i].match))
10043f54a085SPoul-Henning Kamp 			continue;
10053f54a085SPoul-Henning Kamp 		d = fd_suffix[i].minor;
10063f54a085SPoul-Henning Kamp 		break;
10073f54a085SPoul-Henning Kamp 	}
10083f54a085SPoul-Henning Kamp 	if (fd_suffix[i].link == 0) {
10093f54a085SPoul-Henning Kamp 		*dev = make_dev(&fd_cdevsw, (u << 6) + d,
10103f54a085SPoul-Henning Kamp 			UID_ROOT, GID_OPERATOR, 0640, name);
101160444853SJoerg Wunsch 		fd->clonedevs[i] = *dev;
10123f54a085SPoul-Henning Kamp 	} else {
101360444853SJoerg Wunsch 		*dev = make_dev_alias(fd->masterdev, name);
10143f54a085SPoul-Henning Kamp 	}
10153f54a085SPoul-Henning Kamp }
10163f54a085SPoul-Henning Kamp 
101716e68fc6SPeter Wemm /******************************************************************/
101816e68fc6SPeter Wemm /*
101916e68fc6SPeter Wemm  * devices attached to the controller section.
102016e68fc6SPeter Wemm  */
10216182fdbdSPeter Wemm static int
10226182fdbdSPeter Wemm fd_probe(device_t dev)
10236182fdbdSPeter Wemm {
10246182fdbdSPeter Wemm 	int	i;
10256182fdbdSPeter Wemm 	u_int	fdt, st0, st3;
10266182fdbdSPeter Wemm 	struct	fd_data *fd;
10276182fdbdSPeter Wemm 	struct	fdc_data *fdc;
10286182fdbdSPeter Wemm 	fdsu_t	fdsu;
10296182fdbdSPeter Wemm 	static int fd_fifo = 0;
10306182fdbdSPeter Wemm 
10316182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
10326182fdbdSPeter Wemm 	fd = device_get_softc(dev);
10336182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
10346182fdbdSPeter Wemm 
10356182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
10366182fdbdSPeter Wemm 	fd->dev = dev;
10376182fdbdSPeter Wemm 	fd->fdc = fdc;
10386182fdbdSPeter Wemm 	fd->fdsu = fdsu;
10396182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
10406182fdbdSPeter Wemm 
1041a97c75b7SDoug Rabson #ifdef __i386__
1042b99f0a4aSAndrew Moore 	/* look up what bios thinks we have */
10436182fdbdSPeter Wemm 	switch (fd->fdu) {
10446182fdbdSPeter Wemm 	case 0:
10455f830ea2SJoerg Wunsch 		if ((fdc->flags & FDC_ISPCMCIA))
10465f830ea2SJoerg Wunsch 			fdt = RTCFDT_144M;
10475f830ea2SJoerg Wunsch 		else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
10480722d6abSJoerg Wunsch 			fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
10490722d6abSJoerg Wunsch 		else
10500722d6abSJoerg Wunsch 			fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1051b99f0a4aSAndrew Moore 		break;
10526182fdbdSPeter Wemm 	case 1:
10536182fdbdSPeter Wemm 		fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1054dc5df763SJoerg Wunsch 		break;
1055dc5df763SJoerg Wunsch 	default:
10566182fdbdSPeter Wemm 		fdt = RTCFDT_NONE;
1057dc5df763SJoerg Wunsch 		break;
10586b7bd95bSJoerg Wunsch 	}
1059a97c75b7SDoug Rabson #else
1060a97c75b7SDoug Rabson 	fdt = RTCFDT_144M;	/* XXX probably */
1061a97c75b7SDoug Rabson #endif
10626182fdbdSPeter Wemm 
10636182fdbdSPeter Wemm 	/* is there a unit? */
10646182fdbdSPeter Wemm 	if (fdt == RTCFDT_NONE)
10656182fdbdSPeter Wemm 		return (ENXIO);
10666182fdbdSPeter Wemm 
10676182fdbdSPeter Wemm 	/* select it */
10686182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
1069fb35bd37SJoerg Wunsch 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
10706182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
10716182fdbdSPeter Wemm 
10728de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
10736182fdbdSPeter Wemm 	if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1074e34c71eaSJoerg Wunsch 	    && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
10756182fdbdSPeter Wemm 	    && enable_fifo(fdc) == 0) {
1076b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1077b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1078d66c374fSTor Egge 	}
10796182fdbdSPeter Wemm 	fd_fifo = 1;
10806182fdbdSPeter Wemm 
10816182fdbdSPeter Wemm 	if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
10826182fdbdSPeter Wemm 	    && (st3 & NE7_ST3_T0)) {
1083dc5df763SJoerg Wunsch 		/* if at track 0, first seek inwards */
1084dc5df763SJoerg Wunsch 		/* seek some steps: */
10856182fdbdSPeter Wemm 		fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1086dc5df763SJoerg Wunsch 		DELAY(300000); /* ...wait a moment... */
10876182fdbdSPeter Wemm 		fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1088dc5df763SJoerg Wunsch 	}
1089dc5df763SJoerg Wunsch 
1090dc5df763SJoerg Wunsch 	/* If we're at track 0 first seek inwards. */
10916182fdbdSPeter Wemm 	if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1092dc5df763SJoerg Wunsch 		/* Seek some steps... */
10936182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1094dc5df763SJoerg Wunsch 			/* ...wait a moment... */
1095dc5df763SJoerg Wunsch 			DELAY(300000);
1096dc5df763SJoerg Wunsch 			/* make ctrlr happy: */
10976182fdbdSPeter Wemm 			fd_sense_int(fdc, 0, 0);
1098dc5df763SJoerg Wunsch 		}
1099dc5df763SJoerg Wunsch 	}
1100dc5df763SJoerg Wunsch 
11016b7bd95bSJoerg Wunsch 	for (i = 0; i < 2; i++) {
11026b7bd95bSJoerg Wunsch 		/*
11036b7bd95bSJoerg Wunsch 		 * we must recalibrate twice, just in case the
11046b7bd95bSJoerg Wunsch 		 * heads have been beyond cylinder 76, since most
11056b7bd95bSJoerg Wunsch 		 * FDCs still barf when attempting to recalibrate
11066b7bd95bSJoerg Wunsch 		 * more than 77 steps
11076b7bd95bSJoerg Wunsch 		 */
1108dc5df763SJoerg Wunsch 		/* go back to 0: */
11096182fdbdSPeter Wemm 		if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
11106b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek*/
11116b7bd95bSJoerg Wunsch 			DELAY(i == 0 ? 1000000 : 300000);
11125b81b6b3SRodney W. Grimes 
11136b7bd95bSJoerg Wunsch 			/* anything responding? */
1114dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, 0) == 0 &&
1115dc5df763SJoerg Wunsch 			    (st0 & NE7_ST0_EC) == 0)
11166b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
11176b7bd95bSJoerg Wunsch 		}
1118dc5df763SJoerg Wunsch 	}
11196b7bd95bSJoerg Wunsch 
11206182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
11213a2f7427SDavid Greenman 
11223a2f7427SDavid Greenman 	if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
11236182fdbdSPeter Wemm 		return (ENXIO);
11245b81b6b3SRodney W. Grimes 
1125dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1126b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1127b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
11283a2f7427SDavid Greenman 	fd->options = 0;
112902a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
113002a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
11315b81b6b3SRodney W. Grimes 
1132b99f0a4aSAndrew Moore 	switch (fdt) {
11337ca0641bSAndrey A. Chernov 	case RTCFDT_12M:
11346182fdbdSPeter Wemm 		device_set_desc(dev, "1200-KB 5.25\" drive");
1135b99f0a4aSAndrew Moore 		fd->type = FD_1200;
11367ca0641bSAndrey A. Chernov 		break;
11370722d6abSJoerg Wunsch 	case RTCFDT_144M | RTCFDT_144M_PRETENDED:
11386182fdbdSPeter Wemm 		device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
11390722d6abSJoerg Wunsch 		fdt = RTCFDT_144M;
11406182fdbdSPeter Wemm 		fd->type = FD_1440;
11417ca0641bSAndrey A. Chernov 	case RTCFDT_144M:
11426182fdbdSPeter Wemm 		device_set_desc(dev, "1440-KB 3.5\" drive");
1143b99f0a4aSAndrew Moore 		fd->type = FD_1440;
11447ca0641bSAndrey A. Chernov 		break;
1145290dd077SJoerg Wunsch 	case RTCFDT_288M:
114686a727d9SJoerg Wunsch 	case RTCFDT_288M_1:
11476182fdbdSPeter Wemm 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1148290dd077SJoerg Wunsch 		fd->type = FD_1440;
1149290dd077SJoerg Wunsch 		break;
11507ca0641bSAndrey A. Chernov 	case RTCFDT_360K:
11516182fdbdSPeter Wemm 		device_set_desc(dev, "360-KB 5.25\" drive");
1152b99f0a4aSAndrew Moore 		fd->type = FD_360;
11537ca0641bSAndrey A. Chernov 		break;
1154ed2fa05eSAndrey A. Chernov 	case RTCFDT_720K:
11556182fdbdSPeter Wemm 		printf("720-KB 3.5\" drive");
1156b99f0a4aSAndrew Moore 		fd->type = FD_720;
1157ed2fa05eSAndrey A. Chernov 		break;
11587ca0641bSAndrey A. Chernov 	default:
11596182fdbdSPeter Wemm 		return (ENXIO);
11605b81b6b3SRodney W. Grimes 	}
11616182fdbdSPeter Wemm 	return (0);
11626182fdbdSPeter Wemm }
11636182fdbdSPeter Wemm 
11646182fdbdSPeter Wemm static int
11656182fdbdSPeter Wemm fd_attach(device_t dev)
11666182fdbdSPeter Wemm {
11676182fdbdSPeter Wemm 	struct	fd_data *fd;
116864860614SJoerg Wunsch 	static	int cdevsw_add_done;
116960444853SJoerg Wunsch 	int i;
11706182fdbdSPeter Wemm 
117102211baeSNick Hibma 	if (!cdevsw_add_done) {
1172475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
117364860614SJoerg Wunsch 		cdevsw_add_done = 1;
117402211baeSNick Hibma 	}
117564860614SJoerg Wunsch 	fd = device_get_softc(dev);
117660444853SJoerg Wunsch 	fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000);
11775f431174SJoerg Wunsch 	fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6,
11783f54a085SPoul-Henning Kamp 				 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
117960444853SJoerg Wunsch 	for (i = 0; i < NCLONEDEVS; i++)
118060444853SJoerg Wunsch 		fd->clonedevs[i] = NODEV;
11816182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
1182f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
11832a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
11842a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
11856182fdbdSPeter Wemm 	return (0);
11865b81b6b3SRodney W. Grimes }
11875b81b6b3SRodney W. Grimes 
11885f830ea2SJoerg Wunsch static int
11895f830ea2SJoerg Wunsch fd_detach(device_t dev)
11905f830ea2SJoerg Wunsch {
11915f830ea2SJoerg Wunsch 	struct	fd_data *fd;
119260444853SJoerg Wunsch 	int i;
11935f830ea2SJoerg Wunsch 
11945f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
119560444853SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
1196e219897aSJoerg Wunsch 	devstat_remove_entry(&fd->device_stats);
1197e219897aSJoerg Wunsch 	destroy_dev(fd->masterdev);
119860444853SJoerg Wunsch 	for (i = 0; i < NCLONEDEVS; i++)
119960444853SJoerg Wunsch 		if (fd->clonedevs[i] != NODEV)
120060444853SJoerg Wunsch 			destroy_dev(fd->clonedevs[i]);
1201e219897aSJoerg Wunsch 	cdevsw_remove(&fd_cdevsw);
1202e219897aSJoerg Wunsch 	EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag);
12035f830ea2SJoerg Wunsch 
12045f830ea2SJoerg Wunsch 	return (0);
12055f830ea2SJoerg Wunsch }
12065f830ea2SJoerg Wunsch 
120716e68fc6SPeter Wemm static device_method_t fd_methods[] = {
120816e68fc6SPeter Wemm 	/* Device interface */
120916e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
121016e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
12115f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
121216e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
121316e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
121416e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
121516e68fc6SPeter Wemm 
121616e68fc6SPeter Wemm 	{ 0, 0 }
121716e68fc6SPeter Wemm };
121816e68fc6SPeter Wemm 
121916e68fc6SPeter Wemm static driver_t fd_driver = {
122016e68fc6SPeter Wemm 	"fd",
122116e68fc6SPeter Wemm 	fd_methods,
122216e68fc6SPeter Wemm 	sizeof(struct fd_data)
122316e68fc6SPeter Wemm };
122416e68fc6SPeter Wemm 
1225475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
122616e68fc6SPeter Wemm 
12275b81b6b3SRodney W. Grimes /****************************************************************************/
12285b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
12295e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
12305b81b6b3SRodney W. Grimes /****************************************************************************/
12313a2f7427SDavid Greenman static void
12326182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
12335b81b6b3SRodney W. Grimes {
1234fb35bd37SJoerg Wunsch 	int fdout;
12353a2f7427SDavid Greenman 
1236fb35bd37SJoerg Wunsch 	fdout = fdc->fdout;
12373a2f7427SDavid Greenman 	if (turnon) {
12383a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
1239fb35bd37SJoerg Wunsch 		fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu;
12403a2f7427SDavid Greenman 	} else
12413a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
12426182fdbdSPeter Wemm 	fdc->fdout = fdout;
1243fb35bd37SJoerg Wunsch 	fdout_wr(fdc, fdout);
12443a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
12453a2f7427SDavid Greenman }
12463a2f7427SDavid Greenman 
1247381fe1aaSGarrett Wollman static void
12486182fdbdSPeter Wemm fd_turnoff(void *xfd)
12495b81b6b3SRodney W. Grimes {
1250f5f7ba03SJordan K. Hubbard 	int	s;
12516182fdbdSPeter Wemm 	fd_p fd = xfd;
1252dc16046fSJoerg Wunsch 
12536182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
12548335c1b8SBruce Evans 
12555f830ea2SJoerg Wunsch 	s = splbio();
12568335c1b8SBruce Evans 	/*
12578335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
12585f830ea2SJoerg Wunsch 	 *
12595f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
12605f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
12615f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
12625f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
12638335c1b8SBruce Evans 	 */
12646182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
12655f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
12665f830ea2SJoerg Wunsch 		splx(s);
12678335c1b8SBruce Evans 		return;
12688335c1b8SBruce Evans 	}
12698335c1b8SBruce Evans 
12705b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
12716182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1272f5f7ba03SJordan K. Hubbard 	splx(s);
12735b81b6b3SRodney W. Grimes }
12745b81b6b3SRodney W. Grimes 
12753a2f7427SDavid Greenman static void
12766182fdbdSPeter Wemm fd_motor_on(void *xfd)
12775b81b6b3SRodney W. Grimes {
1278f5f7ba03SJordan K. Hubbard 	int	s;
12796182fdbdSPeter Wemm 	fd_p fd = xfd;
1280f5f7ba03SJordan K. Hubbard 
1281f5f7ba03SJordan K. Hubbard 	s = splbio();
12825b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
12835b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
12845b81b6b3SRodney W. Grimes 	{
12856182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
12865b81b6b3SRodney W. Grimes 	}
1287f5f7ba03SJordan K. Hubbard 	splx(s);
12885b81b6b3SRodney W. Grimes }
12895b81b6b3SRodney W. Grimes 
12903a2f7427SDavid Greenman static void
12916182fdbdSPeter Wemm fd_turnon(fd_p fd)
12925b81b6b3SRodney W. Grimes {
12935b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
12945b81b6b3SRodney W. Grimes 	{
12953a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
12966182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
12976182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
12985b81b6b3SRodney W. Grimes 	}
12995b81b6b3SRodney W. Grimes }
13005b81b6b3SRodney W. Grimes 
1301381fe1aaSGarrett Wollman static void
1302dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
13035b81b6b3SRodney W. Grimes {
13043a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1305427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
13063a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
13073a2f7427SDavid Greenman 	DELAY(100);
13083a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1309427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
13103a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
13113a2f7427SDavid Greenman 	DELAY(100);
1312427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
13133a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
13143a2f7427SDavid Greenman 
131564860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
13166182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1317dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1318dc5df763SJoerg Wunsch 		     0);
1319d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1320d66c374fSTor Egge 		(void) enable_fifo(fdc);
13215b81b6b3SRodney W. Grimes }
13225b81b6b3SRodney W. Grimes 
13235b81b6b3SRodney W. Grimes /****************************************************************************/
13245b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
13255b81b6b3SRodney W. Grimes /****************************************************************************/
1326b5e8ce9fSBruce Evans static int
13276182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1328dc5df763SJoerg Wunsch {
1329dc5df763SJoerg Wunsch 	int i, j = 100000;
1330427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1331dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1332dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13336182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1334dc5df763SJoerg Wunsch 	if (j <= 0)
13356182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1336d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1337427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1338dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1339dc5df763SJoerg Wunsch 	*ptr = i;
1340dc5df763SJoerg Wunsch 	return 0;
1341d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1342427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1343dc5df763SJoerg Wunsch 	if (ptr)
1344dc5df763SJoerg Wunsch 		*ptr = i;
1345dc5df763SJoerg Wunsch 	return 0;
1346d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1347dc5df763SJoerg Wunsch }
1348dc5df763SJoerg Wunsch 
1349dc5df763SJoerg Wunsch int
13506182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
13515b81b6b3SRodney W. Grimes {
13523b3837dbSRodney W. Grimes 	int i;
13535b81b6b3SRodney W. Grimes 
13543b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
13553b3837dbSRodney W. Grimes 	i = 100000;
1356427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
13576182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
13583b3837dbSRodney W. Grimes 
13593b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
13603b3837dbSRodney W. Grimes 	i = 100000;
1361427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
136216b04b6aSJoerg Wunsch 	if (i <= 0)
13636182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
13643b3837dbSRodney W. Grimes 
13653b3837dbSRodney W. Grimes 	/* Send the command and return */
1366427ccf00SDoug Rabson 	fddata_wr(fdc, x);
13673a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
13685b81b6b3SRodney W. Grimes 	return (0);
13695b81b6b3SRodney W. Grimes }
13705b81b6b3SRodney W. Grimes 
13715b81b6b3SRodney W. Grimes /****************************************************************************/
13725b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
13735b81b6b3SRodney W. Grimes /****************************************************************************/
1374381fe1aaSGarrett Wollman int
1375671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
13765b81b6b3SRodney W. Grimes {
13775b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
137820a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
13796182fdbdSPeter Wemm 	fd_p	fd;
1380b99f0a4aSAndrew Moore 	fdc_p	fdc;
13815b81b6b3SRodney W. Grimes 
13825b81b6b3SRodney W. Grimes 	/* check bounds */
13836182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1384b99f0a4aSAndrew Moore 		return (ENXIO);
13856182fdbdSPeter Wemm 	fdc = fd->fdc;
13866182fdbdSPeter Wemm 	if ((fdc == NULL) || (fd->type == NO_TYPE))
1387b99f0a4aSAndrew Moore 		return (ENXIO);
1388b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1389b99f0a4aSAndrew Moore 		return (ENXIO);
13907ca0641bSAndrey A. Chernov 	if (type == 0)
13916182fdbdSPeter Wemm 		type = fd->type;
13927ca0641bSAndrey A. Chernov 	else {
13933e425b96SJulian Elischer 		/*
13943e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
13953e425b96SJulian Elischer 		 * to open a type it can do,
13963e425b96SJulian Elischer 		 */
13976182fdbdSPeter Wemm 		if (type != fd->type) {
13986182fdbdSPeter Wemm 			switch (fd->type) {
13997ca0641bSAndrey A. Chernov 			case FD_360:
14007ca0641bSAndrey A. Chernov 				return (ENXIO);
1401ed2fa05eSAndrey A. Chernov 			case FD_720:
1402b39c878eSAndrey A. Chernov 				if (   type != FD_820
1403b39c878eSAndrey A. Chernov 				    && type != FD_800
14046fb89845SKATO Takenori 				    && type != FD_640
1405ed2fa05eSAndrey A. Chernov 				   )
1406ed2fa05eSAndrey A. Chernov 					return (ENXIO);
1407ed2fa05eSAndrey A. Chernov 				break;
14087ca0641bSAndrey A. Chernov 			case FD_1200:
1409b39c878eSAndrey A. Chernov 				switch (type) {
1410b39c878eSAndrey A. Chernov 				case FD_1480:
1411b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1412fa4700b4SAndrey A. Chernov 					break;
14137ca0641bSAndrey A. Chernov 				case FD_1440:
1414b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1415b39c878eSAndrey A. Chernov 					break;
14166fb89845SKATO Takenori 				case FD_1232:
14176fb89845SKATO Takenori 					break;
1418b39c878eSAndrey A. Chernov 				case FD_820:
1419b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1420b39c878eSAndrey A. Chernov 					break;
1421b39c878eSAndrey A. Chernov 				case FD_800:
1422b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1423b39c878eSAndrey A. Chernov 					break;
1424b39c878eSAndrey A. Chernov 				case FD_720:
1425b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1426b39c878eSAndrey A. Chernov 					break;
14276fb89845SKATO Takenori 				case FD_640:
14286fb89845SKATO Takenori 					type = FD_640in5_25;
14296fb89845SKATO Takenori 					break;
1430b39c878eSAndrey A. Chernov 				case FD_360:
1431b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1432b39c878eSAndrey A. Chernov 					break;
1433b39c878eSAndrey A. Chernov 				default:
1434b39c878eSAndrey A. Chernov 					return(ENXIO);
1435b39c878eSAndrey A. Chernov 				}
1436b39c878eSAndrey A. Chernov 				break;
1437b39c878eSAndrey A. Chernov 			case FD_1440:
1438b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1439b39c878eSAndrey A. Chernov 				    && type != FD_1480
1440ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1441b39c878eSAndrey A. Chernov 				    && type != FD_820
1442b39c878eSAndrey A. Chernov 				    && type != FD_800
1443b39c878eSAndrey A. Chernov 				    && type != FD_720
14446fb89845SKATO Takenori 				    && type != FD_640
14457ca0641bSAndrey A. Chernov 				    )
1446dffff499SAndrey A. Chernov 					return(ENXIO);
1447fa4700b4SAndrey A. Chernov 				break;
14487ca0641bSAndrey A. Chernov 			}
14497ca0641bSAndrey A. Chernov 		}
1450fa4700b4SAndrey A. Chernov 	}
14516182fdbdSPeter Wemm 	fd->ft = fd_types + type - 1;
14526182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
14533fef646eSJoerg Wunsch 	/*
14543fef646eSJoerg Wunsch 	 * Clearing the DMA overrun counter at open time is a bit messy.
14553fef646eSJoerg Wunsch 	 * Since we're only managing one counter per controller, opening
14563fef646eSJoerg Wunsch 	 * the second drive could mess it up.  Anyway, if the DMA overrun
14573fef646eSJoerg Wunsch 	 * condition is really persistent, it will eventually time out
14583fef646eSJoerg Wunsch 	 * still.  OTOH, clearing it here will ensure we'll at least start
14593fef646eSJoerg Wunsch 	 * trying again after a previous (maybe even long ago) failure.
14603fef646eSJoerg Wunsch 	 * Also, this is merely a stop-gap measure only that should not
14613fef646eSJoerg Wunsch 	 * happen during normal operation, so we can tolerate it to be a
14623fef646eSJoerg Wunsch 	 * bit sloppy about this.
14633fef646eSJoerg Wunsch 	 */
14643fef646eSJoerg Wunsch 	fdc->dma_overruns = 0;
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 {
1488fb35bd37SJoerg Wunsch 	long blknum, nblocks;
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;
150564860614SJoerg Wunsch 	}
1506d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1507f664aeeeSJoerg Wunsch 	if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) {
15088177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1509dc5df763SJoerg Wunsch 			printf(
15106a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
15118177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
15128177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15138177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15143a2f7427SDavid Greenman 			goto bad;
15153a2f7427SDavid Greenman 		}
15168177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
15178177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15188177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15193a2f7427SDavid Greenman 			goto bad;
15203a2f7427SDavid Greenman 		}
15213a2f7427SDavid Greenman 	}
15223a2f7427SDavid Greenman 
15233a2f7427SDavid Greenman 	/*
15243a2f7427SDavid Greenman 	 * Set up block calculations.
15253a2f7427SDavid Greenman 	 */
15268177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1527bb6382faSJoerg Wunsch 		/*
1528bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1529bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1530bb6382faSJoerg Wunsch 		 */
15318177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
15328177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15333a2f7427SDavid Greenman 		goto bad;
15343a2f7427SDavid Greenman 	}
1535fb35bd37SJoerg Wunsch 	blknum = bp->bio_blkno * DEV_BSIZE / fdblk;
1536bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
1537fb35bd37SJoerg Wunsch 	if (blknum + bp->bio_bcount / fdblk > nblocks) {
1538fb35bd37SJoerg Wunsch 		if (blknum >= nblocks) {
1539fb35bd37SJoerg Wunsch 			if (bp->bio_cmd == BIO_READ)
1540fb35bd37SJoerg Wunsch 				bp->bio_resid = bp->bio_bcount;
1541fb35bd37SJoerg Wunsch 			else {
1542fb35bd37SJoerg Wunsch 				bp->bio_error = ENOSPC;
15438177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
1544bb6382faSJoerg Wunsch 			}
1545fb35bd37SJoerg Wunsch 			goto bad;	/* not always bad, but EOF */
1546fb35bd37SJoerg Wunsch 		}
1547fb35bd37SJoerg Wunsch 		bp->bio_bcount = (nblocks - blknum) * fdblk;
1548bb6382faSJoerg Wunsch 	}
15498177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
15503a2f7427SDavid Greenman 	s = splbio();
15518177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
15526182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1553b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
15545f830ea2SJoerg Wunsch 	device_busy(fd->dev);
15556182fdbdSPeter Wemm 	fdstart(fdc);
15563a2f7427SDavid Greenman 	splx(s);
15573a2f7427SDavid Greenman 	return;
15583a2f7427SDavid Greenman 
15593a2f7427SDavid Greenman bad:
15603a2f7427SDavid Greenman 	biodone(bp);
15613a2f7427SDavid Greenman }
15623a2f7427SDavid Greenman 
15635b81b6b3SRodney W. Grimes /***************************************************************\
15645b81b6b3SRodney W. Grimes *				fdstart				*
15655b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
15665b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
15675b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
15685b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
15695b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
15705b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
15715b81b6b3SRodney W. Grimes \***************************************************************/
1572381fe1aaSGarrett Wollman static void
15736182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
15745b81b6b3SRodney W. Grimes {
15755b81b6b3SRodney W. Grimes 	int s;
15765b81b6b3SRodney W. Grimes 
15775b81b6b3SRodney W. Grimes 	s = splbio();
15786182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
15795b81b6b3SRodney W. Grimes 	{
15806182fdbdSPeter Wemm 		fdc_intr(fdc);
15815b81b6b3SRodney W. Grimes 	}
15825b81b6b3SRodney W. Grimes 	splx(s);
15835b81b6b3SRodney W. Grimes }
15845b81b6b3SRodney W. Grimes 
1585381fe1aaSGarrett Wollman static void
15866182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
15875b81b6b3SRodney W. Grimes {
15885c1a1eaeSBruce Evans  	fdc_p fdc;
1589f5f7ba03SJordan K. Hubbard 	int s;
15905b81b6b3SRodney W. Grimes 
15916182fdbdSPeter Wemm 	fdc = xfdc;
15925c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
15935b81b6b3SRodney W. Grimes 
15943a2f7427SDavid Greenman 	/*
15953a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
15963a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
15973a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
15983a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
15993a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
16005c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
16015c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
16025c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
16035c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
16043a2f7427SDavid Greenman 	 */
16053a2f7427SDavid Greenman 	s = splbio();
16065c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
16075c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
16085c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
16096182fdbdSPeter Wemm 	fdc_intr(fdc);
1610f5f7ba03SJordan K. Hubbard 	splx(s);
16115b81b6b3SRodney W. Grimes }
16125b81b6b3SRodney W. Grimes 
16135b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1614381fe1aaSGarrett Wollman static void
16156182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
16165b81b6b3SRodney W. Grimes {
16175b81b6b3SRodney W. Grimes 	int	s;
16183a2f7427SDavid Greenman 
16195b81b6b3SRodney W. Grimes 	s = splbio();
16206182fdbdSPeter Wemm 	fdc_intr(xfdc);
16215b81b6b3SRodney W. Grimes 	splx(s);
16225b81b6b3SRodney W. Grimes }
16235b81b6b3SRodney W. Grimes 
16245b81b6b3SRodney W. Grimes /***********************************************************************\
162564860614SJoerg Wunsch *                                 fdc_intr				*
16265b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
16275b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
16285b81b6b3SRodney W. Grimes \***********************************************************************/
1629fe310de8SBruce Evans static void
16306182fdbdSPeter Wemm fdc_intr(void *xfdc)
16315b81b6b3SRodney W. Grimes {
16326182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
16336182fdbdSPeter Wemm 	while(fdstate(fdc))
1634381fe1aaSGarrett Wollman 		;
16355b81b6b3SRodney W. Grimes }
16365b81b6b3SRodney W. Grimes 
163769acd21dSWarner Losh /*
163869acd21dSWarner Losh  * magic pseudo-DMA initialization for YE FDC. Sets count and
163969acd21dSWarner Losh  * direction
164069acd21dSWarner Losh  */
16413b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
16423b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
16433b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
16443b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
16453b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
164669acd21dSWarner Losh 
164769acd21dSWarner Losh /*
164869acd21dSWarner Losh  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
164969acd21dSWarner Losh  */
16504c34deeeSJoerg Wunsch static int
16514c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
165269acd21dSWarner Losh {
165369acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
165469acd21dSWarner Losh 
165521144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
165669acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
165769acd21dSWarner Losh 			fdc->state = PIOREAD;
165869acd21dSWarner Losh 			return(0);
165964860614SJoerg Wunsch 		}
16603b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
16613b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16623b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
166369acd21dSWarner Losh 	} else {
16643b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16653b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
16663b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
166764860614SJoerg Wunsch 	}
166869acd21dSWarner Losh 	return(1);
166969acd21dSWarner Losh }
167069acd21dSWarner Losh 
16715b81b6b3SRodney W. Grimes /***********************************************************************\
16725b81b6b3SRodney W. Grimes * The controller state machine.						*
16735b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
16745b81b6b3SRodney W. Grimes \***********************************************************************/
16753a2f7427SDavid Greenman static int
16766182fdbdSPeter Wemm fdstate(fdc_p fdc)
16775b81b6b3SRodney W. Grimes {
167864860614SJoerg Wunsch 	struct fdc_readid *idp;
1679fb35bd37SJoerg Wunsch 	int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac;
1680fb35bd37SJoerg Wunsch 	int st0, cyl, st3, idf;
1681fb35bd37SJoerg Wunsch 	unsigned long blknum;
16825b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
16835b81b6b3SRodney W. Grimes 	fd_p fd;
16848177437dSPoul-Henning Kamp 	register struct bio *bp;
1685b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
16863a2f7427SDavid Greenman 	size_t fdblk;
16875b81b6b3SRodney W. Grimes 
1688e93e63cbSBruce Evans 	bp = fdc->bp;
1689e93e63cbSBruce Evans 	if (bp == NULL) {
16908177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1691e93e63cbSBruce Evans 		if (bp != NULL) {
16928177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1693e93e63cbSBruce Evans 			fdc->bp = bp;
1694e93e63cbSBruce Evans 		}
1695e93e63cbSBruce Evans 	}
1696e93e63cbSBruce Evans 	if (bp == NULL) {
16975b81b6b3SRodney W. Grimes 		/***********************************************\
16985b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
16995b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
17005b81b6b3SRodney W. Grimes 		\***********************************************/
17015b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
17026182fdbdSPeter Wemm 		if (fdc->fd) {
1703b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1704b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
17055b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17065b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17075b81b6b3SRodney W. Grimes 		}
17086182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
17095b81b6b3SRodney W. Grimes  		return (0);
17105b81b6b3SRodney W. Grimes 	}
17118177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
17126182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
17133a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1714b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1715b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
17168177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
171756a23089SPoul-Henning Kamp 	if (read)
171856a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
171956a23089SPoul-Henning Kamp 	else
172056a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
172164860614SJoerg Wunsch 	format = bp->bio_cmd == BIO_FORMAT;
1722f664aeeeSJoerg Wunsch 	rdsectid = bp->bio_cmd == BIO_RDSECTID;
1723fb35bd37SJoerg Wunsch 	if (format)
17248177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
17255b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
17265b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
17275b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
17286182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
17296182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
17305b81b6b3SRodney W. Grimes 	switch (fdc->state)
17315b81b6b3SRodney W. Grimes 	{
17325b81b6b3SRodney W. Grimes 	case DEVIDLE:
17335b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
17345b81b6b3SRodney W. Grimes 		fdc->retry = 0;
17355b81b6b3SRodney W. Grimes 		fd->skip = 0;
17365b81b6b3SRodney W. Grimes 		fdc->fd = fd;
17375b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
17385f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
17393a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
17405b81b6b3SRodney W. Grimes 		/*******************************************************\
17415b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1742dc733423SDag-Erling Smørgrav 		* it will start up in its own good time			*
17435b81b6b3SRodney W. Grimes 		\*******************************************************/
17446182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
17455b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17465b81b6b3SRodney W. Grimes 			return (0); /* come back later */
17475b81b6b3SRodney W. Grimes 		}
17485b81b6b3SRodney W. Grimes 		/*******************************************************\
17495b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
17505b81b6b3SRodney W. Grimes 		\*******************************************************/
17515b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
17525b81b6b3SRodney W. Grimes 		{
17535b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17546182fdbdSPeter Wemm 			fd_turnon(fd);
17555b81b6b3SRodney W. Grimes 			return (0);
17565b81b6b3SRodney W. Grimes 		}
17575b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
17585b81b6b3SRodney W. Grimes 		{
17596182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
17605b81b6b3SRodney W. Grimes 		}
17615c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17625c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17635c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17645c1a1eaeSBruce Evans 		} else
17655e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
1766fb35bd37SJoerg Wunsch 		return (1);	/* come back immediately */
1767fb35bd37SJoerg Wunsch 
17685b81b6b3SRodney W. Grimes 	case DOSEEK:
1769fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
1770fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1771fb35bd37SJoerg Wunsch 		if (cylinder == fd->track)
17725b81b6b3SRodney W. Grimes 		{
17735b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
1774fb35bd37SJoerg Wunsch 			return (1); /* come back immediately */
17755b81b6b3SRodney W. Grimes 		}
17766182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1777fb35bd37SJoerg Wunsch 			   fd->fdsu, cylinder * fd->ft->steptrac,
1778dc5df763SJoerg Wunsch 			   0))
1779dc8603e3SJoerg Wunsch 		{
1780dc8603e3SJoerg Wunsch 			/*
1781dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1782dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1783dc8603e3SJoerg Wunsch 			 */
1784dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
17856182fdbdSPeter Wemm 			return(retrier(fdc));
1786dc8603e3SJoerg Wunsch 		}
1787dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
17885b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
17895b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
1790fb35bd37SJoerg Wunsch 
17915b81b6b3SRodney W. Grimes 	case SEEKWAIT:
17925b81b6b3SRodney W. Grimes 		/* allow heads to settle */
17936182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
17945b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17955b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
1796fb35bd37SJoerg Wunsch 
17975b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
1798fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
1799fb35bd37SJoerg Wunsch 		cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1800fb35bd37SJoerg Wunsch 
18015b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
18026182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
1803fb35bd37SJoerg Wunsch 			int descyl = cylinder * fd->ft->steptrac;
18043a2f7427SDavid Greenman 			do {
18053a2f7427SDavid Greenman 				/*
1806dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1807dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1808dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1809dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1810dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1811dc5df763SJoerg Wunsch 				 *
1812dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1813dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1814dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1815dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1816dc5df763SJoerg Wunsch 				 * real interrupt condition.
1817dc5df763SJoerg Wunsch 				 *
1818dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1819dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1820dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1821dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1822dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1823dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1824dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
18253a2f7427SDavid Greenman 				 */
1826dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1827dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1828dc5df763SJoerg Wunsch 					return 0;
1829dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1830dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1831dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
18323a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1833dc5df763SJoerg Wunsch 
18346182fdbdSPeter Wemm 			if (0 == descyl) {
1835dc5df763SJoerg Wunsch 				int failed = 0;
18363a2f7427SDavid Greenman 				/*
18373a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
18383a2f7427SDavid Greenman 				 * really there
18393a2f7427SDavid Greenman 				 */
1840dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1841dc5df763SJoerg Wunsch 					failed = 1;
18423a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
18433a2f7427SDavid Greenman 					printf(
18443a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
18453a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1846dc5df763SJoerg Wunsch 					failed = 1;
1847dc5df763SJoerg Wunsch 				}
1848dc5df763SJoerg Wunsch 
18496182fdbdSPeter Wemm 				if (failed) {
18503a2f7427SDavid Greenman 					if(fdc->retry < 3)
18513a2f7427SDavid Greenman 						fdc->retry = 3;
18526182fdbdSPeter Wemm 					return (retrier(fdc));
18533a2f7427SDavid Greenman 				}
18543a2f7427SDavid Greenman 			}
1855dc5df763SJoerg Wunsch 
18566182fdbdSPeter Wemm 			if (cyl != descyl) {
18573a2f7427SDavid Greenman 				printf(
18583a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
18592d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1860e5d7d243SBruce Evans 				if (fdc->retry < 3)
1861e5d7d243SBruce Evans 					fdc->retry = 3;
18626182fdbdSPeter Wemm 				return (retrier(fdc));
18635b81b6b3SRodney W. Grimes 			}
18645b81b6b3SRodney W. Grimes 		}
18655b81b6b3SRodney W. Grimes 
1866fb35bd37SJoerg Wunsch 		fd->track = cylinder;
1867fb35bd37SJoerg Wunsch 		if (format)
1868fb35bd37SJoerg Wunsch 			fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1869fb35bd37SJoerg Wunsch 			    - (char *)finfo;
1870250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
18718177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
18728177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
1873fb35bd37SJoerg Wunsch 		blknum = bp->bio_pblkno + fd->skip / fdblk;
18745b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
18755b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
18765b81b6b3SRodney W. Grimes 		head = sec / sectrac;
18775b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
18783a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
18793a2f7427SDavid Greenman 
1880250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
18813a2f7427SDavid Greenman 		{
18823a2f7427SDavid Greenman 			/* make sure the drive is writable */
1883dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1884dc8603e3SJoerg Wunsch 			{
1885dc8603e3SJoerg Wunsch 				/* stuck controller? */
18865f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
188756a23089SPoul-Henning Kamp 					isa_dmadone(idf,
18888177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
18898177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
18905c1a1eaeSBruce Evans 						    fdc->dmachan);
1891dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
18926182fdbdSPeter Wemm 				return (retrier(fdc));
1893dc8603e3SJoerg Wunsch 			}
18943a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
18953a2f7427SDavid Greenman 			{
18963a2f7427SDavid Greenman 				/*
18973a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
18983a2f7427SDavid Greenman 				 * in order to force the current operation
18993a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
19003a2f7427SDavid Greenman 				 * error - all error handling is done
19013a2f7427SDavid Greenman 				 * by the retrier()
19023a2f7427SDavid Greenman 				 */
19033a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
19043a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
19053a2f7427SDavid Greenman 				fdc->status[2] = 0;
19063a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
19073a2f7427SDavid Greenman 				fdc->status[4] = head;
19083a2f7427SDavid Greenman 				fdc->status[5] = sec;
19093a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
19103a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
19113a2f7427SDavid Greenman 				return (1);
19123a2f7427SDavid Greenman 			}
19133a2f7427SDavid Greenman 		}
19145b81b6b3SRodney W. Grimes 
19156182fdbdSPeter Wemm 		if (format) {
19165f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
19175f830ea2SJoerg Wunsch 				/*
19185f830ea2SJoerg Wunsch 				 * This seems to be necessary for
19195f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
19205f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
19215f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
19225f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
19235f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
19245f830ea2SJoerg Wunsch 				 * the place when trying to read them
19255f830ea2SJoerg Wunsch 				 * back.
19265f830ea2SJoerg Wunsch 				 *
19275f830ea2SJoerg Wunsch 				 * Umpf.
19285f830ea2SJoerg Wunsch 				 */
19298177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
19305f830ea2SJoerg Wunsch 
19318177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
19328177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
19338177437dSPoul-Henning Kamp 					bp->bio_bcount);
19345f830ea2SJoerg Wunsch 
19355f830ea2SJoerg Wunsch 			}
1936b39c878eSAndrey A. Chernov 			/* formatting */
19376182fdbdSPeter Wemm 			if(fd_cmd(fdc, 6,  NE7CMD_FORMAT, head << 2 | fdu,
1938dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1939dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1940dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
19416182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
1942dc8603e3SJoerg Wunsch 				/* controller fell over */
19435f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
194456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19458177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19468177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19475c1a1eaeSBruce Evans 						    fdc->dmachan);
1948dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19496182fdbdSPeter Wemm 				return (retrier(fdc));
1950dc8603e3SJoerg Wunsch 			}
1951250300ebSJoerg Wunsch 		} else if (rdsectid) {
1952250300ebSJoerg Wunsch 			if (fd_cmd(fdc, 2, NE7CMD_READID, head << 2 | fdu, 0)) {
1953250300ebSJoerg Wunsch 				/* controller jamming */
1954250300ebSJoerg Wunsch 				fdc->retry = 6;
1955250300ebSJoerg Wunsch 				return (retrier(fdc));
1956250300ebSJoerg Wunsch 			}
19576182fdbdSPeter Wemm 		} else {
1958250300ebSJoerg Wunsch 			/* read or write operation */
19593b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
196069acd21dSWarner Losh 				/*
196169acd21dSWarner Losh 				 * this seems to be necessary even when
196269acd21dSWarner Losh 				 * reading data
196369acd21dSWarner Losh 				 */
19643b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
196569acd21dSWarner Losh 
196669acd21dSWarner Losh 				/*
196769acd21dSWarner Losh 				 * perform the write pseudo-DMA before
196869acd21dSWarner Losh 				 * the WRITE command is sent
196969acd21dSWarner Losh 				 */
197069acd21dSWarner Losh 				if (!read)
19718177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
19728177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
197369acd21dSWarner Losh 					    fdblk);
197469acd21dSWarner Losh 			}
19756182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
1976dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1977dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1978dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1979dc5df763SJoerg Wunsch 				   head,
1980dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1981dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1982dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1983dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1984dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
19856182fdbdSPeter Wemm 				   0)) {
1986dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
19875f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
198856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19898177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19908177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19915c1a1eaeSBruce Evans 						    fdc->dmachan);
1992dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19936182fdbdSPeter Wemm 				return (retrier(fdc));
19945b81b6b3SRodney W. Grimes 			}
1995b39c878eSAndrey A. Chernov 		}
1996250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
199769acd21dSWarner Losh 			/*
199869acd21dSWarner Losh 			 * if this is a read, then simply await interrupt
199969acd21dSWarner Losh 			 * before performing PIO
200069acd21dSWarner Losh 			 */
20018177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
20028177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
20033b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
200469acd21dSWarner Losh 				return(0);      /* will return later */
200564860614SJoerg Wunsch 			}
200669acd21dSWarner Losh 
200769acd21dSWarner Losh 		/*
200869acd21dSWarner Losh 		 * write (or format) operation will fall through and
200969acd21dSWarner Losh 		 * await completion interrupt
201069acd21dSWarner Losh 		 */
20115b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
20126182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
20135b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2014fb35bd37SJoerg Wunsch 
201569acd21dSWarner Losh 	case PIOREAD:
201669acd21dSWarner Losh 		/*
201769acd21dSWarner Losh 		 * actually perform the PIO read.  The IOCOMPLETE case
201869acd21dSWarner Losh 		 * removes the timeout for us.
201969acd21dSWarner Losh 		 */
20208177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
202169acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
202269acd21dSWarner Losh 		/* FALLTHROUGH */
20235b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
20246182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2025dc5df763SJoerg Wunsch 
202664860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2027250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
20288177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
20298177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
20305c1a1eaeSBruce Evans 					    fdc->dmachan);
2031dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2032dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
20336182fdbdSPeter Wemm 			return (retrier(fdc));
20345b81b6b3SRodney W. Grimes   		}
2035dc5df763SJoerg Wunsch 
20363a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2037dc5df763SJoerg Wunsch 
20383a2f7427SDavid Greenman 		/* FALLTHROUGH */
20393a2f7427SDavid Greenman 	case IOTIMEDOUT:
2040250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
20418177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
20428177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
20436182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
20443a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20453a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2046b39c878eSAndrey A. Chernov                                 /*
20473fef646eSJoerg Wunsch 				 * DMA overrun. Someone hogged the bus and
20483fef646eSJoerg Wunsch 				 * didn't release it in time for the next
20493fef646eSJoerg Wunsch 				 * FDC transfer.
20503fef646eSJoerg Wunsch 				 *
20513fef646eSJoerg Wunsch 				 * We normally restart this without bumping
20523fef646eSJoerg Wunsch 				 * the retry counter.  However, in case
20533fef646eSJoerg Wunsch 				 * something is seriously messed up (like
20543fef646eSJoerg Wunsch 				 * broken hardware), we rather limit the
20553fef646eSJoerg Wunsch 				 * number of retries so the IO operation
20563fef646eSJoerg Wunsch 				 * doesn't block indefinately.
2057b39c878eSAndrey A. Chernov 				 */
20583fef646eSJoerg Wunsch 				if (fdc->dma_overruns++ < FDC_DMAOV_MAX) {
2059b39c878eSAndrey A. Chernov 					fdc->state = SEEKCOMPLETE;
2060b39c878eSAndrey A. Chernov 					return (1);
20613fef646eSJoerg Wunsch 				} /* else fall through */
2062b39c878eSAndrey A. Chernov                         }
20633fef646eSJoerg Wunsch 			if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
20643a2f7427SDavid Greenman 				&& fdc->retry < 6)
20653a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
20663a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20673a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
20683a2f7427SDavid Greenman 				&& fdc->retry < 3)
20693a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
20706182fdbdSPeter Wemm 			return (retrier(fdc));
20715b81b6b3SRodney W. Grimes 		}
20725b81b6b3SRodney W. Grimes 		/* All OK */
2073250300ebSJoerg Wunsch 		if (rdsectid) {
2074250300ebSJoerg Wunsch 			/* copy out ID field contents */
207564860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2076250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2077250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2078250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2079250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2080250300ebSJoerg Wunsch 		}
20813fef646eSJoerg Wunsch 		/* Operation successful, retry DMA overruns again next time. */
20823fef646eSJoerg Wunsch 		fdc->dma_overruns = 0;
20833a2f7427SDavid Greenman 		fd->skip += fdblk;
2084fb35bd37SJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount) {
20855b81b6b3SRodney W. Grimes 			/* set up next transfer */
20865b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
20876182fdbdSPeter Wemm 		} else {
20885b81b6b3SRodney W. Grimes 			/* ALL DONE */
20895b81b6b3SRodney W. Grimes 			fd->skip = 0;
2090fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2091e93e63cbSBruce Evans 			fdc->bp = NULL;
20925f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2093a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
20945b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
20955b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
20965b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
20975b81b6b3SRodney W. Grimes 		}
20985b81b6b3SRodney W. Grimes 		return (1);
2099fb35bd37SJoerg Wunsch 
21005b81b6b3SRodney W. Grimes 	case RESETCTLR:
21013a2f7427SDavid Greenman 		fdc_reset(fdc);
21025b81b6b3SRodney W. Grimes 		fdc->retry++;
21035c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
21045c1a1eaeSBruce Evans 		return (0);
2105fb35bd37SJoerg Wunsch 
21065c1a1eaeSBruce Evans 	case RESETCOMPLETE:
21075c1a1eaeSBruce Evans 		/*
21085c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
21095c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
21105c1a1eaeSBruce Evans 		 */
21110e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
21120e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
21135c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
2114fb35bd37SJoerg Wunsch 		/* FALLTHROUGH */
21155c1a1eaeSBruce Evans 	case STARTRECAL:
21166182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2117dc8603e3SJoerg Wunsch 			/* arrgl */
2118dc8603e3SJoerg Wunsch 			fdc->retry = 6;
21196182fdbdSPeter Wemm 			return (retrier(fdc));
2120dc8603e3SJoerg Wunsch 		}
21215b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
21225b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2123fb35bd37SJoerg Wunsch 
21245b81b6b3SRodney W. Grimes 	case RECALWAIT:
21255b81b6b3SRodney W. Grimes 		/* allow heads to settle */
21266182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
21275b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
21285b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
2129fb35bd37SJoerg Wunsch 
21305b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
21313a2f7427SDavid Greenman 		do {
21323a2f7427SDavid Greenman 			/*
2133dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
21343a2f7427SDavid Greenman 			 */
2135dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2136dc5df763SJoerg Wunsch 				return 0;
2137dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2138dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2139dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
21403a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
21413a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
21425b81b6b3SRodney W. Grimes 		{
2143dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2144dc8603e3SJoerg Wunsch 				/*
2145dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
2146dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2147dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2148dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2149dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2150dc8603e3SJoerg Wunsch 				 * time in a line
2151dc8603e3SJoerg Wunsch 				 */
2152dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2153dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
21543a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
21556182fdbdSPeter Wemm 			return (retrier(fdc));
21565b81b6b3SRodney W. Grimes 		}
21575b81b6b3SRodney W. Grimes 		fd->track = 0;
21585b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
21595b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
21605b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
2161fb35bd37SJoerg Wunsch 
21625b81b6b3SRodney W. Grimes 	case MOTORWAIT:
21635b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
21645b81b6b3SRodney W. Grimes 		{
21655b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
21665b81b6b3SRodney W. Grimes 		}
21675c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
21685c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
21695c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
2170fb35bd37SJoerg Wunsch 		} else
2171fb35bd37SJoerg Wunsch 			fdc->state = DOSEEK;
21725b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
2173fb35bd37SJoerg Wunsch 
21745b81b6b3SRodney W. Grimes 	default:
2175b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
217664860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2177a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
21785b81b6b3SRodney W. Grimes 			       fdc->status[0],
21795b81b6b3SRodney W. Grimes 			       fdc->status[1],
21805b81b6b3SRodney W. Grimes 			       fdc->status[2],
21815b81b6b3SRodney W. Grimes 			       fdc->status[3],
21825b81b6b3SRodney W. Grimes 			       fdc->status[4],
21835b81b6b3SRodney W. Grimes 			       fdc->status[5],
21845b81b6b3SRodney W. Grimes 			       fdc->status[6] );
21853a2f7427SDavid Greenman 		else
2186dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2187dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2188dac0f2dbSJoerg Wunsch 		{
2189dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
21905b81b6b3SRodney W. Grimes 			return (0);
21915b81b6b3SRodney W. Grimes 		}
2192dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2193dac0f2dbSJoerg Wunsch 		return (0);
2194dac0f2dbSJoerg Wunsch 	}
2195fb35bd37SJoerg Wunsch 	/* keep the compiler happy -- noone should ever get here */
2196fb35bd37SJoerg Wunsch 	return (999999);
21975b81b6b3SRodney W. Grimes }
21985b81b6b3SRodney W. Grimes 
2199aaf08d94SGarrett Wollman static int
22006182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
22015b81b6b3SRodney W. Grimes {
22028177437dSPoul-Henning Kamp 	struct bio *bp;
22036182fdbdSPeter Wemm 	struct fd_data *fd;
22046182fdbdSPeter Wemm 	int fdu;
22055b81b6b3SRodney W. Grimes 
2206e93e63cbSBruce Evans 	bp = fdc->bp;
22075b81b6b3SRodney W. Grimes 
22086182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
22098177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
22106182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
22116182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
22123a2f7427SDavid Greenman 		goto fail;
22136182fdbdSPeter Wemm 
22146182fdbdSPeter Wemm 	switch (fdc->retry) {
22155b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
22165b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
22175b81b6b3SRodney W. Grimes 		break;
22185b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
22195b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
22205b81b6b3SRodney W. Grimes 		break;
22215b81b6b3SRodney W. Grimes 	case 6:
22225b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
22235b81b6b3SRodney W. Grimes 		break;
22245b81b6b3SRodney W. Grimes 	case 7:
22255b81b6b3SRodney W. Grimes 		break;
22265b81b6b3SRodney W. Grimes 	default:
22273a2f7427SDavid Greenman 	fail:
2228fb35bd37SJoerg Wunsch 		if ((fd->options & FDOPT_NOERRLOG) == 0) {
2229fb35bd37SJoerg Wunsch 			diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
22303a2f7427SDavid Greenman 				(struct disklabel *)NULL);
2231fb35bd37SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID) {
2232dc5df763SJoerg Wunsch 				printf(
2233a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2234dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2235dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2236dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2237dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2238dc5df763SJoerg Wunsch 				       fdc->status[5]);
2239dc5df763SJoerg Wunsch 			}
2240dc5df763SJoerg Wunsch 			else
2241dc5df763SJoerg Wunsch 				printf(" (No status)\n");
22425b81b6b3SRodney W. Grimes 		}
22432995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
22448177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
22458177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
2246fb35bd37SJoerg Wunsch 			bp->bio_resid = bp->bio_bcount - fdc->fd->skip;
2247fb35bd37SJoerg Wunsch 		} else
2248fb35bd37SJoerg Wunsch 			bp->bio_resid = 0;
2249e93e63cbSBruce Evans 		fdc->bp = NULL;
22505b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
22515f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2252a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
225392ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
22545c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
22555b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
22565b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
225792ed385aSRodney W. Grimes 		return (1);
22585b81b6b3SRodney W. Grimes 	}
22595b81b6b3SRodney W. Grimes 	fdc->retry++;
22605b81b6b3SRodney W. Grimes 	return (1);
22615b81b6b3SRodney W. Grimes }
22625b81b6b3SRodney W. Grimes 
22631fdb6e6cSPoul-Henning Kamp static void
22641fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
22651fdb6e6cSPoul-Henning Kamp {
22661fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
22671fdb6e6cSPoul-Henning Kamp }
22681fdb6e6cSPoul-Henning Kamp 
2269b39c878eSAndrey A. Chernov static int
2270f664aeeeSJoerg Wunsch fdmisccmd(dev_t dev, u_int cmd, void *data)
2271b39c878eSAndrey A. Chernov {
2272b39c878eSAndrey A. Chernov  	fdu_t fdu;
2273b39c878eSAndrey A. Chernov  	fd_p fd;
22741fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2275f664aeeeSJoerg Wunsch 	struct fd_formb *finfo;
2276f664aeeeSJoerg Wunsch 	struct fdc_readid *idfield;
22773a2f7427SDavid Greenman 	size_t fdblk;
2278b39c878eSAndrey A. Chernov 
2279b39c878eSAndrey A. Chernov  	fdu = FDUNIT(minor(dev));
22806182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
22813a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2282f664aeeeSJoerg Wunsch 	finfo = (struct fd_formb *)data;
2283f664aeeeSJoerg Wunsch 	idfield = (struct fdc_readid *)data;
2284b39c878eSAndrey A. Chernov 
2285fb35bd37SJoerg Wunsch 	bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO);
2286b39c878eSAndrey A. Chernov 
2287b39c878eSAndrey A. Chernov 	/*
2288f664aeeeSJoerg Wunsch 	 * Set up a bio request for fdstrategy().  bio_blkno is faked
2289f664aeeeSJoerg Wunsch 	 * so that fdstrategy() will seek to the the requested
2290f664aeeeSJoerg Wunsch 	 * cylinder, and use the desired head.  Since we are not
2291f664aeeeSJoerg Wunsch 	 * interested in bioqdisksort() munging with our faked bio
2292f664aeeeSJoerg Wunsch 	 * request, we mark it as being an ordered request.
2293b39c878eSAndrey A. Chernov 	 */
2294f664aeeeSJoerg Wunsch 	bp->bio_cmd = cmd;
2295f664aeeeSJoerg Wunsch 	if (cmd == BIO_FORMAT) {
2296f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2297f664aeeeSJoerg Wunsch 		    (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) +
2298f664aeeeSJoerg Wunsch 		     finfo->head * fd->ft->sectrac) *
2299f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2300f664aeeeSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fd_idfield_data) *
2301f664aeeeSJoerg Wunsch 		    finfo->fd_formb_nsecs;
2302f664aeeeSJoerg Wunsch 	} else if (cmd == BIO_RDSECTID) {
2303f664aeeeSJoerg Wunsch 		bp->bio_blkno =
2304f664aeeeSJoerg Wunsch 		    (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) +
2305f664aeeeSJoerg Wunsch 		     idfield->head * fd->ft->sectrac) *
2306f664aeeeSJoerg Wunsch 		    fdblk / DEV_BSIZE;
2307250300ebSJoerg Wunsch 		bp->bio_bcount = sizeof(struct fdc_readid);
2308f664aeeeSJoerg Wunsch 	} else
2309f664aeeeSJoerg Wunsch 		panic("wrong cmd in fdmisccmd()");
2310f664aeeeSJoerg Wunsch 	bp->bio_data = data;
2311250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2312250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2313f664aeeeSJoerg Wunsch 	bp->bio_flags = BIO_ORDERED;
2314250300ebSJoerg Wunsch 
2315f5f7ba03SJordan K. Hubbard 	/*
2316fb35bd37SJoerg Wunsch 	 * Now run the command.  The wait loop is a version of bufwait()
2317fb35bd37SJoerg Wunsch 	 * adapted for struct bio instead of struct buf and specialized
2318fb35bd37SJoerg Wunsch 	 * for the current context.
2319f5f7ba03SJordan K. Hubbard 	 */
2320f664aeeeSJoerg Wunsch 	fdstrategy(bp);
2321f664aeeeSJoerg Wunsch 	while ((bp->bio_flags & BIO_DONE) == 0)
2322f664aeeeSJoerg Wunsch 		tsleep(bp, PRIBIO, "fdcmd", 0);
2323f664aeeeSJoerg Wunsch 
2324f664aeeeSJoerg Wunsch 	free(bp, M_TEMP);
2325f664aeeeSJoerg Wunsch 	return (bp->bio_flags & BIO_ERROR ? bp->bio_error : 0);
2326f664aeeeSJoerg Wunsch }
23275b81b6b3SRodney W. Grimes 
23283e425b96SJulian Elischer static int
23294c34deeeSJoerg Wunsch fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
2330f5f7ba03SJordan K. Hubbard {
2331f664aeeeSJoerg Wunsch  	fdu_t fdu;
2332f664aeeeSJoerg Wunsch  	fd_p fd;
2333f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2334fb35bd37SJoerg Wunsch 	struct disklabel *lp;
23352995d110SJoerg Wunsch 	struct fdc_status *fsp;
2336250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2337f664aeeeSJoerg Wunsch 	size_t fdblk;
2338fb35bd37SJoerg Wunsch 	int error;
2339f5f7ba03SJordan K. Hubbard 
2340f664aeeeSJoerg Wunsch  	fdu = FDUNIT(minor(dev));
2341f664aeeeSJoerg Wunsch  	fd = devclass_get_softc(fd_devclass, fdu);
2342fb35bd37SJoerg Wunsch 
2343fb35bd37SJoerg Wunsch 
23443a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2345fb35bd37SJoerg Wunsch 	error = 0;
2346f5f7ba03SJordan K. Hubbard 
23476182fdbdSPeter Wemm 	switch (cmd) {
2348f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2349fb35bd37SJoerg Wunsch 		lp = malloc(sizeof(*lp), M_TEMP, M_ZERO);
2350fb35bd37SJoerg Wunsch 		lp->d_secsize = fdblk;
23516182fdbdSPeter Wemm 		fdt = fd->ft;
2352fb35bd37SJoerg Wunsch 		lp->d_secpercyl = fdt->size / fdt->tracks;
2353fb35bd37SJoerg Wunsch 		lp->d_type = DTYPE_FLOPPY;
2354fb35bd37SJoerg Wunsch 		if (readdisklabel(dkmodpart(dev, RAW_PART), lp) != NULL)
2355f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2356fb35bd37SJoerg Wunsch 		else
2357fb35bd37SJoerg Wunsch 			*(struct disklabel *)addr = *lp;
2358fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2359f5f7ba03SJordan K. Hubbard 		break;
2360f5f7ba03SJordan K. Hubbard 
2361f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2362f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2363fb35bd37SJoerg Wunsch 			return (EBADF);
2364fb35bd37SJoerg Wunsch 		/*
2365fb35bd37SJoerg Wunsch 		 * XXX perhaps should call setdisklabel() to do error checking
2366fb35bd37SJoerg Wunsch 		 * although there is nowhere to "set" the result.  Perhaps
2367fb35bd37SJoerg Wunsch 		 * should always just fail.
2368fb35bd37SJoerg Wunsch 		 */
2369f5f7ba03SJordan K. Hubbard 		break;
2370f5f7ba03SJordan K. Hubbard 
2371f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2372f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2373fb35bd37SJoerg Wunsch 			return (EBADF);
2374f5f7ba03SJordan K. Hubbard 		break;
2375f5f7ba03SJordan K. Hubbard 
2376f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
2377fb35bd37SJoerg Wunsch 		if ((flag & FWRITE) == 0)
2378fb35bd37SJoerg Wunsch 			return (EBADF);
2379fb35bd37SJoerg Wunsch 		lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO);
2380fb35bd37SJoerg Wunsch 		error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0);
2381fb35bd37SJoerg Wunsch 		if (error != 0)
2382fb35bd37SJoerg Wunsch 			error = writedisklabel(dev, lp);
2383fb35bd37SJoerg Wunsch 		free(lp, M_TEMP);
2384b39c878eSAndrey A. Chernov 		break;
2385f664aeeeSJoerg Wunsch 
2386b39c878eSAndrey A. Chernov 	case FD_FORM:
2387b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2388fb35bd37SJoerg Wunsch 			return (EBADF);	/* must be opened for writing */
2389fb35bd37SJoerg Wunsch 		if (((struct fd_formb *)addr)->format_version !=
2390b39c878eSAndrey A. Chernov 		    FD_FORMAT_VERSION)
2391fb35bd37SJoerg Wunsch 			return (EINVAL); /* wrong version of formatting prog */
2392f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_FORMAT, addr);
2393b39c878eSAndrey A. Chernov 		break;
2394b39c878eSAndrey A. Chernov 
2395b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
23963e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2397f5f7ba03SJordan K. Hubbard 		break;
2398f5f7ba03SJordan K. Hubbard 
23993a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
24003a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2401f711d546SPoul-Henning Kamp 		if (suser(p) != 0)
2402fb35bd37SJoerg Wunsch 			return (EPERM);
24033e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
24043a2f7427SDavid Greenman 		break;
24053a2f7427SDavid Greenman 
24063a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
24073e425b96SJulian Elischer 		*(int *)addr = fd->options;
24083a2f7427SDavid Greenman 		break;
24093a2f7427SDavid Greenman 
24103a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
24113e425b96SJulian Elischer 		fd->options = *(int *)addr;
24123a2f7427SDavid Greenman 		break;
24133a2f7427SDavid Greenman 
2414f664aeeeSJoerg Wunsch #ifdef FDC_DEBUG
2415f664aeeeSJoerg Wunsch 	case FD_DEBUG:
2416f664aeeeSJoerg Wunsch 		fd_debug = *(int *)addr;
2417f664aeeeSJoerg Wunsch 		break;
2418f664aeeeSJoerg Wunsch #endif
2419f664aeeeSJoerg Wunsch 
24202995d110SJoerg Wunsch 	case FD_CLRERR:
24212995d110SJoerg Wunsch 		if (suser(p) != 0)
2422fb35bd37SJoerg Wunsch 			return (EPERM);
24232995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
24242995d110SJoerg Wunsch 		break;
24252995d110SJoerg Wunsch 
24262995d110SJoerg Wunsch 	case FD_GSTAT:
24272995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
24282995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
2429fb35bd37SJoerg Wunsch 			return (EINVAL);
24302995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
24312995d110SJoerg Wunsch 		break;
24322995d110SJoerg Wunsch 
2433250300ebSJoerg Wunsch 	case FD_READID:
2434250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2435250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2436fb35bd37SJoerg Wunsch 			return (EINVAL);
2437f664aeeeSJoerg Wunsch 		error = fdmisccmd(dev, BIO_RDSECTID, addr);
2438250300ebSJoerg Wunsch 		break;
2439250300ebSJoerg Wunsch 
2440f5f7ba03SJordan K. Hubbard 	default:
24413a2f7427SDavid Greenman 		error = ENOTTY;
2442f5f7ba03SJordan K. Hubbard 		break;
2443f5f7ba03SJordan K. Hubbard 	}
2444f5f7ba03SJordan K. Hubbard 	return (error);
2445f5f7ba03SJordan K. Hubbard }
2446