xref: /freebsd/sys/dev/fdc/fdc.c (revision 64860614a45dfe2bc8e235e1e29a417acb2a1ca2)
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 */
100dc5df763SJoerg Wunsch 
1016fb89845SKATO Takenori #define NUMTYPES 17
1026fb89845SKATO Takenori #define NUMDENS  (NUMTYPES - 7)
1037ca0641bSAndrey A. Chernov 
104b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
105b99f0a4aSAndrew Moore #define FD_1720         1
106b99f0a4aSAndrew Moore #define FD_1480         2
107b99f0a4aSAndrew Moore #define FD_1440         3
108b99f0a4aSAndrew Moore #define FD_1200         4
109b99f0a4aSAndrew Moore #define FD_820          5
110b99f0a4aSAndrew Moore #define FD_800          6
111b99f0a4aSAndrew Moore #define FD_720          7
112b99f0a4aSAndrew Moore #define FD_360          8
1136fb89845SKATO Takenori #define FD_640          9
1146fb89845SKATO Takenori #define FD_1232         10
115ed2fa05eSAndrey A. Chernov 
1166fb89845SKATO Takenori #define FD_1480in5_25   11
1176fb89845SKATO Takenori #define FD_1440in5_25   12
1186fb89845SKATO Takenori #define FD_820in5_25    13
1196fb89845SKATO Takenori #define FD_800in5_25    14
1206fb89845SKATO Takenori #define FD_720in5_25    15
1216fb89845SKATO Takenori #define FD_360in5_25    16
1226fb89845SKATO Takenori #define FD_640in5_25    17
123b99f0a4aSAndrew Moore 
124250300ebSJoerg Wunsch #define BIO_RDSECTID	BIO_FLAG1
1257ca0641bSAndrey A. Chernov 
1266f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1275b81b6b3SRodney W. Grimes {
128126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
129126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
130126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
131126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
132126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
133126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
134126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
135b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
1366fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /*  640K in DD 5.25in */
1376fb89845SKATO Takenori {  8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
138ed2fa05eSAndrey A. Chernov 
139126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
140126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
141126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
142126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
143126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
144126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1456fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /*  640K in HD 5.25in */
1465b81b6b3SRodney W. Grimes };
1475b81b6b3SRodney W. Grimes 
148b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
149dc16046fSJoerg Wunsch 
150f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
15164860614SJoerg Wunsch #define MAX_CYLINDER	85	/* some people really stress their drives
15264860614SJoerg Wunsch 				 * up to cyl 82 */
153250300ebSJoerg Wunsch #define MAX_HEAD	1
154f8ce7dd5SJoerg Wunsch 
1555b81b6b3SRodney W. Grimes /***********************************************************************\
1565b81b6b3SRodney W. Grimes * Per controller structure.						*
1575b81b6b3SRodney W. Grimes \***********************************************************************/
1586182fdbdSPeter Wemm static devclass_t fdc_devclass;
1595b81b6b3SRodney W. Grimes 
1605b81b6b3SRodney W. Grimes /***********************************************************************\
1615b81b6b3SRodney W. Grimes * Per drive structure.							*
162b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1635b81b6b3SRodney W. Grimes \***********************************************************************/
1646182fdbdSPeter Wemm struct fd_data {
165b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1665b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1673a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1685b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1695b81b6b3SRodney W. Grimes 	int	flags;
1705b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1715b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1725b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1735b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1745b81b6b3SRodney W. Grimes 	int	skip;
1755b81b6b3SRodney W. Grimes 	int	hddrv;
176dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1775b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1783a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
17902a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
18002a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
181b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
1826182fdbdSPeter Wemm 	device_t dev;
1836182fdbdSPeter Wemm 	fdu_t	fdu;
1846182fdbdSPeter Wemm };
18537286586SPeter Wemm 
18637286586SPeter Wemm struct fdc_ivars {
18737286586SPeter Wemm 	int	fdunit;
18837286586SPeter Wemm };
1896182fdbdSPeter Wemm static devclass_t fd_devclass;
1905b81b6b3SRodney W. Grimes 
1915b81b6b3SRodney W. Grimes /***********************************************************************\
1925b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1935b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1945b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1955b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1965b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1975b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1985b81b6b3SRodney W. Grimes \***********************************************************************/
199b99f0a4aSAndrew Moore 
2003a2f7427SDavid Greenman /* internal functions */
20164860614SJoerg Wunsch static driver_intr_t fdc_intr;
2026182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
2033a2f7427SDavid Greenman #  define TURNON 1
2043a2f7427SDavid Greenman #  define TURNOFF 0
2053a2f7427SDavid Greenman static timeout_t fd_turnoff;
2063a2f7427SDavid Greenman static timeout_t fd_motor_on;
2076182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
2083a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2096182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
21080909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
2116182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
2125c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2133a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2146182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
2156182fdbdSPeter Wemm static int retrier(struct fdc_data *);
2163a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
217250300ebSJoerg Wunsch static int fdreadid(dev_t, struct fdc_readid *);
2183a2f7427SDavid Greenman 
219d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
22080909a7dSJoerg Wunsch static void fd_clone (void *arg, char *name, int namelen, dev_t *dev);
221d66c374fSTor Egge 
222d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
223d66c374fSTor Egge 
224d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
225cba2a7c6SBruce Evans static char const * const fdstates[] =
2265b81b6b3SRodney W. Grimes {
2275b81b6b3SRodney W. Grimes "DEVIDLE",
2285b81b6b3SRodney W. Grimes "FINDWORK",
2295b81b6b3SRodney W. Grimes "DOSEEK",
2305b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2315b81b6b3SRodney W. Grimes "IOCOMPLETE",
2325b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2335b81b6b3SRodney W. Grimes "STARTRECAL",
2345b81b6b3SRodney W. Grimes "RESETCTLR",
2355b81b6b3SRodney W. Grimes "SEEKWAIT",
2365b81b6b3SRodney W. Grimes "RECALWAIT",
2375b81b6b3SRodney W. Grimes "MOTORWAIT",
2385c1a1eaeSBruce Evans "IOTIMEDOUT",
2395c1a1eaeSBruce Evans "RESETCOMPLETE",
240ff9607b0SBruce Evans "PIOREAD",
2415b81b6b3SRodney W. Grimes };
2425b81b6b3SRodney W. Grimes 
2433a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
244cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2455b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2465b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
247d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2485b81b6b3SRodney W. Grimes #define TRACE0(arg)
2495b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
250d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2515b81b6b3SRodney W. Grimes 
252427ccf00SDoug Rabson static void
253427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
254427ccf00SDoug Rabson {
255427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
256427ccf00SDoug Rabson }
257427ccf00SDoug Rabson 
258427ccf00SDoug Rabson static u_int8_t
259427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
260427ccf00SDoug Rabson {
261427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
262427ccf00SDoug Rabson }
263427ccf00SDoug Rabson 
264427ccf00SDoug Rabson static void
265427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
266427ccf00SDoug Rabson {
267427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
268427ccf00SDoug Rabson }
269427ccf00SDoug Rabson 
270427ccf00SDoug Rabson static u_int8_t
271427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
272427ccf00SDoug Rabson {
273427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
274427ccf00SDoug Rabson }
275427ccf00SDoug Rabson 
276427ccf00SDoug Rabson static void
2775f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
278427ccf00SDoug Rabson {
279427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
280427ccf00SDoug Rabson }
281427ccf00SDoug Rabson 
28258c9d623SPeter Wemm #if NCARD > 0
2835f830ea2SJoerg Wunsch static void
2845f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
2855f830ea2SJoerg Wunsch {
2865f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
2875f830ea2SJoerg Wunsch }
28858c9d623SPeter Wemm #endif
2895f830ea2SJoerg Wunsch 
290427ccf00SDoug Rabson #if 0
291427ccf00SDoug Rabson 
292427ccf00SDoug Rabson static u_int8_t
293427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
294427ccf00SDoug Rabson {
295427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
296427ccf00SDoug Rabson }
297427ccf00SDoug Rabson 
298427ccf00SDoug Rabson #endif
299427ccf00SDoug Rabson 
300f8ce7dd5SJoerg Wunsch /*
30164860614SJoerg Wunsch  * The open function is named Fdopen() to avoid confusion with fdopen()
30264860614SJoerg Wunsch  * in fd(4).  The difference is now only meaningful for debuggers.
303f8ce7dd5SJoerg Wunsch  */
304f8ce7dd5SJoerg Wunsch static	d_open_t	Fdopen;
30587f6c662SJulian Elischer static	d_close_t	fdclose;
30687f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
30787f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
30887f6c662SJulian Elischer 
30987f6c662SJulian Elischer #define CDEV_MAJOR 9
3104e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
3114e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
3124e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
3134e2f199eSPoul-Henning Kamp 	/* read */	physread,
3144e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
3154e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
3164e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
3174e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
3184e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
3194e2f199eSPoul-Henning Kamp 	/* name */	"fd",
3204e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
3214e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
3224e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
3234e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
3244e2f199eSPoul-Henning Kamp };
3254e2f199eSPoul-Henning Kamp 
326dc5df763SJoerg Wunsch static int
3276182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
328dc5df763SJoerg Wunsch {
3296182fdbdSPeter Wemm 	fdc->fdc_errs++;
33016b04b6aSJoerg Wunsch 	if (s) {
331b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
332b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
333b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
334b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
335b6e5f28eSPeter Wemm 						    "logging any more\n");
33616b04b6aSJoerg Wunsch 	}
337dc5df763SJoerg Wunsch 
338dc5df763SJoerg Wunsch 	return FD_FAILED;
339dc5df763SJoerg Wunsch }
340dc5df763SJoerg Wunsch 
341dc5df763SJoerg Wunsch /*
342dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
343dc5df763SJoerg Wunsch  * Unit number,
344dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
345dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
346dc5df763SJoerg Wunsch  */
3476f4e0bebSPoul-Henning Kamp static int
3486182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
349dc5df763SJoerg Wunsch {
350dc5df763SJoerg Wunsch 	u_char cmd;
351dc5df763SJoerg Wunsch 	int n_in;
352dc5df763SJoerg Wunsch 	int n;
353dc5df763SJoerg Wunsch 	va_list ap;
354dc5df763SJoerg Wunsch 
355dc5df763SJoerg Wunsch 	va_start(ap, n_out);
356dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
357dc5df763SJoerg Wunsch 	va_end(ap);
358dc5df763SJoerg Wunsch 	va_start(ap, n_out);
359dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
360dc5df763SJoerg Wunsch 	{
3616182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
362dc5df763SJoerg Wunsch 		{
363dc5df763SJoerg Wunsch 			char msg[50];
3642127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
365dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
366dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
3676182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
368dc5df763SJoerg Wunsch 		}
369dc5df763SJoerg Wunsch 	}
370dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
371dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
372dc5df763SJoerg Wunsch 	{
373dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
3746182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
375dc5df763SJoerg Wunsch 		{
376dc5df763SJoerg Wunsch 			char msg[50];
3772127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
378dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
379dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
3806182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
381dc5df763SJoerg Wunsch 		}
382dc5df763SJoerg Wunsch 	}
383dc5df763SJoerg Wunsch 
384dc5df763SJoerg Wunsch 	return 0;
385dc5df763SJoerg Wunsch }
386dc5df763SJoerg Wunsch 
3876f4e0bebSPoul-Henning Kamp static int
388d66c374fSTor Egge enable_fifo(fdc_p fdc)
389d66c374fSTor Egge {
390d66c374fSTor Egge 	int i, j;
391d66c374fSTor Egge 
392d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
393d66c374fSTor Egge 
394d66c374fSTor Egge 		/*
395d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
396d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
397d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
398d66c374fSTor Egge 		 */
399d66c374fSTor Egge 
4006182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
4016182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
402d66c374fSTor Egge 
403d66c374fSTor Egge 		/* If command is invalid, return */
404d66c374fSTor Egge 		j = 100000;
405427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
406d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
407d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
408d66c374fSTor Egge 				fdc_reset(fdc);
409d66c374fSTor Egge 				return FD_FAILED;
410d66c374fSTor Egge 			}
411d66c374fSTor Egge 		if (j<0 ||
4126182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
413d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
414d66c374fSTor Egge 			fdc_reset(fdc);
4156182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
416d66c374fSTor Egge 		}
417d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
418d66c374fSTor Egge 		return 0;
419d66c374fSTor Egge 	}
4206182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
421d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
4226182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
423d66c374fSTor Egge 	return 0;
424d66c374fSTor Egge }
425d66c374fSTor Egge 
426d66c374fSTor Egge static int
427dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
428dc5df763SJoerg Wunsch {
429dc5df763SJoerg Wunsch 	int st3;
430dc5df763SJoerg Wunsch 
4316182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
432dc5df763SJoerg Wunsch 	{
4336182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
434dc5df763SJoerg Wunsch 	}
435dc5df763SJoerg Wunsch 	if (st3p)
436dc5df763SJoerg Wunsch 		*st3p = st3;
437dc5df763SJoerg Wunsch 
438dc5df763SJoerg Wunsch 	return 0;
439dc5df763SJoerg Wunsch }
440dc5df763SJoerg Wunsch 
4416f4e0bebSPoul-Henning Kamp static int
442dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
443dc5df763SJoerg Wunsch {
4446182fdbdSPeter Wemm 	int cyl, st0, ret;
445dc5df763SJoerg Wunsch 
4466182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
4476182fdbdSPeter Wemm 	if (ret) {
4486182fdbdSPeter Wemm 		(void)fdc_err(fdc,
449dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
450dc5df763SJoerg Wunsch 		return ret;
451dc5df763SJoerg Wunsch 	}
452dc5df763SJoerg Wunsch 
453dc5df763SJoerg Wunsch 	if (st0p)
454dc5df763SJoerg Wunsch 		*st0p = st0;
455dc5df763SJoerg Wunsch 
4566182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
457dc5df763SJoerg Wunsch 		/*
458dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
459dc5df763SJoerg Wunsch 		 */
460dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
461dc5df763SJoerg Wunsch 	}
462dc5df763SJoerg Wunsch 
4636182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
4646182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
465dc5df763SJoerg Wunsch 	}
466dc5df763SJoerg Wunsch 
467dc5df763SJoerg Wunsch 	if (cylp)
468dc5df763SJoerg Wunsch 		*cylp = cyl;
469dc5df763SJoerg Wunsch 
470dc5df763SJoerg Wunsch 	return 0;
471dc5df763SJoerg Wunsch }
472dc5df763SJoerg Wunsch 
473dc5df763SJoerg Wunsch 
4746f4e0bebSPoul-Henning Kamp static int
47564860614SJoerg Wunsch fd_read_status(fdc_p fdc)
476dc5df763SJoerg Wunsch {
477dc5df763SJoerg Wunsch 	int i, ret;
478b5e8ce9fSBruce Evans 
4796182fdbdSPeter Wemm 	for (i = 0; i < 7; i++) {
480b5e8ce9fSBruce Evans 		/*
48164860614SJoerg Wunsch 		 * XXX types are poorly chosen.  Only bytes can be read
482a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
483b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
484b5e8ce9fSBruce Evans 		 */
485b5e8ce9fSBruce Evans 		int status;
486b5e8ce9fSBruce Evans 
4876182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
488b5e8ce9fSBruce Evans 		fdc->status[i] = status;
489b5e8ce9fSBruce Evans 		if (ret != 0)
490dc5df763SJoerg Wunsch 			break;
491dc5df763SJoerg Wunsch 	}
492dc5df763SJoerg Wunsch 
493dc5df763SJoerg Wunsch 	if (ret == 0)
494dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
495dc5df763SJoerg Wunsch 	else
496dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
497dc5df763SJoerg Wunsch 
498dc5df763SJoerg Wunsch 	return ret;
499dc5df763SJoerg Wunsch }
500dc5df763SJoerg Wunsch 
5015b81b6b3SRodney W. Grimes /****************************************************************************/
5025b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5035b81b6b3SRodney W. Grimes /****************************************************************************/
504dc5df763SJoerg Wunsch 
5053a2f7427SDavid Greenman static int
50637286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
5075b81b6b3SRodney W. Grimes {
50837286586SPeter Wemm 	device_t dev;
5095f830ea2SJoerg Wunsch 	int ispnp, ispcmcia;
5105b81b6b3SRodney W. Grimes 
51137286586SPeter Wemm 	dev = fdc->fdc_dev;
5125f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
5135f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
5146182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
5156182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
5166182fdbdSPeter Wemm 
517b6e5f28eSPeter Wemm 	/*
5185f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
5195f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
5205f830ea2SJoerg Wunsch 	 * register at 0x3f6.
5215f830ea2SJoerg Wunsch 	 *
522b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
5235f830ea2SJoerg Wunsch 	 *
5245f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
5255f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
5265f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
527b6e5f28eSPeter Wemm 	 */
5286182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
5296182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
5305f830ea2SJoerg Wunsch 					     ispcmcia ? 8 : (ispnp ? 1 : 6),
5315f830ea2SJoerg Wunsch 					     RF_ACTIVE);
5326182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
533427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve I/O port range\n");
53437286586SPeter Wemm 		return ENXIO;
5356182fdbdSPeter Wemm 	}
536427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
537427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
538427ccf00SDoug Rabson 
5395f830ea2SJoerg Wunsch 	if (!ispcmcia) {
540427ccf00SDoug Rabson 		/*
5415f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
5425f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
5435f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
5445f830ea2SJoerg Wunsch 		 * accordingly.
545b6e5f28eSPeter Wemm 		 */
546b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
547b6e5f28eSPeter Wemm 			fdc->port_off = -2;
548b6e5f28eSPeter Wemm 
549b6e5f28eSPeter Wemm 		/*
5505f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
5515f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
5525f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
553a2639a18SPeter Wemm 		 *
5545f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
5555f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
5565f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
5575f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
5585f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
5595f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
5605f830ea2SJoerg Wunsch 		 * address for the control register.
561427ccf00SDoug Rabson 		 */
562b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
563b9da888fSPeter Wemm 			u_long ctlstart;
564b9da888fSPeter Wemm 
565b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
5665f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
5675f830ea2SJoerg Wunsch 				fdc->port_off + 7;
568b6e5f28eSPeter Wemm 
569b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
570b9da888fSPeter Wemm 		}
571b6e5f28eSPeter Wemm 
572b6e5f28eSPeter Wemm 		/*
573b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
574b6e5f28eSPeter Wemm 		 */
575427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
5765f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
5775f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
578b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
579427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
5805f830ea2SJoerg Wunsch 			device_printf(dev,
5815f830ea2SJoerg Wunsch 				      "cannot reserve control I/O port range\n");
58237286586SPeter Wemm 			return ENXIO;
583427ccf00SDoug Rabson 		}
584427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
585427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
5865f830ea2SJoerg Wunsch 	}
5876182fdbdSPeter Wemm 
5886182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
5896182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
5906182fdbdSPeter Wemm 					  RF_ACTIVE);
5916182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
592427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
59337286586SPeter Wemm 		return ENXIO;
5946182fdbdSPeter Wemm 	}
5955f830ea2SJoerg Wunsch 
5965f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
5976182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
5986182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
5996182fdbdSPeter Wemm 						  RF_ACTIVE);
6006182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
601427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
60237286586SPeter Wemm 			return ENXIO;
6036182fdbdSPeter Wemm 		}
6046182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
6055f830ea2SJoerg Wunsch 	}
60637286586SPeter Wemm 
60737286586SPeter Wemm 	return 0;
60837286586SPeter Wemm }
60937286586SPeter Wemm 
61037286586SPeter Wemm static void
61137286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
61237286586SPeter Wemm {
61337286586SPeter Wemm 	device_t dev;
61437286586SPeter Wemm 
61537286586SPeter Wemm 	dev = fdc->fdc_dev;
61637286586SPeter Wemm 	if (fdc->res_irq != 0) {
61737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
61837286586SPeter Wemm 					fdc->res_irq);
61937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
62037286586SPeter Wemm 				     fdc->res_irq);
62137286586SPeter Wemm 	}
62237286586SPeter Wemm 	if (fdc->res_ctl != 0) {
62337286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
62437286586SPeter Wemm 					fdc->res_ctl);
62537286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
62637286586SPeter Wemm 				     fdc->res_ctl);
62737286586SPeter Wemm 	}
62837286586SPeter Wemm 	if (fdc->res_ioport != 0) {
62937286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
63037286586SPeter Wemm 					fdc->res_ioport);
63137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
63237286586SPeter Wemm 				     fdc->res_ioport);
63337286586SPeter Wemm 	}
63437286586SPeter Wemm 	if (fdc->res_drq != 0) {
63537286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
63637286586SPeter Wemm 					fdc->res_drq);
63737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
63837286586SPeter Wemm 				     fdc->res_drq);
63937286586SPeter Wemm 	}
64037286586SPeter Wemm }
64137286586SPeter Wemm 
64237286586SPeter Wemm /****************************************************************************/
64337286586SPeter Wemm /*                      autoconfiguration stuff                             */
64437286586SPeter Wemm /****************************************************************************/
64537286586SPeter Wemm 
64637286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
64737286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
64837286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
64937286586SPeter Wemm 	{0}
65037286586SPeter Wemm };
65137286586SPeter Wemm 
65237286586SPeter Wemm static int
6533aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
65437286586SPeter Wemm {
65537286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
65637286586SPeter Wemm 
65737286586SPeter Wemm 	switch (which) {
65837286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
65937286586SPeter Wemm 		*result = ivars->fdunit;
66037286586SPeter Wemm 		break;
66137286586SPeter Wemm 	default:
66237286586SPeter Wemm 		return ENOENT;
66337286586SPeter Wemm 	}
66437286586SPeter Wemm 	return 0;
66537286586SPeter Wemm }
66637286586SPeter Wemm 
66737286586SPeter Wemm /*
66837286586SPeter Wemm  * fdc controller section.
66937286586SPeter Wemm  */
67037286586SPeter Wemm static int
67137286586SPeter Wemm fdc_probe(device_t dev)
67237286586SPeter Wemm {
67337286586SPeter Wemm 	int	error, ic_type;
67437286586SPeter Wemm 	struct	fdc_data *fdc;
67537286586SPeter Wemm 
67637286586SPeter Wemm 	fdc = device_get_softc(dev);
67737286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
67837286586SPeter Wemm 	fdc->fdc_dev = dev;
6795f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
68037286586SPeter Wemm 
68137286586SPeter Wemm 	/* Check pnp ids */
68237286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
68337286586SPeter Wemm 	if (error == ENXIO)
68437286586SPeter Wemm 		return ENXIO;
6855f830ea2SJoerg Wunsch 	if (error == 0)
6865f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
68737286586SPeter Wemm 
68837286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
68937286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
69037286586SPeter Wemm 	if (error)
69137286586SPeter Wemm 		goto out;
6925b81b6b3SRodney W. Grimes 
69316111cedSAndrew Moore 	/* First - lets reset the floppy controller */
694427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
69516111cedSAndrew Moore 	DELAY(100);
696427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
69716111cedSAndrew Moore 
6985b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
6996182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7006182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
7016182fdbdSPeter Wemm 		error = ENXIO;
7026182fdbdSPeter Wemm 		goto out;
7035b81b6b3SRodney W. Grimes 	}
7046182fdbdSPeter Wemm 
7056182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
7066182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
7076182fdbdSPeter Wemm 		switch (ic_type) {
7086182fdbdSPeter Wemm 		case 0x80:
7096182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
7106182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
7116182fdbdSPeter Wemm 			break;
7126182fdbdSPeter Wemm 		case 0x81:
7136182fdbdSPeter Wemm 			device_set_desc(dev, "Intel 82077 or clone");
7146182fdbdSPeter Wemm 			fdc->fdct = FDC_I82077;
7156182fdbdSPeter Wemm 			break;
7166182fdbdSPeter Wemm 		case 0x90:
7176182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 72065B or clone");
7186182fdbdSPeter Wemm 			fdc->fdct = FDC_NE72065;
7196182fdbdSPeter Wemm 			break;
7206182fdbdSPeter Wemm 		default:
7216182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
7226182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
7236182fdbdSPeter Wemm 			break;
7246182fdbdSPeter Wemm 		}
7256182fdbdSPeter Wemm 	}
7266182fdbdSPeter Wemm 
7276182fdbdSPeter Wemm out:
72837286586SPeter Wemm 	fdc_release_resources(fdc);
7296182fdbdSPeter Wemm 	return (error);
7305b81b6b3SRodney W. Grimes }
7315b81b6b3SRodney W. Grimes 
7325f830ea2SJoerg Wunsch #if NCARD > 0
7335f830ea2SJoerg Wunsch 
7345f830ea2SJoerg Wunsch static int
7355f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
7365f830ea2SJoerg Wunsch {
7375f830ea2SJoerg Wunsch 	int	error;
7385f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7395f830ea2SJoerg Wunsch 
7405f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7415f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
7425f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
7435f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
7445f830ea2SJoerg Wunsch 
7455f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
7465f830ea2SJoerg Wunsch 
7475f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
7485f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
7495f830ea2SJoerg Wunsch 	if (error)
7505f830ea2SJoerg Wunsch 		goto out;
7515f830ea2SJoerg Wunsch 
7525f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
7535f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
7545f830ea2SJoerg Wunsch 	DELAY(100);
7555f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
7565f830ea2SJoerg Wunsch 
7575f830ea2SJoerg Wunsch 	/* see if it can handle a command */
7585f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7595f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
7605f830ea2SJoerg Wunsch 		error = ENXIO;
7615f830ea2SJoerg Wunsch 		goto out;
7625f830ea2SJoerg Wunsch 	}
7635f830ea2SJoerg Wunsch 
7645f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
7655f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
7665f830ea2SJoerg Wunsch 
7675f830ea2SJoerg Wunsch out:
7685f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7695f830ea2SJoerg Wunsch 	return (error);
7705f830ea2SJoerg Wunsch }
7715f830ea2SJoerg Wunsch 
7725f830ea2SJoerg Wunsch static int
7735f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev)
7745f830ea2SJoerg Wunsch {
7755f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7765f830ea2SJoerg Wunsch 	int	error;
7775f830ea2SJoerg Wunsch 
7785f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7795f830ea2SJoerg Wunsch 
7805f830ea2SJoerg Wunsch 	/* have our children detached first */
7815f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
7825f830ea2SJoerg Wunsch 		return (error);
7835f830ea2SJoerg Wunsch 
7845f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
7855f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
7865f830ea2SJoerg Wunsch 		return (0);
7875f830ea2SJoerg Wunsch 	}
7885f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
7895f830ea2SJoerg Wunsch 
7905f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
7915f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
7925f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7935f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
7945f830ea2SJoerg Wunsch 	return (0);
7955f830ea2SJoerg Wunsch }
7965f830ea2SJoerg Wunsch 
7975f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
7985f830ea2SJoerg Wunsch 
7995b81b6b3SRodney W. Grimes /*
80037286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
8015b81b6b3SRodney W. Grimes  */
8026182fdbdSPeter Wemm static void
80337286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
8045b81b6b3SRodney W. Grimes {
80537286586SPeter Wemm 	int	disabled;
80637286586SPeter Wemm 	struct fdc_ivars *ivar;
8076182fdbdSPeter Wemm 	device_t child;
80892200632SGarrett Wollman 
8097cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
81037286586SPeter Wemm 	if (ivar == NULL)
8116182fdbdSPeter Wemm 		return;
81237286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
81337286586SPeter Wemm 		ivar->fdunit = 0;
814fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
81537286586SPeter Wemm 	if (child == NULL)
8166182fdbdSPeter Wemm 		return;
81737286586SPeter Wemm 	device_set_ivars(child, ivar);
818a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
819a97c75b7SDoug Rabson 	    && disabled != 0)
8206182fdbdSPeter Wemm 		device_disable(child);
8216182fdbdSPeter Wemm }
8226182fdbdSPeter Wemm 
8236182fdbdSPeter Wemm static int
8246182fdbdSPeter Wemm fdc_attach(device_t dev)
8256182fdbdSPeter Wemm {
82637286586SPeter Wemm 	struct	fdc_data *fdc;
8272398f0cdSPeter Wemm 	int	i, error, dunit;
8282398f0cdSPeter Wemm 	const char *name, *dname;
829427ccf00SDoug Rabson 
83037286586SPeter Wemm 	fdc = device_get_softc(dev);
83137286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
83237286586SPeter Wemm 	if (error) {
833f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
83437286586SPeter Wemm 		return error;
83537286586SPeter Wemm 	}
83637286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
8373c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
8383c36743eSMark Murray 			       &fdc->fdc_intr);
83937286586SPeter Wemm 	if (error) {
84037286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
84137286586SPeter Wemm 		return error;
84237286586SPeter Wemm 	}
84337286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
8445b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
8456182fdbdSPeter Wemm 
8465f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
847f8ce7dd5SJoerg Wunsch 		/*
848f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
849f8ce7dd5SJoerg Wunsch 		 * the rest
850f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
851f8ce7dd5SJoerg Wunsch 		 */
852100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
853f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
8545f830ea2SJoerg Wunsch 	}
8555b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
8566182fdbdSPeter Wemm 
8573a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
85864860614SJoerg Wunsch 	fdout_wr(fdc, fdc->fdout = 0);
8598177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
8605b81b6b3SRodney W. Grimes 
8616182fdbdSPeter Wemm 	/*
86237286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
86337286586SPeter Wemm 	 * devices from the BIOS unless overridden.
8646182fdbdSPeter Wemm 	 */
865ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
8662398f0cdSPeter Wemm 	i = 0;
8672398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
8682398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
86937286586SPeter Wemm 
8706182fdbdSPeter Wemm 	return (bus_generic_attach(dev));
8716182fdbdSPeter Wemm }
8726182fdbdSPeter Wemm 
87315317dd8SMatthew N. Dodd static int
8746182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
8756182fdbdSPeter Wemm {
87615317dd8SMatthew N. Dodd 	int retval = 0;
87715317dd8SMatthew N. Dodd 
87815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
87915317dd8SMatthew N. Dodd 	retval += printf(" on %s drive %d\n", device_get_nameunit(me),
88037286586SPeter Wemm 	       fdc_get_fdunit(child));
88115317dd8SMatthew N. Dodd 
88215317dd8SMatthew N. Dodd 	return (retval);
8836182fdbdSPeter Wemm }
8846182fdbdSPeter Wemm 
88516e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
88616e68fc6SPeter Wemm 	/* Device interface */
88716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
88816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
88916e68fc6SPeter Wemm 	DEVMETHOD(device_detach,	bus_generic_detach),
89016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
89116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
89216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
89316e68fc6SPeter Wemm 
89416e68fc6SPeter Wemm 	/* Bus interface */
89516e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
89637286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
89716e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
89816e68fc6SPeter Wemm 
89916e68fc6SPeter Wemm 	{ 0, 0 }
90016e68fc6SPeter Wemm };
90116e68fc6SPeter Wemm 
90216e68fc6SPeter Wemm static driver_t fdc_driver = {
90316e68fc6SPeter Wemm 	"fdc",
90416e68fc6SPeter Wemm 	fdc_methods,
90516e68fc6SPeter Wemm 	sizeof(struct fdc_data)
90616e68fc6SPeter Wemm };
90716e68fc6SPeter Wemm 
90816e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
90916e68fc6SPeter Wemm 
9105f830ea2SJoerg Wunsch #if NCARD > 0
9115f830ea2SJoerg Wunsch 
9125f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
9135f830ea2SJoerg Wunsch 	/* Device interface */
9145f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
9155f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
9165f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_pccard_detach),
9175f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
9185f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
9195f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
9205f830ea2SJoerg Wunsch 
9215f830ea2SJoerg Wunsch 	/* Bus interface */
9225f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
9235f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
9245f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
9255f830ea2SJoerg Wunsch 
9265f830ea2SJoerg Wunsch 	{ 0, 0 }
9275f830ea2SJoerg Wunsch };
9285f830ea2SJoerg Wunsch 
9295f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
9305f830ea2SJoerg Wunsch 	"fdc",
9315f830ea2SJoerg Wunsch 	fdc_pccard_methods,
9325f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
9335f830ea2SJoerg Wunsch };
9345f830ea2SJoerg Wunsch 
9355f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
9365f830ea2SJoerg Wunsch 
9375f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
9385f830ea2SJoerg Wunsch 
9393f54a085SPoul-Henning Kamp static struct {
9403f54a085SPoul-Henning Kamp 	char *match;
9413f54a085SPoul-Henning Kamp 	int minor;
9423f54a085SPoul-Henning Kamp 	int link;
9433f54a085SPoul-Henning Kamp } fd_suffix[] = {
9443f54a085SPoul-Henning Kamp 	{ "a",		0,	1 },
9453f54a085SPoul-Henning Kamp 	{ "b",		0,	1 },
9463f54a085SPoul-Henning Kamp 	{ "c",		0,	1 },
9473f54a085SPoul-Henning Kamp 	{ "d",		0,	1 },
9483f54a085SPoul-Henning Kamp 	{ "e",		0,	1 },
9493f54a085SPoul-Henning Kamp 	{ "f",		0,	1 },
9503f54a085SPoul-Henning Kamp 	{ "g",		0,	1 },
9513f54a085SPoul-Henning Kamp 	{ "h",		0,	1 },
9523f54a085SPoul-Henning Kamp 	{ ".1720",	1,	0 },
9533f54a085SPoul-Henning Kamp 	{ ".1480",	2,	0 },
9543f54a085SPoul-Henning Kamp 	{ ".1440",	3,	0 },
9553f54a085SPoul-Henning Kamp 	{ ".1200",	4,	0 },
9563f54a085SPoul-Henning Kamp 	{ ".820",	5,	0 },
9573f54a085SPoul-Henning Kamp 	{ ".800",	6,	0 },
9583f54a085SPoul-Henning Kamp 	{ ".720",	7,	0 },
9593f54a085SPoul-Henning Kamp 	{ ".360",	8,	0 },
9603f54a085SPoul-Henning Kamp 	{ ".640",	9,	0 },
9613f54a085SPoul-Henning Kamp 	{ ".1232",	10,	0 },
9623f54a085SPoul-Henning Kamp 	{ 0, 0 }
9633f54a085SPoul-Henning Kamp };
9643f54a085SPoul-Henning Kamp static void
96580909a7dSJoerg Wunsch fd_clone (void *arg, char *name, int namelen, dev_t *dev)
9663f54a085SPoul-Henning Kamp {
9673f54a085SPoul-Henning Kamp 	int u, d, i;
9683f54a085SPoul-Henning Kamp 	char *n;
9693f54a085SPoul-Henning Kamp 	dev_t pdev;
9703f54a085SPoul-Henning Kamp 
9713f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
9723f54a085SPoul-Henning Kamp 		return;
973db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
9743f54a085SPoul-Henning Kamp 		return;
9753f54a085SPoul-Henning Kamp 	for (i = 0; ; i++) {
9763f54a085SPoul-Henning Kamp 		if (fd_suffix[i].match == NULL)
9773f54a085SPoul-Henning Kamp 			return;
9783f54a085SPoul-Henning Kamp 		if (strcmp(n, fd_suffix[i].match))
9793f54a085SPoul-Henning Kamp 			continue;
9803f54a085SPoul-Henning Kamp 		d = fd_suffix[i].minor;
9813f54a085SPoul-Henning Kamp 		break;
9823f54a085SPoul-Henning Kamp 	}
9833f54a085SPoul-Henning Kamp 	if (fd_suffix[i].link == 0) {
9843f54a085SPoul-Henning Kamp 		*dev = make_dev(&fd_cdevsw, (u << 6) + d,
9853f54a085SPoul-Henning Kamp 			UID_ROOT, GID_OPERATOR, 0640, name);
9863f54a085SPoul-Henning Kamp 	} else {
9873f54a085SPoul-Henning Kamp 		pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d);
9883f54a085SPoul-Henning Kamp 		*dev = make_dev_alias(pdev, name);
9893f54a085SPoul-Henning Kamp 	}
9903f54a085SPoul-Henning Kamp }
9913f54a085SPoul-Henning Kamp 
99216e68fc6SPeter Wemm /******************************************************************/
99316e68fc6SPeter Wemm /*
99416e68fc6SPeter Wemm  * devices attached to the controller section.
99516e68fc6SPeter Wemm  */
9966182fdbdSPeter Wemm static int
9976182fdbdSPeter Wemm fd_probe(device_t dev)
9986182fdbdSPeter Wemm {
9996182fdbdSPeter Wemm 	int	i;
10006182fdbdSPeter Wemm 	u_int	fdt, st0, st3;
10016182fdbdSPeter Wemm 	struct	fd_data *fd;
10026182fdbdSPeter Wemm 	struct	fdc_data *fdc;
10036182fdbdSPeter Wemm 	fdsu_t	fdsu;
10046182fdbdSPeter Wemm 	static int fd_fifo = 0;
10056182fdbdSPeter Wemm 
10066182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
10076182fdbdSPeter Wemm 	fd = device_get_softc(dev);
10086182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
10096182fdbdSPeter Wemm 
10106182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
10116182fdbdSPeter Wemm 	fd->dev = dev;
10126182fdbdSPeter Wemm 	fd->fdc = fdc;
10136182fdbdSPeter Wemm 	fd->fdsu = fdsu;
10146182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
10156182fdbdSPeter Wemm 
1016a97c75b7SDoug Rabson #ifdef __i386__
1017b99f0a4aSAndrew Moore 	/* look up what bios thinks we have */
10186182fdbdSPeter Wemm 	switch (fd->fdu) {
10196182fdbdSPeter Wemm 	case 0:
10205f830ea2SJoerg Wunsch 		if ((fdc->flags & FDC_ISPCMCIA))
10215f830ea2SJoerg Wunsch 			fdt = RTCFDT_144M;
10225f830ea2SJoerg Wunsch 		else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
10230722d6abSJoerg Wunsch 			fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
10240722d6abSJoerg Wunsch 		else
10250722d6abSJoerg Wunsch 			fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1026b99f0a4aSAndrew Moore 		break;
10276182fdbdSPeter Wemm 	case 1:
10286182fdbdSPeter Wemm 		fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1029dc5df763SJoerg Wunsch 		break;
1030dc5df763SJoerg Wunsch 	default:
10316182fdbdSPeter Wemm 		fdt = RTCFDT_NONE;
1032dc5df763SJoerg Wunsch 		break;
10336b7bd95bSJoerg Wunsch 	}
1034a97c75b7SDoug Rabson #else
1035a97c75b7SDoug Rabson 	fdt = RTCFDT_144M;	/* XXX probably */
1036a97c75b7SDoug Rabson #endif
10376182fdbdSPeter Wemm 
10386182fdbdSPeter Wemm 	/* is there a unit? */
10396182fdbdSPeter Wemm 	if (fdt == RTCFDT_NONE)
10406182fdbdSPeter Wemm 		return (ENXIO);
10416182fdbdSPeter Wemm 
10426182fdbdSPeter Wemm 	/* select it */
10436182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
10446182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
10456182fdbdSPeter Wemm 
10468de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
10476182fdbdSPeter Wemm 	if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1048e34c71eaSJoerg Wunsch 	    && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
10496182fdbdSPeter Wemm 	    && enable_fifo(fdc) == 0) {
1050b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1051b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1052d66c374fSTor Egge 	}
10536182fdbdSPeter Wemm 	fd_fifo = 1;
10546182fdbdSPeter Wemm 
10556182fdbdSPeter Wemm 	if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
10566182fdbdSPeter Wemm 	    && (st3 & NE7_ST3_T0)) {
1057dc5df763SJoerg Wunsch 		/* if at track 0, first seek inwards */
1058dc5df763SJoerg Wunsch 		/* seek some steps: */
10596182fdbdSPeter Wemm 		fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1060dc5df763SJoerg Wunsch 		DELAY(300000); /* ...wait a moment... */
10616182fdbdSPeter Wemm 		fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1062dc5df763SJoerg Wunsch 	}
1063dc5df763SJoerg Wunsch 
1064dc5df763SJoerg Wunsch 	/* If we're at track 0 first seek inwards. */
10656182fdbdSPeter Wemm 	if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1066dc5df763SJoerg Wunsch 		/* Seek some steps... */
10676182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1068dc5df763SJoerg Wunsch 			/* ...wait a moment... */
1069dc5df763SJoerg Wunsch 			DELAY(300000);
1070dc5df763SJoerg Wunsch 			/* make ctrlr happy: */
10716182fdbdSPeter Wemm 			fd_sense_int(fdc, 0, 0);
1072dc5df763SJoerg Wunsch 		}
1073dc5df763SJoerg Wunsch 	}
1074dc5df763SJoerg Wunsch 
10756b7bd95bSJoerg Wunsch 	for (i = 0; i < 2; i++) {
10766b7bd95bSJoerg Wunsch 		/*
10776b7bd95bSJoerg Wunsch 		 * we must recalibrate twice, just in case the
10786b7bd95bSJoerg Wunsch 		 * heads have been beyond cylinder 76, since most
10796b7bd95bSJoerg Wunsch 		 * FDCs still barf when attempting to recalibrate
10806b7bd95bSJoerg Wunsch 		 * more than 77 steps
10816b7bd95bSJoerg Wunsch 		 */
1082dc5df763SJoerg Wunsch 		/* go back to 0: */
10836182fdbdSPeter Wemm 		if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
10846b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek*/
10856b7bd95bSJoerg Wunsch 			DELAY(i == 0 ? 1000000 : 300000);
10865b81b6b3SRodney W. Grimes 
10876b7bd95bSJoerg Wunsch 			/* anything responding? */
1088dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, 0) == 0 &&
1089dc5df763SJoerg Wunsch 			    (st0 & NE7_ST0_EC) == 0)
10906b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
10916b7bd95bSJoerg Wunsch 		}
1092dc5df763SJoerg Wunsch 	}
10936b7bd95bSJoerg Wunsch 
10946182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
10953a2f7427SDavid Greenman 
10963a2f7427SDavid Greenman 	if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
10976182fdbdSPeter Wemm 		return (ENXIO);
10985b81b6b3SRodney W. Grimes 
1099dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1100b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1101b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
11023a2f7427SDavid Greenman 	fd->options = 0;
110302a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
110402a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
11055b81b6b3SRodney W. Grimes 
1106b99f0a4aSAndrew Moore 	switch (fdt) {
11077ca0641bSAndrey A. Chernov 	case RTCFDT_12M:
11086182fdbdSPeter Wemm 		device_set_desc(dev, "1200-KB 5.25\" drive");
1109b99f0a4aSAndrew Moore 		fd->type = FD_1200;
11107ca0641bSAndrey A. Chernov 		break;
11110722d6abSJoerg Wunsch 	case RTCFDT_144M | RTCFDT_144M_PRETENDED:
11126182fdbdSPeter Wemm 		device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
11130722d6abSJoerg Wunsch 		fdt = RTCFDT_144M;
11146182fdbdSPeter Wemm 		fd->type = FD_1440;
11157ca0641bSAndrey A. Chernov 	case RTCFDT_144M:
11166182fdbdSPeter Wemm 		device_set_desc(dev, "1440-KB 3.5\" drive");
1117b99f0a4aSAndrew Moore 		fd->type = FD_1440;
11187ca0641bSAndrey A. Chernov 		break;
1119290dd077SJoerg Wunsch 	case RTCFDT_288M:
112086a727d9SJoerg Wunsch 	case RTCFDT_288M_1:
11216182fdbdSPeter Wemm 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1122290dd077SJoerg Wunsch 		fd->type = FD_1440;
1123290dd077SJoerg Wunsch 		break;
11247ca0641bSAndrey A. Chernov 	case RTCFDT_360K:
11256182fdbdSPeter Wemm 		device_set_desc(dev, "360-KB 5.25\" drive");
1126b99f0a4aSAndrew Moore 		fd->type = FD_360;
11277ca0641bSAndrey A. Chernov 		break;
1128ed2fa05eSAndrey A. Chernov 	case RTCFDT_720K:
11296182fdbdSPeter Wemm 		printf("720-KB 3.5\" drive");
1130b99f0a4aSAndrew Moore 		fd->type = FD_720;
1131ed2fa05eSAndrey A. Chernov 		break;
11327ca0641bSAndrey A. Chernov 	default:
11336182fdbdSPeter Wemm 		return (ENXIO);
11345b81b6b3SRodney W. Grimes 	}
11356182fdbdSPeter Wemm 	return (0);
11366182fdbdSPeter Wemm }
11376182fdbdSPeter Wemm 
11386182fdbdSPeter Wemm static int
11396182fdbdSPeter Wemm fd_attach(device_t dev)
11406182fdbdSPeter Wemm {
11416182fdbdSPeter Wemm 	struct	fd_data *fd;
114264860614SJoerg Wunsch 	static	int cdevsw_add_done;
11436182fdbdSPeter Wemm 
114402211baeSNick Hibma 	if (!cdevsw_add_done) {
1145475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
114664860614SJoerg Wunsch 		cdevsw_add_done = 1;
114702211baeSNick Hibma 	}
1148db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000);
114964860614SJoerg Wunsch 	fd = device_get_softc(dev);
115064860614SJoerg Wunsch 	make_dev(&fd_cdevsw, fd->fdu << 6,
11513f54a085SPoul-Henning Kamp 	    UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
11526182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
1153f8ce7dd5SJoerg Wunsch 			  device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS,
11542a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
11552a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
11566182fdbdSPeter Wemm 	return (0);
11575b81b6b3SRodney W. Grimes }
11585b81b6b3SRodney W. Grimes 
11595f830ea2SJoerg Wunsch static int
11605f830ea2SJoerg Wunsch fd_detach(device_t dev)
11615f830ea2SJoerg Wunsch {
11625f830ea2SJoerg Wunsch 	struct	fd_data *fd;
11635f830ea2SJoerg Wunsch 
11645f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
11655f830ea2SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
11665f830ea2SJoerg Wunsch 
11675f830ea2SJoerg Wunsch 	return (0);
11685f830ea2SJoerg Wunsch }
11695f830ea2SJoerg Wunsch 
117016e68fc6SPeter Wemm static device_method_t fd_methods[] = {
117116e68fc6SPeter Wemm 	/* Device interface */
117216e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
117316e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
11745f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
117516e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
117616e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
117716e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
117816e68fc6SPeter Wemm 
117916e68fc6SPeter Wemm 	{ 0, 0 }
118016e68fc6SPeter Wemm };
118116e68fc6SPeter Wemm 
118216e68fc6SPeter Wemm static driver_t fd_driver = {
118316e68fc6SPeter Wemm 	"fd",
118416e68fc6SPeter Wemm 	fd_methods,
118516e68fc6SPeter Wemm 	sizeof(struct fd_data)
118616e68fc6SPeter Wemm };
118716e68fc6SPeter Wemm 
1188475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
118916e68fc6SPeter Wemm 
11905b81b6b3SRodney W. Grimes /****************************************************************************/
11915b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
11925e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
11935b81b6b3SRodney W. Grimes /****************************************************************************/
11943a2f7427SDavid Greenman static void
11956182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
11965b81b6b3SRodney W. Grimes {
11976182fdbdSPeter Wemm 	int fdout = fdc->fdout;
11983a2f7427SDavid Greenman 	int needspecify = 0;
11993a2f7427SDavid Greenman 
12003a2f7427SDavid Greenman 	if(turnon) {
12013a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
12023a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
12033a2f7427SDavid Greenman 	} else
12043a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
12053a2f7427SDavid Greenman 
12065e235068SJordan K. Hubbard 	if(!turnon
12075e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
12085e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
12095e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
12105e235068SJordan K. Hubbard 	else {
12113a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
12123a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
12133a2f7427SDavid Greenman 			needspecify = 1;
12143a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
12155b81b6b3SRodney W. Grimes 	}
12165b81b6b3SRodney W. Grimes 
1217427ccf00SDoug Rabson 	fdout_wr(fdc, fdout);
12186182fdbdSPeter Wemm 	fdc->fdout = fdout;
12193a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
12203a2f7427SDavid Greenman 
12213a2f7427SDavid Greenman 	if (needspecify) {
1222dc8603e3SJoerg Wunsch 		/*
1223f8ce7dd5SJoerg Wunsch 		 * we silently assume the command will be accepted
1224f8ce7dd5SJoerg Wunsch 		 * after an FDC reset
1225f8ce7dd5SJoerg Wunsch 		 *
1226f8ce7dd5SJoerg Wunsch 		 * Steinbach's Guideline for Systems Programming:
1227f8ce7dd5SJoerg Wunsch 		 * Never test for an error condition you don't know
1228f8ce7dd5SJoerg Wunsch 		 * how to handle.
1229dc8603e3SJoerg Wunsch 		 */
12306182fdbdSPeter Wemm 		(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1231dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1232dc5df763SJoerg Wunsch 			     0);
12336182fdbdSPeter Wemm 		if (fdc->flags & FDC_HAS_FIFO)
12346182fdbdSPeter Wemm 			(void) enable_fifo(fdc);
12353a2f7427SDavid Greenman 	}
12363a2f7427SDavid Greenman }
12373a2f7427SDavid Greenman 
1238381fe1aaSGarrett Wollman static void
12396182fdbdSPeter Wemm fd_turnoff(void *xfd)
12405b81b6b3SRodney W. Grimes {
1241f5f7ba03SJordan K. Hubbard 	int	s;
12426182fdbdSPeter Wemm 	fd_p fd = xfd;
1243dc16046fSJoerg Wunsch 
12446182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
12458335c1b8SBruce Evans 
12465f830ea2SJoerg Wunsch 	s = splbio();
12478335c1b8SBruce Evans 	/*
12488335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
12495f830ea2SJoerg Wunsch 	 *
12505f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
12515f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
12525f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
12535f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
12548335c1b8SBruce Evans 	 */
12556182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
12565f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
12575f830ea2SJoerg Wunsch 		splx(s);
12588335c1b8SBruce Evans 		return;
12598335c1b8SBruce Evans 	}
12608335c1b8SBruce Evans 
12615b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
12626182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1263f5f7ba03SJordan K. Hubbard 	splx(s);
12645b81b6b3SRodney W. Grimes }
12655b81b6b3SRodney W. Grimes 
12663a2f7427SDavid Greenman static void
12676182fdbdSPeter Wemm fd_motor_on(void *xfd)
12685b81b6b3SRodney W. Grimes {
1269f5f7ba03SJordan K. Hubbard 	int	s;
12706182fdbdSPeter Wemm 	fd_p fd = xfd;
1271f5f7ba03SJordan K. Hubbard 
1272f5f7ba03SJordan K. Hubbard 	s = splbio();
12735b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
12745b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
12755b81b6b3SRodney W. Grimes 	{
12766182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
12775b81b6b3SRodney W. Grimes 	}
1278f5f7ba03SJordan K. Hubbard 	splx(s);
12795b81b6b3SRodney W. Grimes }
12805b81b6b3SRodney W. Grimes 
12813a2f7427SDavid Greenman static void
12826182fdbdSPeter Wemm fd_turnon(fd_p fd)
12835b81b6b3SRodney W. Grimes {
12845b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
12855b81b6b3SRodney W. Grimes 	{
12863a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
12876182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
12886182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
12895b81b6b3SRodney W. Grimes 	}
12905b81b6b3SRodney W. Grimes }
12915b81b6b3SRodney W. Grimes 
1292381fe1aaSGarrett Wollman static void
1293dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
12945b81b6b3SRodney W. Grimes {
12953a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1296427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
12973a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
12983a2f7427SDavid Greenman 	DELAY(100);
12993a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1300427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
13013a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
13023a2f7427SDavid Greenman 	DELAY(100);
1303427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
13043a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
13053a2f7427SDavid Greenman 
130664860614SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
13076182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1308dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1309dc5df763SJoerg Wunsch 		     0);
1310d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1311d66c374fSTor Egge 		(void) enable_fifo(fdc);
13125b81b6b3SRodney W. Grimes }
13135b81b6b3SRodney W. Grimes 
13145b81b6b3SRodney W. Grimes /****************************************************************************/
13155b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
13165b81b6b3SRodney W. Grimes /****************************************************************************/
1317b5e8ce9fSBruce Evans static int
13186182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1319dc5df763SJoerg Wunsch {
1320dc5df763SJoerg Wunsch 	int i, j = 100000;
1321427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1322dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1323dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13246182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1325dc5df763SJoerg Wunsch 	if (j <= 0)
13266182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1327d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1328427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1329dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1330dc5df763SJoerg Wunsch 	*ptr = i;
1331dc5df763SJoerg Wunsch 	return 0;
1332d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1333427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1334dc5df763SJoerg Wunsch 	if (ptr)
1335dc5df763SJoerg Wunsch 		*ptr = i;
1336dc5df763SJoerg Wunsch 	return 0;
1337d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1338dc5df763SJoerg Wunsch }
1339dc5df763SJoerg Wunsch 
1340dc5df763SJoerg Wunsch int
13416182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
13425b81b6b3SRodney W. Grimes {
13433b3837dbSRodney W. Grimes 	int i;
13445b81b6b3SRodney W. Grimes 
13453b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
13463b3837dbSRodney W. Grimes 	i = 100000;
1347427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
13486182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
13493b3837dbSRodney W. Grimes 
13503b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
13513b3837dbSRodney W. Grimes 	i = 100000;
1352427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
135316b04b6aSJoerg Wunsch 	if (i <= 0)
13546182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
13553b3837dbSRodney W. Grimes 
13563b3837dbSRodney W. Grimes 	/* Send the command and return */
1357427ccf00SDoug Rabson 	fddata_wr(fdc, x);
13583a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
13595b81b6b3SRodney W. Grimes 	return (0);
13605b81b6b3SRodney W. Grimes }
13615b81b6b3SRodney W. Grimes 
13625b81b6b3SRodney W. Grimes /****************************************************************************/
13635b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
13645b81b6b3SRodney W. Grimes /****************************************************************************/
1365381fe1aaSGarrett Wollman int
1366671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
13675b81b6b3SRodney W. Grimes {
13685b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
136920a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
13706182fdbdSPeter Wemm 	fd_p	fd;
1371b99f0a4aSAndrew Moore 	fdc_p	fdc;
13725b81b6b3SRodney W. Grimes 
13735b81b6b3SRodney W. Grimes 	/* check bounds */
13746182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1375b99f0a4aSAndrew Moore 		return (ENXIO);
13766182fdbdSPeter Wemm 	fdc = fd->fdc;
13776182fdbdSPeter Wemm 	if ((fdc == NULL) || (fd->type == NO_TYPE))
1378b99f0a4aSAndrew Moore 		return (ENXIO);
1379b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1380b99f0a4aSAndrew Moore 		return (ENXIO);
13817ca0641bSAndrey A. Chernov 	if (type == 0)
13826182fdbdSPeter Wemm 		type = fd->type;
13837ca0641bSAndrey A. Chernov 	else {
13843e425b96SJulian Elischer 		/*
13853e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
13863e425b96SJulian Elischer 		 * to open a type it can do,
13873e425b96SJulian Elischer 		 */
13886182fdbdSPeter Wemm 		if (type != fd->type) {
13896182fdbdSPeter Wemm 			switch (fd->type) {
13907ca0641bSAndrey A. Chernov 			case FD_360:
13917ca0641bSAndrey A. Chernov 				return (ENXIO);
1392ed2fa05eSAndrey A. Chernov 			case FD_720:
1393b39c878eSAndrey A. Chernov 				if (   type != FD_820
1394b39c878eSAndrey A. Chernov 				    && type != FD_800
13956fb89845SKATO Takenori 				    && type != FD_640
1396ed2fa05eSAndrey A. Chernov 				   )
1397ed2fa05eSAndrey A. Chernov 					return (ENXIO);
1398ed2fa05eSAndrey A. Chernov 				break;
13997ca0641bSAndrey A. Chernov 			case FD_1200:
1400b39c878eSAndrey A. Chernov 				switch (type) {
1401b39c878eSAndrey A. Chernov 				case FD_1480:
1402b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1403fa4700b4SAndrey A. Chernov 					break;
14047ca0641bSAndrey A. Chernov 				case FD_1440:
1405b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1406b39c878eSAndrey A. Chernov 					break;
14076fb89845SKATO Takenori 				case FD_1232:
14086fb89845SKATO Takenori 					break;
1409b39c878eSAndrey A. Chernov 				case FD_820:
1410b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1411b39c878eSAndrey A. Chernov 					break;
1412b39c878eSAndrey A. Chernov 				case FD_800:
1413b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1414b39c878eSAndrey A. Chernov 					break;
1415b39c878eSAndrey A. Chernov 				case FD_720:
1416b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1417b39c878eSAndrey A. Chernov 					break;
14186fb89845SKATO Takenori 				case FD_640:
14196fb89845SKATO Takenori 					type = FD_640in5_25;
14206fb89845SKATO Takenori 					break;
1421b39c878eSAndrey A. Chernov 				case FD_360:
1422b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1423b39c878eSAndrey A. Chernov 					break;
1424b39c878eSAndrey A. Chernov 				default:
1425b39c878eSAndrey A. Chernov 					return(ENXIO);
1426b39c878eSAndrey A. Chernov 				}
1427b39c878eSAndrey A. Chernov 				break;
1428b39c878eSAndrey A. Chernov 			case FD_1440:
1429b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1430b39c878eSAndrey A. Chernov 				    && type != FD_1480
1431ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1432b39c878eSAndrey A. Chernov 				    && type != FD_820
1433b39c878eSAndrey A. Chernov 				    && type != FD_800
1434b39c878eSAndrey A. Chernov 				    && type != FD_720
14356fb89845SKATO Takenori 				    && type != FD_640
14367ca0641bSAndrey A. Chernov 				    )
1437dffff499SAndrey A. Chernov 					return(ENXIO);
1438fa4700b4SAndrey A. Chernov 				break;
14397ca0641bSAndrey A. Chernov 			}
14407ca0641bSAndrey A. Chernov 		}
1441fa4700b4SAndrey A. Chernov 	}
14426182fdbdSPeter Wemm 	fd->ft = fd_types + type - 1;
14436182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
14445f830ea2SJoerg Wunsch 
14455b81b6b3SRodney W. Grimes 	return 0;
14465b81b6b3SRodney W. Grimes }
14475b81b6b3SRodney W. Grimes 
1448381fe1aaSGarrett Wollman int
1449671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
14505b81b6b3SRodney W. Grimes {
14515b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
14526182fdbdSPeter Wemm 	struct fd_data *fd;
1453b99f0a4aSAndrew Moore 
14546182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
14556182fdbdSPeter Wemm 	fd->flags &= ~FD_OPEN;
14562995d110SJoerg Wunsch 	fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1457dc16046fSJoerg Wunsch 
14585b81b6b3SRodney W. Grimes 	return (0);
14595b81b6b3SRodney W. Grimes }
14605b81b6b3SRodney W. Grimes 
14613a2f7427SDavid Greenman /****************************************************************************/
14623a2f7427SDavid Greenman /*                               fdstrategy                                 */
14633a2f7427SDavid Greenman /****************************************************************************/
14643a2f7427SDavid Greenman void
14658177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
14663a2f7427SDavid Greenman {
1467bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
14683a2f7427SDavid Greenman  	int	s;
14693a2f7427SDavid Greenman  	fdu_t	fdu;
14703a2f7427SDavid Greenman  	fdc_p	fdc;
14713a2f7427SDavid Greenman  	fd_p	fd;
14723a2f7427SDavid Greenman 	size_t	fdblk;
14733a2f7427SDavid Greenman 
14748177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
14756182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
14766182fdbdSPeter Wemm 	if (fd == 0)
14776182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
14788177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
14793a2f7427SDavid Greenman 	fdc = fd->fdc;
148069acd21dSWarner Losh 	if (fd->type == NO_TYPE) {
14818177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
14828177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
14833b178206SWarner Losh 		goto bad;
148464860614SJoerg Wunsch 	}
1485d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
148664860614SJoerg Wunsch 	if (bp->bio_cmd != BIO_FORMAT && (bp->bio_flags & BIO_RDSECTID) == 0) {
14878177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1488dc5df763SJoerg Wunsch 			printf(
14896a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
14908177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
14918177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
14928177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
14933a2f7427SDavid Greenman 			goto bad;
14943a2f7427SDavid Greenman 		}
14958177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
14968177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
14978177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
14983a2f7427SDavid Greenman 			goto bad;
14993a2f7427SDavid Greenman 		}
15003a2f7427SDavid Greenman 	}
15013a2f7427SDavid Greenman 
15023a2f7427SDavid Greenman 	/*
15033a2f7427SDavid Greenman 	 * Set up block calculations.
15043a2f7427SDavid Greenman 	 */
15058177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1506bb6382faSJoerg Wunsch 		/*
1507bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1508bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1509bb6382faSJoerg Wunsch 		 */
15108177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
15118177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15123a2f7427SDavid Greenman 		goto bad;
15133a2f7427SDavid Greenman 	}
15148177437dSPoul-Henning Kamp 	blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk;
1515bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
15168177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
15178177437dSPoul-Henning Kamp 	if (blknum + (bp->bio_bcount / fdblk) > nblocks) {
1518bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1519bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
15208177437dSPoul-Henning Kamp 			bp->bio_resid = bp->bio_bcount - cando;
1521bb6382faSJoerg Wunsch 			if (cando == 0)
1522bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1523bb6382faSJoerg Wunsch 		} else {
15248177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15258177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
1526bb6382faSJoerg Wunsch 			goto bad;
1527bb6382faSJoerg Wunsch 		}
1528bb6382faSJoerg Wunsch 	}
15298177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
15303a2f7427SDavid Greenman 	s = splbio();
15318177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
15326182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1533b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
15345f830ea2SJoerg Wunsch 	device_busy(fd->dev);
15356182fdbdSPeter Wemm 	fdstart(fdc);
15363a2f7427SDavid Greenman 	splx(s);
15373a2f7427SDavid Greenman 	return;
15383a2f7427SDavid Greenman 
15393a2f7427SDavid Greenman bad:
15403a2f7427SDavid Greenman 	biodone(bp);
15413a2f7427SDavid Greenman }
15423a2f7427SDavid Greenman 
15435b81b6b3SRodney W. Grimes /***************************************************************\
15445b81b6b3SRodney W. Grimes *				fdstart				*
15455b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
15465b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
15475b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
15485b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
15495b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
15505b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
15515b81b6b3SRodney W. Grimes \***************************************************************/
1552381fe1aaSGarrett Wollman static void
15536182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
15545b81b6b3SRodney W. Grimes {
15555b81b6b3SRodney W. Grimes 	int s;
15565b81b6b3SRodney W. Grimes 
15575b81b6b3SRodney W. Grimes 	s = splbio();
15586182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
15595b81b6b3SRodney W. Grimes 	{
15606182fdbdSPeter Wemm 		fdc_intr(fdc);
15615b81b6b3SRodney W. Grimes 	}
15625b81b6b3SRodney W. Grimes 	splx(s);
15635b81b6b3SRodney W. Grimes }
15645b81b6b3SRodney W. Grimes 
1565381fe1aaSGarrett Wollman static void
15666182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
15675b81b6b3SRodney W. Grimes {
15685c1a1eaeSBruce Evans  	fdc_p fdc;
1569f5f7ba03SJordan K. Hubbard 	int s;
15705b81b6b3SRodney W. Grimes 
15716182fdbdSPeter Wemm 	fdc = xfdc;
15725c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
15735b81b6b3SRodney W. Grimes 
15743a2f7427SDavid Greenman 	/*
15753a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
15763a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
15773a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
15783a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
15793a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
15805c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
15815c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
15825c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
15835c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
15843a2f7427SDavid Greenman 	 */
15853a2f7427SDavid Greenman 	s = splbio();
15865c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
15875c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
15885c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
15896182fdbdSPeter Wemm 	fdc_intr(fdc);
1590f5f7ba03SJordan K. Hubbard 	splx(s);
15915b81b6b3SRodney W. Grimes }
15925b81b6b3SRodney W. Grimes 
15935b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1594381fe1aaSGarrett Wollman static void
15956182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
15965b81b6b3SRodney W. Grimes {
15975b81b6b3SRodney W. Grimes 	int	s;
15983a2f7427SDavid Greenman 
15995b81b6b3SRodney W. Grimes 	s = splbio();
16006182fdbdSPeter Wemm 	fdc_intr(xfdc);
16015b81b6b3SRodney W. Grimes 	splx(s);
16025b81b6b3SRodney W. Grimes }
16035b81b6b3SRodney W. Grimes 
16045b81b6b3SRodney W. Grimes /***********************************************************************\
160564860614SJoerg Wunsch *                                 fdc_intr				*
16065b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
16075b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
16085b81b6b3SRodney W. Grimes \***********************************************************************/
1609fe310de8SBruce Evans static void
16106182fdbdSPeter Wemm fdc_intr(void *xfdc)
16115b81b6b3SRodney W. Grimes {
16126182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
16136182fdbdSPeter Wemm 	while(fdstate(fdc))
1614381fe1aaSGarrett Wollman 		;
16155b81b6b3SRodney W. Grimes }
16165b81b6b3SRodney W. Grimes 
161769acd21dSWarner Losh /*
161869acd21dSWarner Losh  * magic pseudo-DMA initialization for YE FDC. Sets count and
161969acd21dSWarner Losh  * direction
162069acd21dSWarner Losh  */
16213b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
16223b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
16233b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
16243b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
16253b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
162669acd21dSWarner Losh 
162769acd21dSWarner Losh /*
162869acd21dSWarner Losh  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
162969acd21dSWarner Losh  */
16304c34deeeSJoerg Wunsch static int
16314c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
163269acd21dSWarner Losh {
163369acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
163469acd21dSWarner Losh 
163521144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
163669acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
163769acd21dSWarner Losh 			fdc->state = PIOREAD;
163869acd21dSWarner Losh 			return(0);
163964860614SJoerg Wunsch 		}
16403b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
16413b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16423b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
164369acd21dSWarner Losh 	} else {
16443b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16453b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
16463b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
164764860614SJoerg Wunsch 	}
164869acd21dSWarner Losh 	return(1);
164969acd21dSWarner Losh }
165069acd21dSWarner Losh 
16515b81b6b3SRodney W. Grimes /***********************************************************************\
16525b81b6b3SRodney W. Grimes * The controller state machine.						*
16535b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
16545b81b6b3SRodney W. Grimes \***********************************************************************/
16553a2f7427SDavid Greenman static int
16566182fdbdSPeter Wemm fdstate(fdc_p fdc)
16575b81b6b3SRodney W. Grimes {
1658250300ebSJoerg Wunsch 	int read, format, rdsectid, head, i, sec = 0, sectrac, st0, cyl, st3, idf;
1659bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
166064860614SJoerg Wunsch 	struct fdc_readid *idp;
16615b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
16625b81b6b3SRodney W. Grimes 	fd_p fd;
16638177437dSPoul-Henning Kamp 	register struct bio *bp;
1664b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
16653a2f7427SDavid Greenman 	size_t fdblk;
16665b81b6b3SRodney W. Grimes 
1667e93e63cbSBruce Evans 	bp = fdc->bp;
1668e93e63cbSBruce Evans 	if (bp == NULL) {
16698177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1670e93e63cbSBruce Evans 		if (bp != NULL) {
16718177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1672e93e63cbSBruce Evans 			fdc->bp = bp;
1673e93e63cbSBruce Evans 		}
1674e93e63cbSBruce Evans 	}
1675e93e63cbSBruce Evans 	if (bp == NULL) {
16765b81b6b3SRodney W. Grimes 		/***********************************************\
16775b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
16785b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
16795b81b6b3SRodney W. Grimes 		\***********************************************/
16805b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
16816182fdbdSPeter Wemm 		if (fdc->fd) {
1682b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1683b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
16845b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
16855b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
16865b81b6b3SRodney W. Grimes 		}
16876182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
16885b81b6b3SRodney W. Grimes  		return (0);
16895b81b6b3SRodney W. Grimes 	}
16908177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
16916182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16923a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1693b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1694b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
16958177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
169656a23089SPoul-Henning Kamp 	if (read)
169756a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
169856a23089SPoul-Henning Kamp 	else
169956a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
170064860614SJoerg Wunsch 	format = bp->bio_cmd == BIO_FORMAT;
1701250300ebSJoerg Wunsch 	rdsectid = bp->bio_flags & BIO_RDSECTID;
1702bb6382faSJoerg Wunsch 	if (format) {
17038177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
1704bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1705bb6382faSJoerg Wunsch 			- (char *)finfo;
1706bb6382faSJoerg Wunsch 	}
1707bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
17088177437dSPoul-Henning Kamp 		blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk +
1709bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1710bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1711bb6382faSJoerg Wunsch 	}
17125b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
17135b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
17145b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
17156182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
17166182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
17175b81b6b3SRodney W. Grimes 	switch (fdc->state)
17185b81b6b3SRodney W. Grimes 	{
17195b81b6b3SRodney W. Grimes 	case DEVIDLE:
17205b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
17215b81b6b3SRodney W. Grimes 		fdc->retry = 0;
17225b81b6b3SRodney W. Grimes 		fd->skip = 0;
17235b81b6b3SRodney W. Grimes 		fdc->fd = fd;
17245b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
17255f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
17263a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
17275b81b6b3SRodney W. Grimes 		/*******************************************************\
17285b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1729dc733423SDag-Erling Smørgrav 		* it will start up in its own good time			*
17305b81b6b3SRodney W. Grimes 		\*******************************************************/
17316182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
17325b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17335b81b6b3SRodney W. Grimes 			return (0); /* come back later */
17345b81b6b3SRodney W. Grimes 		}
17355b81b6b3SRodney W. Grimes 		/*******************************************************\
17365b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
17375b81b6b3SRodney W. Grimes 		\*******************************************************/
17385b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
17395b81b6b3SRodney W. Grimes 		{
17405b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17416182fdbdSPeter Wemm 			fd_turnon(fd);
17425b81b6b3SRodney W. Grimes 			return (0);
17435b81b6b3SRodney W. Grimes 		}
17445b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
17455b81b6b3SRodney W. Grimes 		{
17466182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
17475b81b6b3SRodney W. Grimes 		}
17485c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17495c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17505c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17515c1a1eaeSBruce Evans 		} else
17525e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
17535b81b6b3SRodney W. Grimes 		break;
17545b81b6b3SRodney W. Grimes 	case DOSEEK:
1755bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
17565b81b6b3SRodney W. Grimes 		{
17575b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
17585b81b6b3SRodney W. Grimes 			break;
17595b81b6b3SRodney W. Grimes 		}
17606182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1761bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1762dc5df763SJoerg Wunsch 			   0))
1763dc8603e3SJoerg Wunsch 		{
1764dc8603e3SJoerg Wunsch 			/*
1765dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1766dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1767dc8603e3SJoerg Wunsch 			 */
1768dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
17696182fdbdSPeter Wemm 			return(retrier(fdc));
1770dc8603e3SJoerg Wunsch 		}
1771dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
17725b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
17735b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17745b81b6b3SRodney W. Grimes 	case SEEKWAIT:
17755b81b6b3SRodney W. Grimes 		/* allow heads to settle */
17766182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
17775b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17785b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17795b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
17805b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
17816182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
1782bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
17833a2f7427SDavid Greenman 			do {
17843a2f7427SDavid Greenman 				/*
1785dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1786dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1787dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1788dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1789dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1790dc5df763SJoerg Wunsch 				 *
1791dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1792dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1793dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1794dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1795dc5df763SJoerg Wunsch 				 * real interrupt condition.
1796dc5df763SJoerg Wunsch 				 *
1797dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1798dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1799dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1800dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1801dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1802dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1803dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
18043a2f7427SDavid Greenman 				 */
1805dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1806dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1807dc5df763SJoerg Wunsch 					return 0;
1808dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1809dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1810dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
18113a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1812dc5df763SJoerg Wunsch 
18136182fdbdSPeter Wemm 			if (0 == descyl) {
1814dc5df763SJoerg Wunsch 				int failed = 0;
18153a2f7427SDavid Greenman 				/*
18163a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
18173a2f7427SDavid Greenman 				 * really there
18183a2f7427SDavid Greenman 				 */
1819dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1820dc5df763SJoerg Wunsch 					failed = 1;
18213a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
18223a2f7427SDavid Greenman 					printf(
18233a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
18243a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1825dc5df763SJoerg Wunsch 					failed = 1;
1826dc5df763SJoerg Wunsch 				}
1827dc5df763SJoerg Wunsch 
18286182fdbdSPeter Wemm 				if (failed) {
18293a2f7427SDavid Greenman 					if(fdc->retry < 3)
18303a2f7427SDavid Greenman 						fdc->retry = 3;
18316182fdbdSPeter Wemm 					return (retrier(fdc));
18323a2f7427SDavid Greenman 				}
18333a2f7427SDavid Greenman 			}
1834dc5df763SJoerg Wunsch 
18356182fdbdSPeter Wemm 			if (cyl != descyl) {
18363a2f7427SDavid Greenman 				printf(
18373a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
18382d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1839e5d7d243SBruce Evans 				if (fdc->retry < 3)
1840e5d7d243SBruce Evans 					fdc->retry = 3;
18416182fdbdSPeter Wemm 				return (retrier(fdc));
18425b81b6b3SRodney W. Grimes 			}
18435b81b6b3SRodney W. Grimes 		}
18445b81b6b3SRodney W. Grimes 
1845bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1846250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
18478177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
18488177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
18495b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
18505b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
18515b81b6b3SRodney W. Grimes 		head = sec / sectrac;
18525b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
18533a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
18543a2f7427SDavid Greenman 
1855250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
18563a2f7427SDavid Greenman 		{
18573a2f7427SDavid Greenman 			/* make sure the drive is writable */
1858dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1859dc8603e3SJoerg Wunsch 			{
1860dc8603e3SJoerg Wunsch 				/* stuck controller? */
18615f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
186256a23089SPoul-Henning Kamp 					isa_dmadone(idf,
18638177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
18648177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
18655c1a1eaeSBruce Evans 						    fdc->dmachan);
1866dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
18676182fdbdSPeter Wemm 				return (retrier(fdc));
1868dc8603e3SJoerg Wunsch 			}
18693a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
18703a2f7427SDavid Greenman 			{
18713a2f7427SDavid Greenman 				/*
18723a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
18733a2f7427SDavid Greenman 				 * in order to force the current operation
18743a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
18753a2f7427SDavid Greenman 				 * error - all error handling is done
18763a2f7427SDavid Greenman 				 * by the retrier()
18773a2f7427SDavid Greenman 				 */
18783a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
18793a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
18803a2f7427SDavid Greenman 				fdc->status[2] = 0;
18813a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
18823a2f7427SDavid Greenman 				fdc->status[4] = head;
18833a2f7427SDavid Greenman 				fdc->status[5] = sec;
18843a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
18853a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
18863a2f7427SDavid Greenman 				return (1);
18873a2f7427SDavid Greenman 			}
18883a2f7427SDavid Greenman 		}
18895b81b6b3SRodney W. Grimes 
18906182fdbdSPeter Wemm 		if (format) {
18915f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
18925f830ea2SJoerg Wunsch 				/*
18935f830ea2SJoerg Wunsch 				 * This seems to be necessary for
18945f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
18955f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
18965f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
18975f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
18985f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
18995f830ea2SJoerg Wunsch 				 * the place when trying to read them
19005f830ea2SJoerg Wunsch 				 * back.
19015f830ea2SJoerg Wunsch 				 *
19025f830ea2SJoerg Wunsch 				 * Umpf.
19035f830ea2SJoerg Wunsch 				 */
19048177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
19055f830ea2SJoerg Wunsch 
19068177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
19078177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
19088177437dSPoul-Henning Kamp 					bp->bio_bcount);
19095f830ea2SJoerg Wunsch 
19105f830ea2SJoerg Wunsch 			}
1911b39c878eSAndrey A. Chernov 			/* formatting */
19126182fdbdSPeter Wemm 			if(fd_cmd(fdc, 6,  NE7CMD_FORMAT, head << 2 | fdu,
1913dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1914dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1915dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
19166182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
1917dc8603e3SJoerg Wunsch 				/* controller fell over */
19185f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
191956a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19208177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19218177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19225c1a1eaeSBruce Evans 						    fdc->dmachan);
1923dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19246182fdbdSPeter Wemm 				return (retrier(fdc));
1925dc8603e3SJoerg Wunsch 			}
1926250300ebSJoerg Wunsch 		} else if (rdsectid) {
1927250300ebSJoerg Wunsch 			if (fd_cmd(fdc, 2, NE7CMD_READID, head << 2 | fdu, 0)) {
1928250300ebSJoerg Wunsch 				/* controller jamming */
1929250300ebSJoerg Wunsch 				fdc->retry = 6;
1930250300ebSJoerg Wunsch 				return (retrier(fdc));
1931250300ebSJoerg Wunsch 			}
19326182fdbdSPeter Wemm 		} else {
1933250300ebSJoerg Wunsch 			/* read or write operation */
19343b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
193569acd21dSWarner Losh 				/*
193669acd21dSWarner Losh 				 * this seems to be necessary even when
193769acd21dSWarner Losh 				 * reading data
193869acd21dSWarner Losh 				 */
19393b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
194069acd21dSWarner Losh 
194169acd21dSWarner Losh 				/*
194269acd21dSWarner Losh 				 * perform the write pseudo-DMA before
194369acd21dSWarner Losh 				 * the WRITE command is sent
194469acd21dSWarner Losh 				 */
194569acd21dSWarner Losh 				if (!read)
19468177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
19478177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
194869acd21dSWarner Losh 					    fdblk);
194969acd21dSWarner Losh 			}
19506182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
1951dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1952dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1953dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1954dc5df763SJoerg Wunsch 				   head,
1955dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1956dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1957dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1958dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1959dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
19606182fdbdSPeter Wemm 				   0)) {
1961dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
19625f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
196356a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19648177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19658177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19665c1a1eaeSBruce Evans 						    fdc->dmachan);
1967dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19686182fdbdSPeter Wemm 				return (retrier(fdc));
19695b81b6b3SRodney W. Grimes 			}
1970b39c878eSAndrey A. Chernov 		}
1971250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
197269acd21dSWarner Losh 			/*
197369acd21dSWarner Losh 			 * if this is a read, then simply await interrupt
197469acd21dSWarner Losh 			 * before performing PIO
197569acd21dSWarner Losh 			 */
19768177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
19778177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
19783b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
197969acd21dSWarner Losh 				return(0);      /* will return later */
198064860614SJoerg Wunsch 			}
198169acd21dSWarner Losh 
198269acd21dSWarner Losh 		/*
198369acd21dSWarner Losh 		 * write (or format) operation will fall through and
198469acd21dSWarner Losh 		 * await completion interrupt
198569acd21dSWarner Losh 		 */
19865b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
19876182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
19885b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
198969acd21dSWarner Losh 	case PIOREAD:
199069acd21dSWarner Losh 		/*
199169acd21dSWarner Losh 		 * actually perform the PIO read.  The IOCOMPLETE case
199269acd21dSWarner Losh 		 * removes the timeout for us.
199369acd21dSWarner Losh 		 */
19948177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
199569acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
199669acd21dSWarner Losh 		/* FALLTHROUGH */
19975b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
19986182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
1999dc5df763SJoerg Wunsch 
200064860614SJoerg Wunsch 		if (fd_read_status(fdc)) {
2001250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
20028177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
20038177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
20045c1a1eaeSBruce Evans 					    fdc->dmachan);
2005dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2006dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
20076182fdbdSPeter Wemm 			return (retrier(fdc));
20085b81b6b3SRodney W. Grimes   		}
2009dc5df763SJoerg Wunsch 
20103a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2011dc5df763SJoerg Wunsch 
20123a2f7427SDavid Greenman 		/* FALLTHROUGH */
2013dc5df763SJoerg Wunsch 
20143a2f7427SDavid Greenman 	case IOTIMEDOUT:
2015250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
20168177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
20178177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
20186182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
20193a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20203a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2021b39c878eSAndrey A. Chernov                                 /*
20223a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
20233a2f7427SDavid Greenman 				 * and didn't release it in time for the
20243a2f7427SDavid Greenman 				 * next FDC transfer.
20253a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
20263a2f7427SDavid Greenman 				 * count. (vak)
2027b39c878eSAndrey A. Chernov                                  */
2028b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
2029b39c878eSAndrey A. Chernov                                 return (1);
2030b39c878eSAndrey A. Chernov                         }
20313a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
20323a2f7427SDavid Greenman 				&& fdc->retry < 6)
20333a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
20343a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20353a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
20363a2f7427SDavid Greenman 				&& fdc->retry < 3)
20373a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
20386182fdbdSPeter Wemm 			return (retrier(fdc));
20395b81b6b3SRodney W. Grimes 		}
20405b81b6b3SRodney W. Grimes 		/* All OK */
2041250300ebSJoerg Wunsch 		if (rdsectid) {
2042250300ebSJoerg Wunsch 			/* copy out ID field contents */
204364860614SJoerg Wunsch 			idp = (struct fdc_readid *)bp->bio_data;
2044250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2045250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2046250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2047250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2048250300ebSJoerg Wunsch 		}
20493a2f7427SDavid Greenman 		fd->skip += fdblk;
2050250300ebSJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount - bp->bio_resid) {
20515b81b6b3SRodney W. Grimes 			/* set up next transfer */
20525b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
20536182fdbdSPeter Wemm 		} else {
20545b81b6b3SRodney W. Grimes 			/* ALL DONE */
20555b81b6b3SRodney W. Grimes 			fd->skip = 0;
2056e93e63cbSBruce Evans 			fdc->bp = NULL;
20575f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2058a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
20595b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
20605b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
20615b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
20625b81b6b3SRodney W. Grimes 		}
20635b81b6b3SRodney W. Grimes 		return (1);
20645b81b6b3SRodney W. Grimes 	case RESETCTLR:
20653a2f7427SDavid Greenman 		fdc_reset(fdc);
20665b81b6b3SRodney W. Grimes 		fdc->retry++;
20675c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
20685c1a1eaeSBruce Evans 		return (0);
20695c1a1eaeSBruce Evans 	case RESETCOMPLETE:
20705c1a1eaeSBruce Evans 		/*
20715c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
20725c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
20735c1a1eaeSBruce Evans 		 */
20740e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
20750e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
20765c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
20775c1a1eaeSBruce Evans 		/* Fall through. */
20785c1a1eaeSBruce Evans 	case STARTRECAL:
20796182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2080dc8603e3SJoerg Wunsch 			/* arrgl */
2081dc8603e3SJoerg Wunsch 			fdc->retry = 6;
20826182fdbdSPeter Wemm 			return (retrier(fdc));
2083dc8603e3SJoerg Wunsch 		}
20845b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
20855b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20865b81b6b3SRodney W. Grimes 	case RECALWAIT:
20875b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20886182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
20895b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
20905b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20915b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
20923a2f7427SDavid Greenman 		do {
20933a2f7427SDavid Greenman 			/*
2094dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
20953a2f7427SDavid Greenman 			 */
2096dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2097dc5df763SJoerg Wunsch 				return 0;
2098dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2099dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2100dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
21013a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
21023a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
21035b81b6b3SRodney W. Grimes 		{
2104dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2105dc8603e3SJoerg Wunsch 				/*
2106dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
2107dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2108dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2109dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2110dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2111dc8603e3SJoerg Wunsch 				 * time in a line
2112dc8603e3SJoerg Wunsch 				 */
2113dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2114dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
21153a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
21166182fdbdSPeter Wemm 			return (retrier(fdc));
21175b81b6b3SRodney W. Grimes 		}
21185b81b6b3SRodney W. Grimes 		fd->track = 0;
21195b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
21205b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
21215b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21225b81b6b3SRodney W. Grimes 	case MOTORWAIT:
21235b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
21245b81b6b3SRodney W. Grimes 		{
21255b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
21265b81b6b3SRodney W. Grimes 		}
21275c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
21285c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
21295c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
21305c1a1eaeSBruce Evans 		} else {
21315e235068SJordan K. Hubbard 			/*
21325c1a1eaeSBruce Evans 			 * If all motors were off, then the controller was
21335c1a1eaeSBruce Evans 			 * reset, so it has lost track of the current
21345c1a1eaeSBruce Evans 			 * cylinder.  Recalibrate to handle this case.
2135f86e4077SBruce Evans 			 * But first, discard the results of the reset.
21365e235068SJordan K. Hubbard 			 */
2137f86e4077SBruce Evans 			fdc->state = RESETCOMPLETE;
21385c1a1eaeSBruce Evans 		}
21395b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21405b81b6b3SRodney W. Grimes 	default:
2141b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
214264860614SJoerg Wunsch 		if (fd_read_status(fdc) == 0)
2143a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
21445b81b6b3SRodney W. Grimes 			       fdc->status[0],
21455b81b6b3SRodney W. Grimes 			       fdc->status[1],
21465b81b6b3SRodney W. Grimes 			       fdc->status[2],
21475b81b6b3SRodney W. Grimes 			       fdc->status[3],
21485b81b6b3SRodney W. Grimes 			       fdc->status[4],
21495b81b6b3SRodney W. Grimes 			       fdc->status[5],
21505b81b6b3SRodney W. Grimes 			       fdc->status[6] );
21513a2f7427SDavid Greenman 		else
2152dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2153dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2154dac0f2dbSJoerg Wunsch 		{
2155dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
21565b81b6b3SRodney W. Grimes 			return (0);
21575b81b6b3SRodney W. Grimes 		}
2158dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2159dac0f2dbSJoerg Wunsch 		return (0);
2160dac0f2dbSJoerg Wunsch 	}
2161dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
21625b81b6b3SRodney W. Grimes 	return (1); /* Come back immediatly to new state */
21635b81b6b3SRodney W. Grimes }
21645b81b6b3SRodney W. Grimes 
2165aaf08d94SGarrett Wollman static int
21666182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
21675b81b6b3SRodney W. Grimes {
21688177437dSPoul-Henning Kamp 	struct bio *bp;
21696182fdbdSPeter Wemm 	struct fd_data *fd;
21706182fdbdSPeter Wemm 	int fdu;
21715b81b6b3SRodney W. Grimes 
2172e93e63cbSBruce Evans 	bp = fdc->bp;
21735b81b6b3SRodney W. Grimes 
21746182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
21758177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
21766182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
21776182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
21783a2f7427SDavid Greenman 		goto fail;
21796182fdbdSPeter Wemm 
21806182fdbdSPeter Wemm 	switch (fdc->retry) {
21815b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
21825b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
21835b81b6b3SRodney W. Grimes 		break;
21845b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
21855b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
21865b81b6b3SRodney W. Grimes 		break;
21875b81b6b3SRodney W. Grimes 	case 6:
21885b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
21895b81b6b3SRodney W. Grimes 		break;
21905b81b6b3SRodney W. Grimes 	case 7:
21915b81b6b3SRodney W. Grimes 		break;
21925b81b6b3SRodney W. Grimes 	default:
21933a2f7427SDavid Greenman 	fail:
21945b81b6b3SRodney W. Grimes 		{
21952995d110SJoerg Wunsch 			int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
21962995d110SJoerg Wunsch 
21972995d110SJoerg Wunsch 			if (printerror)
219864860614SJoerg Wunsch 				diskerr(bp, "hard error",
219964860614SJoerg Wunsch 				    fdc->fd->skip / DEV_BSIZE,
22003a2f7427SDavid Greenman 				    (struct disklabel *)NULL);
22012995d110SJoerg Wunsch 			if (printerror) {
2202dc5df763SJoerg Wunsch 				if (fdc->flags & FDC_STAT_VALID)
2203dc5df763SJoerg Wunsch 				{
2204dc5df763SJoerg Wunsch 					printf(
2205a838d83dSBruce Evans 				" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2206dc5df763SJoerg Wunsch 					       fdc->status[0], NE7_ST0BITS,
2207dc5df763SJoerg Wunsch 					       fdc->status[1], NE7_ST1BITS,
2208dc5df763SJoerg Wunsch 					       fdc->status[2], NE7_ST2BITS,
2209dc5df763SJoerg Wunsch 					       fdc->status[3], fdc->status[4],
2210dc5df763SJoerg Wunsch 					       fdc->status[5]);
2211dc5df763SJoerg Wunsch 				}
2212dc5df763SJoerg Wunsch 				else
2213dc5df763SJoerg Wunsch 					printf(" (No status)\n");
22145b81b6b3SRodney W. Grimes 			}
22152995d110SJoerg Wunsch 		}
22162995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
22178177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
22188177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
22198177437dSPoul-Henning Kamp 			bp->bio_resid += bp->bio_bcount - fdc->fd->skip;
22202995d110SJoerg Wunsch 		}
2221e93e63cbSBruce Evans 		fdc->bp = NULL;
22225b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
22235f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2224a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
222592ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
22265c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
22275b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
22285b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
222992ed385aSRodney W. Grimes 		return (1);
22305b81b6b3SRodney W. Grimes 	}
22315b81b6b3SRodney W. Grimes 	fdc->retry++;
22325b81b6b3SRodney W. Grimes 	return (1);
22335b81b6b3SRodney W. Grimes }
22345b81b6b3SRodney W. Grimes 
22351fdb6e6cSPoul-Henning Kamp static void
22361fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
22371fdb6e6cSPoul-Henning Kamp {
22381fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
22391fdb6e6cSPoul-Henning Kamp }
22401fdb6e6cSPoul-Henning Kamp 
2241b39c878eSAndrey A. Chernov static int
22424c34deeeSJoerg Wunsch fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
2243b39c878eSAndrey A. Chernov {
2244b39c878eSAndrey A. Chernov  	fdu_t	fdu;
2245b39c878eSAndrey A. Chernov  	fd_p	fd;
2246b39c878eSAndrey A. Chernov 
22471fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2248b39c878eSAndrey A. Chernov 	int rv = 0, s;
22493a2f7427SDavid Greenman 	size_t fdblk;
2250b39c878eSAndrey A. Chernov 
2251b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
22526182fdbdSPeter Wemm 	fd	= devclass_get_softc(fd_devclass, fdu);
22533a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2254b39c878eSAndrey A. Chernov 
2255b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
22561fdb6e6cSPoul-Henning Kamp 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2257b39c878eSAndrey A. Chernov 	if(bp == 0)
22581fdb6e6cSPoul-Henning Kamp 		return ENOMEM;
225982f5379bSJoerg Wunsch 	/*
226082f5379bSJoerg Wunsch 	 * keep the process from being swapped
226182f5379bSJoerg Wunsch 	 */
22622ada239cSPeter Wemm 	PHOLD(p);
22631fdb6e6cSPoul-Henning Kamp 	bzero((void *)bp, sizeof(*bp));
22641fdb6e6cSPoul-Henning Kamp 	bp->bio_cmd = BIO_FORMAT;
2265b39c878eSAndrey A. Chernov 
2266b39c878eSAndrey A. Chernov 	/*
2267b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2268b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2269b39c878eSAndrey A. Chernov 	 */
22701fdb6e6cSPoul-Henning Kamp 	bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
22713a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2272b39c878eSAndrey A. Chernov 
22731fdb6e6cSPoul-Henning Kamp 	bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
22741fdb6e6cSPoul-Henning Kamp 	bp->bio_data = (caddr_t)finfo;
2275b39c878eSAndrey A. Chernov 
2276b39c878eSAndrey A. Chernov 	/* now do the format */
22771fdb6e6cSPoul-Henning Kamp 	bp->bio_dev = dev;
22781fdb6e6cSPoul-Henning Kamp 	bp->bio_done = fdbiodone;
22791fdb6e6cSPoul-Henning Kamp 	fdstrategy(bp);
2280b39c878eSAndrey A. Chernov 
2281b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2282b39c878eSAndrey A. Chernov 	s = splbio();
22831fdb6e6cSPoul-Henning Kamp 	while(!(bp->bio_flags & BIO_DONE)) {
22845e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2285b39c878eSAndrey A. Chernov 		if (rv == EWOULDBLOCK)
2286b39c878eSAndrey A. Chernov 			break;
2287b39c878eSAndrey A. Chernov 	}
2288b39c878eSAndrey A. Chernov 	splx(s);
2289b39c878eSAndrey A. Chernov 
229082f5379bSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2291b39c878eSAndrey A. Chernov 		/* timed out */
2292b39c878eSAndrey A. Chernov 		rv = EIO;
22935f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
229482f5379bSJoerg Wunsch 	}
22951fdb6e6cSPoul-Henning Kamp 	if (bp->bio_flags & BIO_ERROR)
22961fdb6e6cSPoul-Henning Kamp 		rv = bp->bio_error;
229782f5379bSJoerg Wunsch 	/*
229882f5379bSJoerg Wunsch 	 * allow the process to be swapped
229982f5379bSJoerg Wunsch 	 */
23002ada239cSPeter Wemm 	PRELE(p);
2301b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2302b39c878eSAndrey A. Chernov 	return rv;
2303b39c878eSAndrey A. Chernov }
2304b39c878eSAndrey A. Chernov 
2305250300ebSJoerg Wunsch static int
2306250300ebSJoerg Wunsch fdreadid(dev_t dev, struct fdc_readid *idfield)
2307250300ebSJoerg Wunsch {
2308250300ebSJoerg Wunsch  	fdu_t	fdu;
2309250300ebSJoerg Wunsch  	fd_p	fd;
2310250300ebSJoerg Wunsch 
2311250300ebSJoerg Wunsch 	struct bio *bp;
2312250300ebSJoerg Wunsch 	int rv = 0, s;
2313250300ebSJoerg Wunsch 	size_t fdblk;
2314250300ebSJoerg Wunsch 
2315250300ebSJoerg Wunsch  	fdu	= FDUNIT(minor(dev));
2316250300ebSJoerg Wunsch 	fd	= devclass_get_softc(fd_devclass, fdu);
2317250300ebSJoerg Wunsch 	fdblk = 128 << fd->ft->secsize;
2318250300ebSJoerg Wunsch 
2319250300ebSJoerg Wunsch 	/* set up a buffer header for fdstrategy() */
2320250300ebSJoerg Wunsch 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2321250300ebSJoerg Wunsch 	if(bp == 0)
2322250300ebSJoerg Wunsch 		return ENOMEM;
2323250300ebSJoerg Wunsch 
2324250300ebSJoerg Wunsch 	bzero((void *)bp, sizeof(*bp));
2325250300ebSJoerg Wunsch 	bp->bio_cmd = BIO_READ;
2326250300ebSJoerg Wunsch 	bp->bio_flags |= BIO_RDSECTID;
2327250300ebSJoerg Wunsch 
2328250300ebSJoerg Wunsch 	/*
2329250300ebSJoerg Wunsch 	 * calculate a fake blkno, so fdstrategy() would initiate a
2330250300ebSJoerg Wunsch 	 * seek to the requested cylinder
2331250300ebSJoerg Wunsch 	 */
2332250300ebSJoerg Wunsch 	bp->bio_blkno = (idfield->cyl * (fd->ft->sectrac * fd->ft->heads)
2333250300ebSJoerg Wunsch 		+ idfield->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2334250300ebSJoerg Wunsch 	bp->bio_bcount = sizeof(struct fdc_readid);
2335250300ebSJoerg Wunsch 	bp->bio_data = (caddr_t)idfield;
2336250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2337250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2338250300ebSJoerg Wunsch 	fdstrategy(bp);
2339250300ebSJoerg Wunsch 
2340250300ebSJoerg Wunsch 	/* ...and wait for it to complete */
2341250300ebSJoerg Wunsch 	s = splbio();
2342250300ebSJoerg Wunsch 	while(!(bp->bio_flags & BIO_DONE)) {
2343250300ebSJoerg Wunsch 		rv = tsleep((caddr_t)bp, PRIBIO, "fdrdid", 10 * hz);
2344250300ebSJoerg Wunsch 		if (rv == EWOULDBLOCK)
2345250300ebSJoerg Wunsch 			break;
2346250300ebSJoerg Wunsch 	}
2347250300ebSJoerg Wunsch 	splx(s);
2348250300ebSJoerg Wunsch 
2349250300ebSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2350250300ebSJoerg Wunsch 		/* timed out */
2351250300ebSJoerg Wunsch 		rv = EIO;
2352250300ebSJoerg Wunsch 		device_unbusy(fd->dev);
2353250300ebSJoerg Wunsch 	}
2354250300ebSJoerg Wunsch 	if (bp->bio_flags & BIO_ERROR)
2355250300ebSJoerg Wunsch 		rv = bp->bio_error;
2356250300ebSJoerg Wunsch 	free(bp, M_TEMP);
2357250300ebSJoerg Wunsch 	return rv;
2358250300ebSJoerg Wunsch }
2359250300ebSJoerg Wunsch 
2360f5f7ba03SJordan K. Hubbard /*
2361671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2362f5f7ba03SJordan K. Hubbard  */
23635b81b6b3SRodney W. Grimes 
23643e425b96SJulian Elischer static int
23654c34deeeSJoerg Wunsch fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
2366f5f7ba03SJordan K. Hubbard {
23673a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
23686182fdbdSPeter Wemm  	fd_p	fd = devclass_get_softc(fd_devclass, fdu);
23693a2f7427SDavid Greenman 	size_t fdblk;
23703a2f7427SDavid Greenman 
2371f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2372f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
23732995d110SJoerg Wunsch 	struct fdc_status *fsp;
2374250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2375f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
23763a2f7427SDavid Greenman 	int error = 0;
2377f5f7ba03SJordan K. Hubbard 
23783a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2379f5f7ba03SJordan K. Hubbard 
23806182fdbdSPeter Wemm 	switch (cmd) {
2381f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2382f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2383f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
23843a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
23856182fdbdSPeter Wemm 		fdt = fd->ft;
2386f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2387f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2388f5f7ba03SJordan K. Hubbard 
238949ff4debSPoul-Henning Kamp 		if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2390191e1a59SBruce Evans 		    == NULL)
2391f5f7ba03SJordan K. Hubbard 			error = 0;
2392f5f7ba03SJordan K. Hubbard 		else
2393f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2394f5f7ba03SJordan K. Hubbard 
2395f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2396f5f7ba03SJordan K. Hubbard 		break;
2397f5f7ba03SJordan K. Hubbard 
2398f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2399f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2400f5f7ba03SJordan K. Hubbard 			error = EBADF;
2401f5f7ba03SJordan K. Hubbard 		break;
2402f5f7ba03SJordan K. Hubbard 
2403f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2404f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2405f5f7ba03SJordan K. Hubbard 			error = EBADF;
2406f5f7ba03SJordan K. Hubbard 		break;
2407f5f7ba03SJordan K. Hubbard 
2408f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
24096182fdbdSPeter Wemm 		if ((flag & FWRITE) == 0) {
2410f5f7ba03SJordan K. Hubbard 			error = EBADF;
2411f5f7ba03SJordan K. Hubbard 			break;
2412f5f7ba03SJordan K. Hubbard 		}
2413f5f7ba03SJordan K. Hubbard 
2414f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2415f5f7ba03SJordan K. Hubbard 
2416191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2417191e1a59SBruce Evans 					  (u_long)0)) != 0)
2418f5f7ba03SJordan K. Hubbard 			break;
2419f5f7ba03SJordan K. Hubbard 
242049ff4debSPoul-Henning Kamp 		error = writedisklabel(dev, (struct disklabel *)buffer);
2421b39c878eSAndrey A. Chernov 		break;
2422b39c878eSAndrey A. Chernov 	case FD_FORM:
2423b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2424b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2425b39c878eSAndrey A. Chernov 		else if (((struct fd_formb *)addr)->format_version !=
2426b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2427b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2428b39c878eSAndrey A. Chernov 		else
2429b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
2430b39c878eSAndrey A. Chernov 		break;
2431b39c878eSAndrey A. Chernov 
2432b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
24333e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2434f5f7ba03SJordan K. Hubbard 		break;
2435f5f7ba03SJordan K. Hubbard 
24363a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
24373a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2438f711d546SPoul-Henning Kamp 		if (suser(p) != 0)
24393a2f7427SDavid Greenman 			return EPERM;
24403e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
24413a2f7427SDavid Greenman 		break;
24423a2f7427SDavid Greenman 
24433a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
24443e425b96SJulian Elischer 		*(int *)addr = fd->options;
24453a2f7427SDavid Greenman 		break;
24463a2f7427SDavid Greenman 
24473a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
24483e425b96SJulian Elischer 		fd->options = *(int *)addr;
24493a2f7427SDavid Greenman 		break;
24503a2f7427SDavid Greenman 
24512995d110SJoerg Wunsch 	case FD_CLRERR:
24522995d110SJoerg Wunsch 		if (suser(p) != 0)
24532995d110SJoerg Wunsch 			return EPERM;
24542995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
24552995d110SJoerg Wunsch 		break;
24562995d110SJoerg Wunsch 
24572995d110SJoerg Wunsch 	case FD_GSTAT:
24582995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
24592995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
24602995d110SJoerg Wunsch 			return EINVAL;
24612995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
24622995d110SJoerg Wunsch 		break;
24632995d110SJoerg Wunsch 
2464250300ebSJoerg Wunsch 	case FD_READID:
2465250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2466250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2467250300ebSJoerg Wunsch 			return EINVAL;
2468250300ebSJoerg Wunsch 		error = fdreadid(dev, rid);
2469250300ebSJoerg Wunsch 		break;
2470250300ebSJoerg Wunsch 
2471f5f7ba03SJordan K. Hubbard 	default:
24723a2f7427SDavid Greenman 		error = ENOTTY;
2473f5f7ba03SJordan K. Hubbard 		break;
2474f5f7ba03SJordan K. Hubbard 	}
2475f5f7ba03SJordan K. Hubbard 	return (error);
2476f5f7ba03SJordan K. Hubbard }
2477