xref: /freebsd/sys/dev/fdc/fdc.c (revision 250300ebb02595388c54059794bf53ce5d35842c)
187f6c662SJulian Elischer /*
25b81b6b3SRodney W. Grimes  * Copyright (c) 1990 The Regents of the University of California.
35b81b6b3SRodney W. Grimes  * All rights reserved.
45b81b6b3SRodney W. Grimes  *
55b81b6b3SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
65b81b6b3SRodney W. Grimes  * Don Ahn.
75b81b6b3SRodney W. Grimes  *
869acd21dSWarner Losh  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
969acd21dSWarner Losh  * aided by the Linux floppy driver modifications from David Bateman
1069acd21dSWarner Losh  * (dbateman@eng.uts.edu.au).
1169acd21dSWarner Losh  *
12dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994 by
133a2f7427SDavid Greenman  *  jc@irbs.UUCP (John Capo)
143a2f7427SDavid Greenman  *  vak@zebub.msk.su (Serge Vakulenko)
153a2f7427SDavid Greenman  *  ache@astral.msk.su (Andrew A. Chernov)
16dc16046fSJoerg Wunsch  *
17dc16046fSJoerg Wunsch  * Copyright (c) 1993, 1994, 1995 by
183a2f7427SDavid Greenman  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
19dc5df763SJoerg Wunsch  *  dufault@hda.com (Peter Dufault)
203a2f7427SDavid Greenman  *
212995d110SJoerg Wunsch  * Copyright (c) 2001 Joerg Wunsch,
2206740f7bSJoerg Wunsch  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
232995d110SJoerg Wunsch  *
245b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
255b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
265b81b6b3SRodney W. Grimes  * are met:
275b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
285b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
295b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
305b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
315b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
325b81b6b3SRodney W. Grimes  *
335b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
345b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
355b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
365b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
385b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
405b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
415b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
425b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
435b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
445b81b6b3SRodney W. Grimes  *
45dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
46c3aac50fSPeter Wemm  * $FreeBSD$
475b81b6b3SRodney W. Grimes  *
485b81b6b3SRodney W. Grimes  */
495b81b6b3SRodney W. Grimes 
50d2fb4892SJoerg Wunsch #include "opt_fdc.h"
515f830ea2SJoerg Wunsch #include "card.h"
525b81b6b3SRodney W. Grimes 
53b99f0a4aSAndrew Moore #include <sys/param.h>
54b99f0a4aSAndrew Moore #include <sys/systm.h>
559626b608SPoul-Henning Kamp #include <sys/bio.h>
566182fdbdSPeter Wemm #include <sys/bus.h>
576182fdbdSPeter Wemm #include <sys/conf.h>
58b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
59fb919e4dSMark Murray #include <sys/disklabel.h>
606182fdbdSPeter Wemm #include <sys/fcntl.h>
61e774b251SJoerg Wunsch #include <sys/fdcio.h>
62fb919e4dSMark Murray #include <sys/kernel.h>
63fb919e4dSMark Murray #include <sys/lock.h>
64b99f0a4aSAndrew Moore #include <sys/malloc.h>
656182fdbdSPeter Wemm #include <sys/module.h>
66fb919e4dSMark Murray #include <sys/mutex.h>
673a2f7427SDavid Greenman #include <sys/proc.h>
68b99f0a4aSAndrew Moore #include <sys/syslog.h>
696182fdbdSPeter Wemm 
706182fdbdSPeter Wemm #include <sys/bus.h>
716182fdbdSPeter Wemm #include <machine/bus.h>
726182fdbdSPeter Wemm #include <sys/rman.h>
736182fdbdSPeter Wemm 
746182fdbdSPeter Wemm #include <machine/clock.h>
756182fdbdSPeter Wemm #include <machine/resource.h>
76dc5df763SJoerg Wunsch #include <machine/stdarg.h>
776182fdbdSPeter Wemm 
786182fdbdSPeter Wemm #include <isa/isavar.h>
79a97c75b7SDoug Rabson #include <isa/isareg.h>
80a97c75b7SDoug Rabson #include <isa/fdreg.h>
81a97c75b7SDoug Rabson #include <isa/fdc.h>
82a97c75b7SDoug Rabson #include <isa/rtc.h>
836182fdbdSPeter Wemm 
840722d6abSJoerg Wunsch /* configuration flags */
850722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
86e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
870722d6abSJoerg Wunsch 
880722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
890722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
900722d6abSJoerg Wunsch 
91dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
92dc5df763SJoerg Wunsch #define FD_FAILED -1
93dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
94dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
95dc5df763SJoerg Wunsch 
966fb89845SKATO Takenori #define NUMTYPES 17
976fb89845SKATO Takenori #define NUMDENS  (NUMTYPES - 7)
987ca0641bSAndrey A. Chernov 
99b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
100b99f0a4aSAndrew Moore #define FD_1720         1
101b99f0a4aSAndrew Moore #define FD_1480         2
102b99f0a4aSAndrew Moore #define FD_1440         3
103b99f0a4aSAndrew Moore #define FD_1200         4
104b99f0a4aSAndrew Moore #define FD_820          5
105b99f0a4aSAndrew Moore #define FD_800          6
106b99f0a4aSAndrew Moore #define FD_720          7
107b99f0a4aSAndrew Moore #define FD_360          8
1086fb89845SKATO Takenori #define FD_640          9
1096fb89845SKATO Takenori #define FD_1232         10
110ed2fa05eSAndrey A. Chernov 
1116fb89845SKATO Takenori #define FD_1480in5_25   11
1126fb89845SKATO Takenori #define FD_1440in5_25   12
1136fb89845SKATO Takenori #define FD_820in5_25    13
1146fb89845SKATO Takenori #define FD_800in5_25    14
1156fb89845SKATO Takenori #define FD_720in5_25    15
1166fb89845SKATO Takenori #define FD_360in5_25    16
1176fb89845SKATO Takenori #define FD_640in5_25    17
118b99f0a4aSAndrew Moore 
119250300ebSJoerg Wunsch #define BIO_RDSECTID	BIO_FLAG1
1207ca0641bSAndrey A. Chernov 
1216f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1225b81b6b3SRodney W. Grimes {
123126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
124126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
125126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
126126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
127126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
128126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
129126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
130b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
1316fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /*  640K in DD 5.25in */
1326fb89845SKATO Takenori {  8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
133ed2fa05eSAndrey A. Chernov 
134126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
135126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
136126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
137126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
138126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
139126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1406fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /*  640K in HD 5.25in */
1415b81b6b3SRodney W. Grimes };
1425b81b6b3SRodney W. Grimes 
143b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
144dc16046fSJoerg Wunsch 
145f8ce7dd5SJoerg Wunsch #define MAX_SEC_SIZE	(128 << 3)
146250300ebSJoerg Wunsch #define MAX_CYLINDER	79
147250300ebSJoerg Wunsch #define MAX_HEAD	1
148f8ce7dd5SJoerg Wunsch 
1495b81b6b3SRodney W. Grimes /***********************************************************************\
1505b81b6b3SRodney W. Grimes * Per controller structure.						*
1515b81b6b3SRodney W. Grimes \***********************************************************************/
1526182fdbdSPeter Wemm static devclass_t fdc_devclass;
1535b81b6b3SRodney W. Grimes 
1545b81b6b3SRodney W. Grimes /***********************************************************************\
1555b81b6b3SRodney W. Grimes * Per drive structure.							*
156b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1575b81b6b3SRodney W. Grimes \***********************************************************************/
1586182fdbdSPeter Wemm struct fd_data {
159b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1605b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1613a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1625b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1635b81b6b3SRodney W. Grimes 	int	flags;
1645b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1655b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1665b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1675b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1685b81b6b3SRodney W. Grimes 	int	skip;
1695b81b6b3SRodney W. Grimes 	int	hddrv;
170dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1715b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1723a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
17302a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
17402a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
175b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
1766182fdbdSPeter Wemm 	device_t dev;
1776182fdbdSPeter Wemm 	fdu_t	fdu;
1786182fdbdSPeter Wemm };
17937286586SPeter Wemm 
18037286586SPeter Wemm struct fdc_ivars {
18137286586SPeter Wemm 	int	fdunit;
18237286586SPeter Wemm };
1836182fdbdSPeter Wemm static devclass_t fd_devclass;
1845b81b6b3SRodney W. Grimes 
1855b81b6b3SRodney W. Grimes /***********************************************************************\
1865b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1875b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1885b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1895b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1905b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1915b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1925b81b6b3SRodney W. Grimes \***********************************************************************/
193b99f0a4aSAndrew Moore 
1943a2f7427SDavid Greenman /* internal functions */
1956182fdbdSPeter Wemm static	void fdc_intr(void *);
1966182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
1973a2f7427SDavid Greenman #  define TURNON 1
1983a2f7427SDavid Greenman #  define TURNOFF 0
1993a2f7427SDavid Greenman static timeout_t fd_turnoff;
2003a2f7427SDavid Greenman static timeout_t fd_motor_on;
2016182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
2023a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2036182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
20480909a7dSJoerg Wunsch static int out_fdc(struct fdc_data *, int);
2056182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
2065c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2073a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2086182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
2096182fdbdSPeter Wemm static int retrier(struct fdc_data *);
2103a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
211250300ebSJoerg Wunsch static int fdreadid(dev_t, struct fdc_readid *);
2123a2f7427SDavid Greenman 
213d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
21480909a7dSJoerg Wunsch static void fd_clone (void *arg, char *name, int namelen, dev_t *dev);
215d66c374fSTor Egge 
216d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
217d66c374fSTor Egge 
218d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
219cba2a7c6SBruce Evans static char const * const fdstates[] =
2205b81b6b3SRodney W. Grimes {
2215b81b6b3SRodney W. Grimes "DEVIDLE",
2225b81b6b3SRodney W. Grimes "FINDWORK",
2235b81b6b3SRodney W. Grimes "DOSEEK",
2245b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2255b81b6b3SRodney W. Grimes "IOCOMPLETE",
2265b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2275b81b6b3SRodney W. Grimes "STARTRECAL",
2285b81b6b3SRodney W. Grimes "RESETCTLR",
2295b81b6b3SRodney W. Grimes "SEEKWAIT",
2305b81b6b3SRodney W. Grimes "RECALWAIT",
2315b81b6b3SRodney W. Grimes "MOTORWAIT",
2325c1a1eaeSBruce Evans "IOTIMEDOUT",
2335c1a1eaeSBruce Evans "RESETCOMPLETE",
234ff9607b0SBruce Evans "PIOREAD",
2355b81b6b3SRodney W. Grimes };
2365b81b6b3SRodney W. Grimes 
2373a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
238cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2395b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2405b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
241d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2425b81b6b3SRodney W. Grimes #define TRACE0(arg)
2435b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
244d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2455b81b6b3SRodney W. Grimes 
246427ccf00SDoug Rabson static void
247427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
248427ccf00SDoug Rabson {
249427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
250427ccf00SDoug Rabson }
251427ccf00SDoug Rabson 
252427ccf00SDoug Rabson static u_int8_t
253427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
254427ccf00SDoug Rabson {
255427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
256427ccf00SDoug Rabson }
257427ccf00SDoug Rabson 
258427ccf00SDoug Rabson static void
259427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
260427ccf00SDoug Rabson {
261427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
262427ccf00SDoug Rabson }
263427ccf00SDoug Rabson 
264427ccf00SDoug Rabson static u_int8_t
265427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
266427ccf00SDoug Rabson {
267427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
268427ccf00SDoug Rabson }
269427ccf00SDoug Rabson 
270427ccf00SDoug Rabson static void
2715f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
272427ccf00SDoug Rabson {
273427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
274427ccf00SDoug Rabson }
275427ccf00SDoug Rabson 
27658c9d623SPeter Wemm #if NCARD > 0
2775f830ea2SJoerg Wunsch static void
2785f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
2795f830ea2SJoerg Wunsch {
2805f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
2815f830ea2SJoerg Wunsch }
28258c9d623SPeter Wemm #endif
2835f830ea2SJoerg Wunsch 
284427ccf00SDoug Rabson #if 0
285427ccf00SDoug Rabson 
286427ccf00SDoug Rabson static u_int8_t
287427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
288427ccf00SDoug Rabson {
289427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
290427ccf00SDoug Rabson }
291427ccf00SDoug Rabson 
292427ccf00SDoug Rabson #endif
293427ccf00SDoug Rabson 
294f8ce7dd5SJoerg Wunsch /*
295f8ce7dd5SJoerg Wunsch  * named Fdopen() to avoid confusion with fdopen() in fd(4); the
296f8ce7dd5SJoerg Wunsch  * difference is now only meaningful for debuggers
297f8ce7dd5SJoerg Wunsch  */
298f8ce7dd5SJoerg Wunsch static	d_open_t	Fdopen;
29987f6c662SJulian Elischer static	d_close_t	fdclose;
30087f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
30187f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
30287f6c662SJulian Elischer 
30387f6c662SJulian Elischer #define CDEV_MAJOR 9
304f7ea2f55SJulian Elischer 
3054e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
3064e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
3074e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
3084e2f199eSPoul-Henning Kamp 	/* read */	physread,
3094e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
3104e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
3114e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
3124e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
3134e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
3144e2f199eSPoul-Henning Kamp 	/* name */	"fd",
3154e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
3164e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
3174e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
3184e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
3194e2f199eSPoul-Henning Kamp };
3204e2f199eSPoul-Henning Kamp 
321dc5df763SJoerg Wunsch static int
3226182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
323dc5df763SJoerg Wunsch {
3246182fdbdSPeter Wemm 	fdc->fdc_errs++;
32516b04b6aSJoerg Wunsch 	if (s) {
326b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
327b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
328b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
329b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
330b6e5f28eSPeter Wemm 						    "logging any more\n");
33116b04b6aSJoerg Wunsch 	}
332dc5df763SJoerg Wunsch 
333dc5df763SJoerg Wunsch 	return FD_FAILED;
334dc5df763SJoerg Wunsch }
335dc5df763SJoerg Wunsch 
336dc5df763SJoerg Wunsch /*
337dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
338dc5df763SJoerg Wunsch  * Unit number,
339dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
340dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
341dc5df763SJoerg Wunsch  */
3426f4e0bebSPoul-Henning Kamp static int
3436182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
344dc5df763SJoerg Wunsch {
345dc5df763SJoerg Wunsch 	u_char cmd;
346dc5df763SJoerg Wunsch 	int n_in;
347dc5df763SJoerg Wunsch 	int n;
348dc5df763SJoerg Wunsch 	va_list ap;
349dc5df763SJoerg Wunsch 
350dc5df763SJoerg Wunsch 	va_start(ap, n_out);
351dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
352dc5df763SJoerg Wunsch 	va_end(ap);
353dc5df763SJoerg Wunsch 	va_start(ap, n_out);
354dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
355dc5df763SJoerg Wunsch 	{
3566182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
357dc5df763SJoerg Wunsch 		{
358dc5df763SJoerg Wunsch 			char msg[50];
3592127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
360dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
361dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
3626182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
363dc5df763SJoerg Wunsch 		}
364dc5df763SJoerg Wunsch 	}
365dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
366dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
367dc5df763SJoerg Wunsch 	{
368dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
3696182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
370dc5df763SJoerg Wunsch 		{
371dc5df763SJoerg Wunsch 			char msg[50];
3722127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
373dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
374dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
3756182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
376dc5df763SJoerg Wunsch 		}
377dc5df763SJoerg Wunsch 	}
378dc5df763SJoerg Wunsch 
379dc5df763SJoerg Wunsch 	return 0;
380dc5df763SJoerg Wunsch }
381dc5df763SJoerg Wunsch 
3826f4e0bebSPoul-Henning Kamp static int
383d66c374fSTor Egge enable_fifo(fdc_p fdc)
384d66c374fSTor Egge {
385d66c374fSTor Egge 	int i, j;
386d66c374fSTor Egge 
387d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
388d66c374fSTor Egge 
389d66c374fSTor Egge 		/*
390d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
391d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
392d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
393d66c374fSTor Egge 		 */
394d66c374fSTor Egge 
3956182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
3966182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
397d66c374fSTor Egge 
398d66c374fSTor Egge 		/* If command is invalid, return */
399d66c374fSTor Egge 		j = 100000;
400427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
401d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
402d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
403d66c374fSTor Egge 				fdc_reset(fdc);
404d66c374fSTor Egge 				return FD_FAILED;
405d66c374fSTor Egge 			}
406d66c374fSTor Egge 		if (j<0 ||
4076182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
408d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
409d66c374fSTor Egge 			fdc_reset(fdc);
4106182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
411d66c374fSTor Egge 		}
412d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
413d66c374fSTor Egge 		return 0;
414d66c374fSTor Egge 	}
4156182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
416d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
4176182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
418d66c374fSTor Egge 	return 0;
419d66c374fSTor Egge }
420d66c374fSTor Egge 
421d66c374fSTor Egge static int
422dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
423dc5df763SJoerg Wunsch {
424dc5df763SJoerg Wunsch 	int st3;
425dc5df763SJoerg Wunsch 
4266182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
427dc5df763SJoerg Wunsch 	{
4286182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
429dc5df763SJoerg Wunsch 	}
430dc5df763SJoerg Wunsch 	if (st3p)
431dc5df763SJoerg Wunsch 		*st3p = st3;
432dc5df763SJoerg Wunsch 
433dc5df763SJoerg Wunsch 	return 0;
434dc5df763SJoerg Wunsch }
435dc5df763SJoerg Wunsch 
4366f4e0bebSPoul-Henning Kamp static int
437dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
438dc5df763SJoerg Wunsch {
4396182fdbdSPeter Wemm 	int cyl, st0, ret;
440dc5df763SJoerg Wunsch 
4416182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
4426182fdbdSPeter Wemm 	if (ret) {
4436182fdbdSPeter Wemm 		(void)fdc_err(fdc,
444dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
445dc5df763SJoerg Wunsch 		return ret;
446dc5df763SJoerg Wunsch 	}
447dc5df763SJoerg Wunsch 
448dc5df763SJoerg Wunsch 	if (st0p)
449dc5df763SJoerg Wunsch 		*st0p = st0;
450dc5df763SJoerg Wunsch 
4516182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
452dc5df763SJoerg Wunsch 		/*
453dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
454dc5df763SJoerg Wunsch 		 */
455dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
456dc5df763SJoerg Wunsch 	}
457dc5df763SJoerg Wunsch 
4586182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
4596182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
460dc5df763SJoerg Wunsch 	}
461dc5df763SJoerg Wunsch 
462dc5df763SJoerg Wunsch 	if (cylp)
463dc5df763SJoerg Wunsch 		*cylp = cyl;
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 	return 0;
466dc5df763SJoerg Wunsch }
467dc5df763SJoerg Wunsch 
468dc5df763SJoerg Wunsch 
4696f4e0bebSPoul-Henning Kamp static int
470dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
471dc5df763SJoerg Wunsch {
472dc5df763SJoerg Wunsch 	int i, ret;
473b5e8ce9fSBruce Evans 
4746182fdbdSPeter Wemm 	for (i = 0; i < 7; i++) {
475b5e8ce9fSBruce Evans 		/*
476b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
477a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
478b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
479b5e8ce9fSBruce Evans 		 */
480b5e8ce9fSBruce Evans 		int status;
481b5e8ce9fSBruce Evans 
4826182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
483b5e8ce9fSBruce Evans 		fdc->status[i] = status;
484b5e8ce9fSBruce Evans 		if (ret != 0)
485dc5df763SJoerg Wunsch 			break;
486dc5df763SJoerg Wunsch 	}
487dc5df763SJoerg Wunsch 
488dc5df763SJoerg Wunsch 	if (ret == 0)
489dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
490dc5df763SJoerg Wunsch 	else
491dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
492dc5df763SJoerg Wunsch 
493dc5df763SJoerg Wunsch 	return ret;
494dc5df763SJoerg Wunsch }
495dc5df763SJoerg Wunsch 
4965b81b6b3SRodney W. Grimes /****************************************************************************/
4975b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
4985b81b6b3SRodney W. Grimes /****************************************************************************/
499dc5df763SJoerg Wunsch 
5003a2f7427SDavid Greenman static int
50137286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
5025b81b6b3SRodney W. Grimes {
50337286586SPeter Wemm 	device_t dev;
5045f830ea2SJoerg Wunsch 	int ispnp, ispcmcia;
5055b81b6b3SRodney W. Grimes 
50637286586SPeter Wemm 	dev = fdc->fdc_dev;
5075f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
5085f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
5096182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
5106182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
5116182fdbdSPeter Wemm 
512b6e5f28eSPeter Wemm 	/*
5135f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
5145f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
5155f830ea2SJoerg Wunsch 	 * register at 0x3f6.
5165f830ea2SJoerg Wunsch 	 *
517b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
5185f830ea2SJoerg Wunsch 	 *
5195f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
5205f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
5215f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
522b6e5f28eSPeter Wemm 	 */
5236182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
5246182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
5255f830ea2SJoerg Wunsch 					     ispcmcia ? 8 : (ispnp ? 1 : 6),
5265f830ea2SJoerg Wunsch 					     RF_ACTIVE);
5276182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
528427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve I/O port range\n");
52937286586SPeter Wemm 		return ENXIO;
5306182fdbdSPeter Wemm 	}
531427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
532427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
533427ccf00SDoug Rabson 
5345f830ea2SJoerg Wunsch 	if (!ispcmcia) {
535427ccf00SDoug Rabson 		/*
5365f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
5375f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
5385f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
5395f830ea2SJoerg Wunsch 		 * accordingly.
540b6e5f28eSPeter Wemm 		 */
541b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
542b6e5f28eSPeter Wemm 			fdc->port_off = -2;
543b6e5f28eSPeter Wemm 
544b6e5f28eSPeter Wemm 		/*
5455f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
5465f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
5475f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
548a2639a18SPeter Wemm 		 *
5495f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
5505f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
5515f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
5525f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
5535f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
5545f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
5555f830ea2SJoerg Wunsch 		 * address for the control register.
556427ccf00SDoug Rabson 		 */
557b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
558b9da888fSPeter Wemm 			u_long ctlstart;
559b9da888fSPeter Wemm 
560b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
5615f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
5625f830ea2SJoerg Wunsch 				fdc->port_off + 7;
563b6e5f28eSPeter Wemm 
564b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
565b9da888fSPeter Wemm 		}
566b6e5f28eSPeter Wemm 
567b6e5f28eSPeter Wemm 		/*
568b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
569b6e5f28eSPeter Wemm 		 */
570427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
5715f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
5725f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
573b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
574427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
5755f830ea2SJoerg Wunsch 			device_printf(dev,
5765f830ea2SJoerg Wunsch 				      "cannot reserve control I/O port range\n");
57737286586SPeter Wemm 			return ENXIO;
578427ccf00SDoug Rabson 		}
579427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
580427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
5815f830ea2SJoerg Wunsch 	}
5826182fdbdSPeter Wemm 
5836182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
5846182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
5856182fdbdSPeter Wemm 					  RF_ACTIVE);
5866182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
587427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
58837286586SPeter Wemm 		return ENXIO;
5896182fdbdSPeter Wemm 	}
5905f830ea2SJoerg Wunsch 
5915f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
5926182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
5936182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
5946182fdbdSPeter Wemm 						  RF_ACTIVE);
5956182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
596427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
59737286586SPeter Wemm 			return ENXIO;
5986182fdbdSPeter Wemm 		}
5996182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
6005f830ea2SJoerg Wunsch 	}
60137286586SPeter Wemm 
60237286586SPeter Wemm 	return 0;
60337286586SPeter Wemm }
60437286586SPeter Wemm 
60537286586SPeter Wemm static void
60637286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
60737286586SPeter Wemm {
60837286586SPeter Wemm 	device_t dev;
60937286586SPeter Wemm 
61037286586SPeter Wemm 	dev = fdc->fdc_dev;
61137286586SPeter Wemm 	if (fdc->res_irq != 0) {
61237286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
61337286586SPeter Wemm 					fdc->res_irq);
61437286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
61537286586SPeter Wemm 				     fdc->res_irq);
61637286586SPeter Wemm 	}
61737286586SPeter Wemm 	if (fdc->res_ctl != 0) {
61837286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
61937286586SPeter Wemm 					fdc->res_ctl);
62037286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
62137286586SPeter Wemm 				     fdc->res_ctl);
62237286586SPeter Wemm 	}
62337286586SPeter Wemm 	if (fdc->res_ioport != 0) {
62437286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
62537286586SPeter Wemm 					fdc->res_ioport);
62637286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
62737286586SPeter Wemm 				     fdc->res_ioport);
62837286586SPeter Wemm 	}
62937286586SPeter Wemm 	if (fdc->res_drq != 0) {
63037286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
63137286586SPeter Wemm 					fdc->res_drq);
63237286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
63337286586SPeter Wemm 				     fdc->res_drq);
63437286586SPeter Wemm 	}
63537286586SPeter Wemm }
63637286586SPeter Wemm 
63737286586SPeter Wemm /****************************************************************************/
63837286586SPeter Wemm /*                      autoconfiguration stuff                             */
63937286586SPeter Wemm /****************************************************************************/
64037286586SPeter Wemm 
64137286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
64237286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
64337286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
64437286586SPeter Wemm 	{0}
64537286586SPeter Wemm };
64637286586SPeter Wemm 
64737286586SPeter Wemm static int
6483aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
64937286586SPeter Wemm {
65037286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
65137286586SPeter Wemm 
65237286586SPeter Wemm 	switch (which) {
65337286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
65437286586SPeter Wemm 		*result = ivars->fdunit;
65537286586SPeter Wemm 		break;
65637286586SPeter Wemm 	default:
65737286586SPeter Wemm 		return ENOENT;
65837286586SPeter Wemm 	}
65937286586SPeter Wemm 	return 0;
66037286586SPeter Wemm }
66137286586SPeter Wemm 
66237286586SPeter Wemm /*
66337286586SPeter Wemm  * fdc controller section.
66437286586SPeter Wemm  */
66537286586SPeter Wemm static int
66637286586SPeter Wemm fdc_probe(device_t dev)
66737286586SPeter Wemm {
66837286586SPeter Wemm 	int	error, ic_type;
66937286586SPeter Wemm 	struct	fdc_data *fdc;
67037286586SPeter Wemm 
67137286586SPeter Wemm 	fdc = device_get_softc(dev);
67237286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
67337286586SPeter Wemm 	fdc->fdc_dev = dev;
6745f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
67537286586SPeter Wemm 
67637286586SPeter Wemm 	/* Check pnp ids */
67737286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
67837286586SPeter Wemm 	if (error == ENXIO)
67937286586SPeter Wemm 		return ENXIO;
6805f830ea2SJoerg Wunsch 	if (error == 0)
6815f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
68237286586SPeter Wemm 
68337286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
68437286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
68537286586SPeter Wemm 	if (error)
68637286586SPeter Wemm 		goto out;
6875b81b6b3SRodney W. Grimes 
68816111cedSAndrew Moore 	/* First - lets reset the floppy controller */
689427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
69016111cedSAndrew Moore 	DELAY(100);
691427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
69216111cedSAndrew Moore 
6935b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
6946182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
6956182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
6966182fdbdSPeter Wemm 		error = ENXIO;
6976182fdbdSPeter Wemm 		goto out;
6985b81b6b3SRodney W. Grimes 	}
6996182fdbdSPeter Wemm 
7006182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
7016182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
7026182fdbdSPeter Wemm 		switch (ic_type) {
7036182fdbdSPeter Wemm 		case 0x80:
7046182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
7056182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
7066182fdbdSPeter Wemm 			break;
7076182fdbdSPeter Wemm 		case 0x81:
7086182fdbdSPeter Wemm 			device_set_desc(dev, "Intel 82077 or clone");
7096182fdbdSPeter Wemm 			fdc->fdct = FDC_I82077;
7106182fdbdSPeter Wemm 			break;
7116182fdbdSPeter Wemm 		case 0x90:
7126182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 72065B or clone");
7136182fdbdSPeter Wemm 			fdc->fdct = FDC_NE72065;
7146182fdbdSPeter Wemm 			break;
7156182fdbdSPeter Wemm 		default:
7166182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
7176182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
7186182fdbdSPeter Wemm 			break;
7196182fdbdSPeter Wemm 		}
7206182fdbdSPeter Wemm 	}
7216182fdbdSPeter Wemm 
7226182fdbdSPeter Wemm out:
72337286586SPeter Wemm 	fdc_release_resources(fdc);
7246182fdbdSPeter Wemm 	return (error);
7255b81b6b3SRodney W. Grimes }
7265b81b6b3SRodney W. Grimes 
7275f830ea2SJoerg Wunsch #if NCARD > 0
7285f830ea2SJoerg Wunsch 
7295f830ea2SJoerg Wunsch static int
7305f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
7315f830ea2SJoerg Wunsch {
7325f830ea2SJoerg Wunsch 	int	error;
7335f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7345f830ea2SJoerg Wunsch 
7355f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7365f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
7375f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
7385f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
7395f830ea2SJoerg Wunsch 
7405f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
7415f830ea2SJoerg Wunsch 
7425f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
7435f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
7445f830ea2SJoerg Wunsch 	if (error)
7455f830ea2SJoerg Wunsch 		goto out;
7465f830ea2SJoerg Wunsch 
7475f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
7485f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
7495f830ea2SJoerg Wunsch 	DELAY(100);
7505f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
7515f830ea2SJoerg Wunsch 
7525f830ea2SJoerg Wunsch 	/* see if it can handle a command */
7535f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7545f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
7555f830ea2SJoerg Wunsch 		error = ENXIO;
7565f830ea2SJoerg Wunsch 		goto out;
7575f830ea2SJoerg Wunsch 	}
7585f830ea2SJoerg Wunsch 
7595f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
7605f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
7615f830ea2SJoerg Wunsch 
7625f830ea2SJoerg Wunsch out:
7635f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7645f830ea2SJoerg Wunsch 	return (error);
7655f830ea2SJoerg Wunsch }
7665f830ea2SJoerg Wunsch 
7675f830ea2SJoerg Wunsch static int
7685f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev)
7695f830ea2SJoerg Wunsch {
7705f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7715f830ea2SJoerg Wunsch 	int	error;
7725f830ea2SJoerg Wunsch 
7735f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7745f830ea2SJoerg Wunsch 
7755f830ea2SJoerg Wunsch 	/* have our children detached first */
7765f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
7775f830ea2SJoerg Wunsch 		return (error);
7785f830ea2SJoerg Wunsch 
7795f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
7805f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
7815f830ea2SJoerg Wunsch 		return (0);
7825f830ea2SJoerg Wunsch 	}
7835f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
7845f830ea2SJoerg Wunsch 
7855f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
7865f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
7875f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7885f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
7895f830ea2SJoerg Wunsch 	return (0);
7905f830ea2SJoerg Wunsch }
7915f830ea2SJoerg Wunsch 
7925f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
7935f830ea2SJoerg Wunsch 
7945b81b6b3SRodney W. Grimes /*
79537286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
7965b81b6b3SRodney W. Grimes  */
7976182fdbdSPeter Wemm static void
79837286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
7995b81b6b3SRodney W. Grimes {
80037286586SPeter Wemm 	int	disabled;
80137286586SPeter Wemm 	struct fdc_ivars *ivar;
8026182fdbdSPeter Wemm 	device_t child;
80392200632SGarrett Wollman 
8047cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
80537286586SPeter Wemm 	if (ivar == NULL)
8066182fdbdSPeter Wemm 		return;
80737286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
80837286586SPeter Wemm 		ivar->fdunit = 0;
809fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
81037286586SPeter Wemm 	if (child == NULL)
8116182fdbdSPeter Wemm 		return;
81237286586SPeter Wemm 	device_set_ivars(child, ivar);
813a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
814a97c75b7SDoug Rabson 	    && disabled != 0)
8156182fdbdSPeter Wemm 		device_disable(child);
8166182fdbdSPeter Wemm }
8176182fdbdSPeter Wemm 
8186182fdbdSPeter Wemm static int
8196182fdbdSPeter Wemm fdc_attach(device_t dev)
8206182fdbdSPeter Wemm {
82137286586SPeter Wemm 	struct	fdc_data *fdc;
8222398f0cdSPeter Wemm 	int	i, error, dunit;
8232398f0cdSPeter Wemm 	const char *name, *dname;
824427ccf00SDoug Rabson 
82537286586SPeter Wemm 	fdc = device_get_softc(dev);
82637286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
82737286586SPeter Wemm 	if (error) {
828f8ce7dd5SJoerg Wunsch 		device_printf(dev, "cannot re-acquire resources\n");
82937286586SPeter Wemm 		return error;
83037286586SPeter Wemm 	}
83137286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
8323c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
8333c36743eSMark Murray 			       &fdc->fdc_intr);
83437286586SPeter Wemm 	if (error) {
83537286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
83637286586SPeter Wemm 		return error;
83737286586SPeter Wemm 	}
83837286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
8395b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
8406182fdbdSPeter Wemm 
8415f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
842f8ce7dd5SJoerg Wunsch 		/*
843f8ce7dd5SJoerg Wunsch 		 * Acquire the DMA channel forever, the driver will do
844f8ce7dd5SJoerg Wunsch 		 * the rest
845f8ce7dd5SJoerg Wunsch 		 * XXX should integrate with rman
846f8ce7dd5SJoerg Wunsch 		 */
847100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
848f8ce7dd5SJoerg Wunsch 		isa_dmainit(fdc->dmachan, MAX_SEC_SIZE);
8495f830ea2SJoerg Wunsch 	}
8505b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
8516182fdbdSPeter Wemm 
8523a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
853427ccf00SDoug Rabson 	fdout_wr(fdc, ((fdc->fdout = 0)));
8548177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
8555b81b6b3SRodney W. Grimes 
8566182fdbdSPeter Wemm 	/*
85737286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
85837286586SPeter Wemm 	 * devices from the BIOS unless overridden.
8596182fdbdSPeter Wemm 	 */
860ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
8612398f0cdSPeter Wemm 	i = 0;
8622398f0cdSPeter Wemm 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0)
8632398f0cdSPeter Wemm 		fdc_add_child(dev, dname, dunit);
86437286586SPeter Wemm 
8656182fdbdSPeter Wemm 	return (bus_generic_attach(dev));
8666182fdbdSPeter Wemm }
8676182fdbdSPeter Wemm 
86815317dd8SMatthew N. Dodd static int
8696182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
8706182fdbdSPeter Wemm {
87115317dd8SMatthew N. Dodd 	int retval = 0;
87215317dd8SMatthew N. Dodd 
87315317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
87415317dd8SMatthew N. Dodd 	retval += printf(" on %s drive %d\n", device_get_nameunit(me),
87537286586SPeter Wemm 	       fdc_get_fdunit(child));
87615317dd8SMatthew N. Dodd 
87715317dd8SMatthew N. Dodd 	return (retval);
8786182fdbdSPeter Wemm }
8796182fdbdSPeter Wemm 
88016e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
88116e68fc6SPeter Wemm 	/* Device interface */
88216e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
88316e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
88416e68fc6SPeter Wemm 	DEVMETHOD(device_detach,	bus_generic_detach),
88516e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88616e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
88716e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
88816e68fc6SPeter Wemm 
88916e68fc6SPeter Wemm 	/* Bus interface */
89016e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
89137286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
89216e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
89316e68fc6SPeter Wemm 
89416e68fc6SPeter Wemm 	{ 0, 0 }
89516e68fc6SPeter Wemm };
89616e68fc6SPeter Wemm 
89716e68fc6SPeter Wemm static driver_t fdc_driver = {
89816e68fc6SPeter Wemm 	"fdc",
89916e68fc6SPeter Wemm 	fdc_methods,
90016e68fc6SPeter Wemm 	sizeof(struct fdc_data)
90116e68fc6SPeter Wemm };
90216e68fc6SPeter Wemm 
90316e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
90416e68fc6SPeter Wemm 
9055f830ea2SJoerg Wunsch #if NCARD > 0
9065f830ea2SJoerg Wunsch 
9075f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
9085f830ea2SJoerg Wunsch 	/* Device interface */
9095f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
9105f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
9115f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_pccard_detach),
9125f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
9135f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
9145f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
9155f830ea2SJoerg Wunsch 
9165f830ea2SJoerg Wunsch 	/* Bus interface */
9175f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
9185f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
9195f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
9205f830ea2SJoerg Wunsch 
9215f830ea2SJoerg Wunsch 	{ 0, 0 }
9225f830ea2SJoerg Wunsch };
9235f830ea2SJoerg Wunsch 
9245f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
9255f830ea2SJoerg Wunsch 	"fdc",
9265f830ea2SJoerg Wunsch 	fdc_pccard_methods,
9275f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
9285f830ea2SJoerg Wunsch };
9295f830ea2SJoerg Wunsch 
9305f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
9315f830ea2SJoerg Wunsch 
9325f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
9335f830ea2SJoerg Wunsch 
9343f54a085SPoul-Henning Kamp static struct {
9353f54a085SPoul-Henning Kamp 	char *match;
9363f54a085SPoul-Henning Kamp 	int minor;
9373f54a085SPoul-Henning Kamp 	int link;
9383f54a085SPoul-Henning Kamp } fd_suffix[] = {
9393f54a085SPoul-Henning Kamp 	{ "a",		0,	1 },
9403f54a085SPoul-Henning Kamp 	{ "b",		0,	1 },
9413f54a085SPoul-Henning Kamp 	{ "c",		0,	1 },
9423f54a085SPoul-Henning Kamp 	{ "d",		0,	1 },
9433f54a085SPoul-Henning Kamp 	{ "e",		0,	1 },
9443f54a085SPoul-Henning Kamp 	{ "f",		0,	1 },
9453f54a085SPoul-Henning Kamp 	{ "g",		0,	1 },
9463f54a085SPoul-Henning Kamp 	{ "h",		0,	1 },
9473f54a085SPoul-Henning Kamp 	{ ".1720",	1,	0 },
9483f54a085SPoul-Henning Kamp 	{ ".1480",	2,	0 },
9493f54a085SPoul-Henning Kamp 	{ ".1440",	3,	0 },
9503f54a085SPoul-Henning Kamp 	{ ".1200",	4,	0 },
9513f54a085SPoul-Henning Kamp 	{ ".820",	5,	0 },
9523f54a085SPoul-Henning Kamp 	{ ".800",	6,	0 },
9533f54a085SPoul-Henning Kamp 	{ ".720",	7,	0 },
9543f54a085SPoul-Henning Kamp 	{ ".360",	8,	0 },
9553f54a085SPoul-Henning Kamp 	{ ".640",	9,	0 },
9563f54a085SPoul-Henning Kamp 	{ ".1232",	10,	0 },
9573f54a085SPoul-Henning Kamp 	{ 0, 0 }
9583f54a085SPoul-Henning Kamp };
9593f54a085SPoul-Henning Kamp static void
96080909a7dSJoerg Wunsch fd_clone (void *arg, char *name, int namelen, dev_t *dev)
9613f54a085SPoul-Henning Kamp {
9623f54a085SPoul-Henning Kamp 	int u, d, i;
9633f54a085SPoul-Henning Kamp 	char *n;
9643f54a085SPoul-Henning Kamp 	dev_t pdev;
9653f54a085SPoul-Henning Kamp 
9663f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
9673f54a085SPoul-Henning Kamp 		return;
968db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
9693f54a085SPoul-Henning Kamp 		return;
9703f54a085SPoul-Henning Kamp 	for (i = 0; ; i++) {
9713f54a085SPoul-Henning Kamp 		if (fd_suffix[i].match == NULL)
9723f54a085SPoul-Henning Kamp 			return;
9733f54a085SPoul-Henning Kamp 		if (strcmp(n, fd_suffix[i].match))
9743f54a085SPoul-Henning Kamp 			continue;
9753f54a085SPoul-Henning Kamp 		d = fd_suffix[i].minor;
9763f54a085SPoul-Henning Kamp 		break;
9773f54a085SPoul-Henning Kamp 	}
9783f54a085SPoul-Henning Kamp 	if (fd_suffix[i].link == 0) {
9793f54a085SPoul-Henning Kamp 		*dev = make_dev(&fd_cdevsw, (u << 6) + d,
9803f54a085SPoul-Henning Kamp 			UID_ROOT, GID_OPERATOR, 0640, name);
9813f54a085SPoul-Henning Kamp 	} else {
9823f54a085SPoul-Henning Kamp 		pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d);
9833f54a085SPoul-Henning Kamp 		*dev = make_dev_alias(pdev, name);
9843f54a085SPoul-Henning Kamp 	}
9853f54a085SPoul-Henning Kamp }
9863f54a085SPoul-Henning Kamp 
98716e68fc6SPeter Wemm /******************************************************************/
98816e68fc6SPeter Wemm /*
98916e68fc6SPeter Wemm  * devices attached to the controller section.
99016e68fc6SPeter Wemm  */
9916182fdbdSPeter Wemm static int
9926182fdbdSPeter Wemm fd_probe(device_t dev)
9936182fdbdSPeter Wemm {
9946182fdbdSPeter Wemm 	int	i;
9956182fdbdSPeter Wemm 	u_int	fdt, st0, st3;
9966182fdbdSPeter Wemm 	struct	fd_data *fd;
9976182fdbdSPeter Wemm 	struct	fdc_data *fdc;
9986182fdbdSPeter Wemm 	fdsu_t	fdsu;
9996182fdbdSPeter Wemm 	static int fd_fifo = 0;
10006182fdbdSPeter Wemm 
10016182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
10026182fdbdSPeter Wemm 	fd = device_get_softc(dev);
10036182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
10046182fdbdSPeter Wemm 
10056182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
10066182fdbdSPeter Wemm 	fd->dev = dev;
10076182fdbdSPeter Wemm 	fd->fdc = fdc;
10086182fdbdSPeter Wemm 	fd->fdsu = fdsu;
10096182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
10106182fdbdSPeter Wemm 
1011a97c75b7SDoug Rabson #ifdef __i386__
1012b99f0a4aSAndrew Moore 	/* look up what bios thinks we have */
10136182fdbdSPeter Wemm 	switch (fd->fdu) {
10146182fdbdSPeter Wemm 	case 0:
10155f830ea2SJoerg Wunsch 		if ((fdc->flags & FDC_ISPCMCIA))
10165f830ea2SJoerg Wunsch 			fdt = RTCFDT_144M;
10175f830ea2SJoerg Wunsch 		else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
10180722d6abSJoerg Wunsch 			fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
10190722d6abSJoerg Wunsch 		else
10200722d6abSJoerg Wunsch 			fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1021b99f0a4aSAndrew Moore 		break;
10226182fdbdSPeter Wemm 	case 1:
10236182fdbdSPeter Wemm 		fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1024dc5df763SJoerg Wunsch 		break;
1025dc5df763SJoerg Wunsch 	default:
10266182fdbdSPeter Wemm 		fdt = RTCFDT_NONE;
1027dc5df763SJoerg Wunsch 		break;
10286b7bd95bSJoerg Wunsch 	}
1029a97c75b7SDoug Rabson #else
1030a97c75b7SDoug Rabson 	fdt = RTCFDT_144M;	/* XXX probably */
1031a97c75b7SDoug Rabson #endif
10326182fdbdSPeter Wemm 
10336182fdbdSPeter Wemm 	/* is there a unit? */
10346182fdbdSPeter Wemm 	if (fdt == RTCFDT_NONE)
10356182fdbdSPeter Wemm 		return (ENXIO);
10366182fdbdSPeter Wemm 
10376182fdbdSPeter Wemm 	/* select it */
10386182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
10396182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
10406182fdbdSPeter Wemm 
10418de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
10426182fdbdSPeter Wemm 	if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1043e34c71eaSJoerg Wunsch 	    && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
10446182fdbdSPeter Wemm 	    && enable_fifo(fdc) == 0) {
1045b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1046b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1047d66c374fSTor Egge 	}
10486182fdbdSPeter Wemm 	fd_fifo = 1;
10496182fdbdSPeter Wemm 
10506182fdbdSPeter Wemm 	if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
10516182fdbdSPeter Wemm 	    && (st3 & NE7_ST3_T0)) {
1052dc5df763SJoerg Wunsch 		/* if at track 0, first seek inwards */
1053dc5df763SJoerg Wunsch 		/* seek some steps: */
10546182fdbdSPeter Wemm 		fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1055dc5df763SJoerg Wunsch 		DELAY(300000); /* ...wait a moment... */
10566182fdbdSPeter Wemm 		fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1057dc5df763SJoerg Wunsch 	}
1058dc5df763SJoerg Wunsch 
1059dc5df763SJoerg Wunsch 	/* If we're at track 0 first seek inwards. */
10606182fdbdSPeter Wemm 	if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1061dc5df763SJoerg Wunsch 		/* Seek some steps... */
10626182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1063dc5df763SJoerg Wunsch 			/* ...wait a moment... */
1064dc5df763SJoerg Wunsch 			DELAY(300000);
1065dc5df763SJoerg Wunsch 			/* make ctrlr happy: */
10666182fdbdSPeter Wemm 			fd_sense_int(fdc, 0, 0);
1067dc5df763SJoerg Wunsch 		}
1068dc5df763SJoerg Wunsch 	}
1069dc5df763SJoerg Wunsch 
10706b7bd95bSJoerg Wunsch 	for (i = 0; i < 2; i++) {
10716b7bd95bSJoerg Wunsch 		/*
10726b7bd95bSJoerg Wunsch 		 * we must recalibrate twice, just in case the
10736b7bd95bSJoerg Wunsch 		 * heads have been beyond cylinder 76, since most
10746b7bd95bSJoerg Wunsch 		 * FDCs still barf when attempting to recalibrate
10756b7bd95bSJoerg Wunsch 		 * more than 77 steps
10766b7bd95bSJoerg Wunsch 		 */
1077dc5df763SJoerg Wunsch 		/* go back to 0: */
10786182fdbdSPeter Wemm 		if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
10796b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek*/
10806b7bd95bSJoerg Wunsch 			DELAY(i == 0 ? 1000000 : 300000);
10815b81b6b3SRodney W. Grimes 
10826b7bd95bSJoerg Wunsch 			/* anything responding? */
1083dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, 0) == 0 &&
1084dc5df763SJoerg Wunsch 			    (st0 & NE7_ST0_EC) == 0)
10856b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
10866b7bd95bSJoerg Wunsch 		}
1087dc5df763SJoerg Wunsch 	}
10886b7bd95bSJoerg Wunsch 
10896182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
10903a2f7427SDavid Greenman 
10913a2f7427SDavid Greenman 	if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
10926182fdbdSPeter Wemm 		return (ENXIO);
10935b81b6b3SRodney W. Grimes 
1094dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1095b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1096b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
10973a2f7427SDavid Greenman 	fd->options = 0;
109802a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
109902a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
11005b81b6b3SRodney W. Grimes 
1101b99f0a4aSAndrew Moore 	switch (fdt) {
11027ca0641bSAndrey A. Chernov 	case RTCFDT_12M:
11036182fdbdSPeter Wemm 		device_set_desc(dev, "1200-KB 5.25\" drive");
1104b99f0a4aSAndrew Moore 		fd->type = FD_1200;
11057ca0641bSAndrey A. Chernov 		break;
11060722d6abSJoerg Wunsch 	case RTCFDT_144M | RTCFDT_144M_PRETENDED:
11076182fdbdSPeter Wemm 		device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
11080722d6abSJoerg Wunsch 		fdt = RTCFDT_144M;
11096182fdbdSPeter Wemm 		fd->type = FD_1440;
11107ca0641bSAndrey A. Chernov 	case RTCFDT_144M:
11116182fdbdSPeter Wemm 		device_set_desc(dev, "1440-KB 3.5\" drive");
1112b99f0a4aSAndrew Moore 		fd->type = FD_1440;
11137ca0641bSAndrey A. Chernov 		break;
1114290dd077SJoerg Wunsch 	case RTCFDT_288M:
111586a727d9SJoerg Wunsch 	case RTCFDT_288M_1:
11166182fdbdSPeter Wemm 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1117290dd077SJoerg Wunsch 		fd->type = FD_1440;
1118290dd077SJoerg Wunsch 		break;
11197ca0641bSAndrey A. Chernov 	case RTCFDT_360K:
11206182fdbdSPeter Wemm 		device_set_desc(dev, "360-KB 5.25\" drive");
1121b99f0a4aSAndrew Moore 		fd->type = FD_360;
11227ca0641bSAndrey A. Chernov 		break;
1123ed2fa05eSAndrey A. Chernov 	case RTCFDT_720K:
11246182fdbdSPeter Wemm 		printf("720-KB 3.5\" drive");
1125b99f0a4aSAndrew Moore 		fd->type = FD_720;
1126ed2fa05eSAndrey A. Chernov 		break;
11277ca0641bSAndrey A. Chernov 	default:
11286182fdbdSPeter Wemm 		return (ENXIO);
11295b81b6b3SRodney W. Grimes 	}
11306182fdbdSPeter Wemm 	return (0);
11316182fdbdSPeter Wemm }
11326182fdbdSPeter Wemm 
11336182fdbdSPeter Wemm static int
11346182fdbdSPeter Wemm fd_attach(device_t dev)
11356182fdbdSPeter Wemm {
11366182fdbdSPeter Wemm 	struct	fd_data *fd;
1137db901281SPoul-Henning Kamp 	static int cdevsw_add_done = 0;
11386182fdbdSPeter Wemm 
11396182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11406182fdbdSPeter Wemm 
114102211baeSNick Hibma 	if (!cdevsw_add_done) {
1142475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
114302211baeSNick Hibma 		cdevsw_add_done++;
114402211baeSNick Hibma 	}
1145db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000);
11463f54a085SPoul-Henning Kamp 	make_dev(&fd_cdevsw, (fd->fdu << 6),
11473f54a085SPoul-Henning Kamp 		UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1148ada9bd8cSJulian Elischer 
1149671e2ceeSBruce Evans 	/*
1150b2dfb1f9SJustin T. Gibbs 	 * Export the drive to the devstat interface.
1151671e2ceeSBruce Evans 	 */
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 
1306f8ce7dd5SJoerg Wunsch 	/* 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;
148469acd21dSWarner Losh 	};
14853a2f7427SDavid Greenman 
1486d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
1487250300ebSJoerg Wunsch 	if (!(bp->bio_cmd & BIO_FORMAT) &&
1488250300ebSJoerg Wunsch 	    !(bp->bio_flags & BIO_RDSECTID)) {
14898177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1490dc5df763SJoerg Wunsch 			printf(
14916a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
14928177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
14938177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
14948177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
14953a2f7427SDavid Greenman 			goto bad;
14963a2f7427SDavid Greenman 		}
14978177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
14988177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
14998177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15003a2f7427SDavid Greenman 			goto bad;
15013a2f7427SDavid Greenman 		}
15023a2f7427SDavid Greenman 	}
15033a2f7427SDavid Greenman 
15043a2f7427SDavid Greenman 	/*
15053a2f7427SDavid Greenman 	 * Set up block calculations.
15063a2f7427SDavid Greenman 	 */
15078177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1508bb6382faSJoerg Wunsch 		/*
1509bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1510bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1511bb6382faSJoerg Wunsch 		 */
15128177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
15138177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15143a2f7427SDavid Greenman 		goto bad;
15153a2f7427SDavid Greenman 	}
15168177437dSPoul-Henning Kamp 	blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk;
1517bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
15188177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
15198177437dSPoul-Henning Kamp 	if (blknum + (bp->bio_bcount / fdblk) > nblocks) {
1520bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1521bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
15228177437dSPoul-Henning Kamp 			bp->bio_resid = bp->bio_bcount - cando;
1523bb6382faSJoerg Wunsch 			if (cando == 0)
1524bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1525bb6382faSJoerg Wunsch 		} else {
15268177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15278177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
1528bb6382faSJoerg Wunsch 			goto bad;
1529bb6382faSJoerg Wunsch 		}
1530bb6382faSJoerg Wunsch 	}
15318177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
15323a2f7427SDavid Greenman 	s = splbio();
15338177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
15346182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1535b2dfb1f9SJustin T. Gibbs 
1536b2dfb1f9SJustin T. Gibbs 	/* Tell devstat we are starting on the transaction */
1537b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
15385f830ea2SJoerg Wunsch 	device_busy(fd->dev);
1539b2dfb1f9SJustin T. Gibbs 
15406182fdbdSPeter Wemm 	fdstart(fdc);
15413a2f7427SDavid Greenman 	splx(s);
15423a2f7427SDavid Greenman 	return;
15433a2f7427SDavid Greenman 
15443a2f7427SDavid Greenman bad:
15453a2f7427SDavid Greenman 	biodone(bp);
15463a2f7427SDavid Greenman }
15473a2f7427SDavid Greenman 
15485b81b6b3SRodney W. Grimes /***************************************************************\
15495b81b6b3SRodney W. Grimes *				fdstart				*
15505b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
15515b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
15525b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
15535b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
15545b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
15555b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
15565b81b6b3SRodney W. Grimes \***************************************************************/
1557381fe1aaSGarrett Wollman static void
15586182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
15595b81b6b3SRodney W. Grimes {
15605b81b6b3SRodney W. Grimes 	int s;
15615b81b6b3SRodney W. Grimes 
15625b81b6b3SRodney W. Grimes 	s = splbio();
15636182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
15645b81b6b3SRodney W. Grimes 	{
15656182fdbdSPeter Wemm 		fdc_intr(fdc);
15665b81b6b3SRodney W. Grimes 	}
15675b81b6b3SRodney W. Grimes 	splx(s);
15685b81b6b3SRodney W. Grimes }
15695b81b6b3SRodney W. Grimes 
1570381fe1aaSGarrett Wollman static void
15716182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
15725b81b6b3SRodney W. Grimes {
15735c1a1eaeSBruce Evans  	fdc_p fdc;
1574f5f7ba03SJordan K. Hubbard 	int s;
15755b81b6b3SRodney W. Grimes 
15766182fdbdSPeter Wemm 	fdc = xfdc;
15775c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
15785b81b6b3SRodney W. Grimes 
15793a2f7427SDavid Greenman 	/*
15803a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
15813a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
15823a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
15833a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
15843a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
15855c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
15865c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
15875c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
15885c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
15893a2f7427SDavid Greenman 	 */
15903a2f7427SDavid Greenman 	s = splbio();
15915c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
15925c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
15935c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
15946182fdbdSPeter Wemm 	fdc_intr(fdc);
1595f5f7ba03SJordan K. Hubbard 	splx(s);
15965b81b6b3SRodney W. Grimes }
15975b81b6b3SRodney W. Grimes 
15985b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1599381fe1aaSGarrett Wollman static void
16006182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
16015b81b6b3SRodney W. Grimes {
16025b81b6b3SRodney W. Grimes 	int	s;
16033a2f7427SDavid Greenman 
16045b81b6b3SRodney W. Grimes 	s = splbio();
16056182fdbdSPeter Wemm 	fdc_intr(xfdc);
16065b81b6b3SRodney W. Grimes 	splx(s);
16075b81b6b3SRodney W. Grimes }
16085b81b6b3SRodney W. Grimes 
16095b81b6b3SRodney W. Grimes /***********************************************************************\
16105b81b6b3SRodney W. Grimes *                                 fdintr				*
16115b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
16125b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
16135b81b6b3SRodney W. Grimes \***********************************************************************/
1614fe310de8SBruce Evans static void
16156182fdbdSPeter Wemm fdc_intr(void *xfdc)
16165b81b6b3SRodney W. Grimes {
16176182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
16186182fdbdSPeter Wemm 	while(fdstate(fdc))
1619381fe1aaSGarrett Wollman 		;
16205b81b6b3SRodney W. Grimes }
16215b81b6b3SRodney W. Grimes 
162269acd21dSWarner Losh /*
162369acd21dSWarner Losh  * magic pseudo-DMA initialization for YE FDC. Sets count and
162469acd21dSWarner Losh  * direction
162569acd21dSWarner Losh  */
16263b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
16273b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
16283b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
16293b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
16303b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
163169acd21dSWarner Losh 
163269acd21dSWarner Losh /*
163369acd21dSWarner Losh  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
163469acd21dSWarner Losh  */
16354c34deeeSJoerg Wunsch static int
16364c34deeeSJoerg Wunsch fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
163769acd21dSWarner Losh {
163869acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
163969acd21dSWarner Losh 
164021144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
164169acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
164269acd21dSWarner Losh 			fdc->state = PIOREAD;
164369acd21dSWarner Losh 			return(0);
164469acd21dSWarner Losh 		};
16453b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
16463b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16473b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
164869acd21dSWarner Losh 	} else {
16493b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16503b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
16513b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
165269acd21dSWarner Losh 	};
165369acd21dSWarner Losh 	return(1);
165469acd21dSWarner Losh }
165569acd21dSWarner Losh 
16565b81b6b3SRodney W. Grimes /***********************************************************************\
16575b81b6b3SRodney W. Grimes * The controller state machine.						*
16585b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
16595b81b6b3SRodney W. Grimes \***********************************************************************/
16603a2f7427SDavid Greenman static int
16616182fdbdSPeter Wemm fdstate(fdc_p fdc)
16625b81b6b3SRodney W. Grimes {
1663250300ebSJoerg Wunsch 	int read, format, rdsectid, head, i, sec = 0, sectrac, st0, cyl, st3, idf;
1664bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
16655b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
16665b81b6b3SRodney W. Grimes 	fd_p fd;
16678177437dSPoul-Henning Kamp 	register struct bio *bp;
1668b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
16693a2f7427SDavid Greenman 	size_t fdblk;
16705b81b6b3SRodney W. Grimes 
1671e93e63cbSBruce Evans 	bp = fdc->bp;
1672e93e63cbSBruce Evans 	if (bp == NULL) {
16738177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1674e93e63cbSBruce Evans 		if (bp != NULL) {
16758177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1676e93e63cbSBruce Evans 			fdc->bp = bp;
1677e93e63cbSBruce Evans 		}
1678e93e63cbSBruce Evans 	}
1679e93e63cbSBruce Evans 	if (bp == NULL) {
16805b81b6b3SRodney W. Grimes 		/***********************************************\
16815b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
16825b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
16835b81b6b3SRodney W. Grimes 		\***********************************************/
16845b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
16856182fdbdSPeter Wemm 		if (fdc->fd) {
1686b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1687b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
16885b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
16895b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
16905b81b6b3SRodney W. Grimes 		}
16916182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
16925b81b6b3SRodney W. Grimes  		return (0);
16935b81b6b3SRodney W. Grimes 	}
16948177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
16956182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
16963a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1697b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1698b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
16998177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
170056a23089SPoul-Henning Kamp 	if (read)
170156a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
170256a23089SPoul-Henning Kamp 	else
170356a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
17048177437dSPoul-Henning Kamp 	format = bp->bio_cmd & BIO_FORMAT;
1705250300ebSJoerg Wunsch 	rdsectid = bp->bio_flags & BIO_RDSECTID;
1706bb6382faSJoerg Wunsch 	if (format) {
17078177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
1708bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1709bb6382faSJoerg Wunsch 			- (char *)finfo;
1710bb6382faSJoerg Wunsch 	}
1711bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
17128177437dSPoul-Henning Kamp 		blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk +
1713bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1714bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1715bb6382faSJoerg Wunsch 	}
17165b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
17175b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
17185b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
17196182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
17206182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
17215b81b6b3SRodney W. Grimes 	switch (fdc->state)
17225b81b6b3SRodney W. Grimes 	{
17235b81b6b3SRodney W. Grimes 	case DEVIDLE:
17245b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
17255b81b6b3SRodney W. Grimes 		fdc->retry = 0;
17265b81b6b3SRodney W. Grimes 		fd->skip = 0;
17275b81b6b3SRodney W. Grimes 		fdc->fd = fd;
17285b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
17295f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
17303a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
17315b81b6b3SRodney W. Grimes 		/*******************************************************\
17325b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1733dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
17345b81b6b3SRodney W. Grimes 		\*******************************************************/
17356182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
17365b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17375b81b6b3SRodney W. Grimes 			return (0); /* come back later */
17385b81b6b3SRodney W. Grimes 		}
17395b81b6b3SRodney W. Grimes 		/*******************************************************\
17405b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
17415b81b6b3SRodney W. Grimes 		\*******************************************************/
17425b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
17435b81b6b3SRodney W. Grimes 		{
17445b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17456182fdbdSPeter Wemm 			fd_turnon(fd);
17465b81b6b3SRodney W. Grimes 			return (0);
17475b81b6b3SRodney W. Grimes 		}
17485b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
17495b81b6b3SRodney W. Grimes 		{
17506182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
17515b81b6b3SRodney W. Grimes 		}
17525c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17535c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17545c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17555c1a1eaeSBruce Evans 		} else
17565e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
17575b81b6b3SRodney W. Grimes 		break;
17585b81b6b3SRodney W. Grimes 	case DOSEEK:
1759bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
17605b81b6b3SRodney W. Grimes 		{
17615b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
17625b81b6b3SRodney W. Grimes 			break;
17635b81b6b3SRodney W. Grimes 		}
17646182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1765bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1766dc5df763SJoerg Wunsch 			   0))
1767dc8603e3SJoerg Wunsch 		{
1768dc8603e3SJoerg Wunsch 			/*
1769dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1770dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1771dc8603e3SJoerg Wunsch 			 */
1772dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
17736182fdbdSPeter Wemm 			return(retrier(fdc));
1774dc8603e3SJoerg Wunsch 		}
1775dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
17765b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
17775b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17785b81b6b3SRodney W. Grimes 	case SEEKWAIT:
17795b81b6b3SRodney W. Grimes 		/* allow heads to settle */
17806182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
17815b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
17825b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
17835b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
17845b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
17856182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
1786bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
17873a2f7427SDavid Greenman 			do {
17883a2f7427SDavid Greenman 				/*
1789dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1790dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1791dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1792dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1793dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1794dc5df763SJoerg Wunsch 				 *
1795dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1796dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1797dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1798dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1799dc5df763SJoerg Wunsch 				 * real interrupt condition.
1800dc5df763SJoerg Wunsch 				 *
1801dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1802dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1803dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1804dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1805dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1806dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1807dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
18083a2f7427SDavid Greenman 				 */
1809dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1810dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1811dc5df763SJoerg Wunsch 					return 0;
1812dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1813dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1814dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
18153a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1816dc5df763SJoerg Wunsch 
18176182fdbdSPeter Wemm 			if (0 == descyl) {
1818dc5df763SJoerg Wunsch 				int failed = 0;
18193a2f7427SDavid Greenman 				/*
18203a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
18213a2f7427SDavid Greenman 				 * really there
18223a2f7427SDavid Greenman 				 */
1823dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1824dc5df763SJoerg Wunsch 					failed = 1;
18253a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
18263a2f7427SDavid Greenman 					printf(
18273a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
18283a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1829dc5df763SJoerg Wunsch 					failed = 1;
1830dc5df763SJoerg Wunsch 				}
1831dc5df763SJoerg Wunsch 
18326182fdbdSPeter Wemm 				if (failed) {
18333a2f7427SDavid Greenman 					if(fdc->retry < 3)
18343a2f7427SDavid Greenman 						fdc->retry = 3;
18356182fdbdSPeter Wemm 					return (retrier(fdc));
18363a2f7427SDavid Greenman 				}
18373a2f7427SDavid Greenman 			}
1838dc5df763SJoerg Wunsch 
18396182fdbdSPeter Wemm 			if (cyl != descyl) {
18403a2f7427SDavid Greenman 				printf(
18413a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
18422d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1843e5d7d243SBruce Evans 				if (fdc->retry < 3)
1844e5d7d243SBruce Evans 					fdc->retry = 3;
18456182fdbdSPeter Wemm 				return (retrier(fdc));
18465b81b6b3SRodney W. Grimes 			}
18475b81b6b3SRodney W. Grimes 		}
18485b81b6b3SRodney W. Grimes 
1849bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
1850250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
18518177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
18528177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
18535b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
18545b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
18555b81b6b3SRodney W. Grimes 		head = sec / sectrac;
18565b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
18573a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
18583a2f7427SDavid Greenman 
1859250300ebSJoerg Wunsch 		if(format || !(read || rdsectid))
18603a2f7427SDavid Greenman 		{
18613a2f7427SDavid Greenman 			/* make sure the drive is writable */
1862dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1863dc8603e3SJoerg Wunsch 			{
1864dc8603e3SJoerg Wunsch 				/* stuck controller? */
18655f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
186656a23089SPoul-Henning Kamp 					isa_dmadone(idf,
18678177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
18688177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
18695c1a1eaeSBruce Evans 						    fdc->dmachan);
1870dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
18716182fdbdSPeter Wemm 				return (retrier(fdc));
1872dc8603e3SJoerg Wunsch 			}
18733a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
18743a2f7427SDavid Greenman 			{
18753a2f7427SDavid Greenman 				/*
18763a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
18773a2f7427SDavid Greenman 				 * in order to force the current operation
18783a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
18793a2f7427SDavid Greenman 				 * error - all error handling is done
18803a2f7427SDavid Greenman 				 * by the retrier()
18813a2f7427SDavid Greenman 				 */
18823a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
18833a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
18843a2f7427SDavid Greenman 				fdc->status[2] = 0;
18853a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
18863a2f7427SDavid Greenman 				fdc->status[4] = head;
18873a2f7427SDavid Greenman 				fdc->status[5] = sec;
18883a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
18893a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
18903a2f7427SDavid Greenman 				return (1);
18913a2f7427SDavid Greenman 			}
18923a2f7427SDavid Greenman 		}
18935b81b6b3SRodney W. Grimes 
18946182fdbdSPeter Wemm 		if (format) {
18955f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
18965f830ea2SJoerg Wunsch 				/*
18975f830ea2SJoerg Wunsch 				 * This seems to be necessary for
18985f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
18995f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
19005f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
19015f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
19025f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
19035f830ea2SJoerg Wunsch 				 * the place when trying to read them
19045f830ea2SJoerg Wunsch 				 * back.
19055f830ea2SJoerg Wunsch 				 *
19065f830ea2SJoerg Wunsch 				 * Umpf.
19075f830ea2SJoerg Wunsch 				 */
19088177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
19095f830ea2SJoerg Wunsch 
19108177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
19118177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
19128177437dSPoul-Henning Kamp 					bp->bio_bcount);
19135f830ea2SJoerg Wunsch 
19145f830ea2SJoerg Wunsch 			}
1915b39c878eSAndrey A. Chernov 			/* formatting */
19166182fdbdSPeter Wemm 			if(fd_cmd(fdc, 6,  NE7CMD_FORMAT, head << 2 | fdu,
1917dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1918dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1919dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
19206182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
1921dc8603e3SJoerg Wunsch 				/* controller fell over */
19225f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
192356a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19248177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19258177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19265c1a1eaeSBruce Evans 						    fdc->dmachan);
1927dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19286182fdbdSPeter Wemm 				return (retrier(fdc));
1929dc8603e3SJoerg Wunsch 			}
1930250300ebSJoerg Wunsch 		} else if (rdsectid) {
1931250300ebSJoerg Wunsch 			if (fd_cmd(fdc, 2, NE7CMD_READID, head << 2 | fdu, 0)) {
1932250300ebSJoerg Wunsch 				/* controller jamming */
1933250300ebSJoerg Wunsch 				fdc->retry = 6;
1934250300ebSJoerg Wunsch 				return (retrier(fdc));
1935250300ebSJoerg Wunsch 			}
19366182fdbdSPeter Wemm 		} else {
1937250300ebSJoerg Wunsch 			/* read or write operation */
19383b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
193969acd21dSWarner Losh 				/*
194069acd21dSWarner Losh 				 * this seems to be necessary even when
194169acd21dSWarner Losh 				 * reading data
194269acd21dSWarner Losh 				 */
19433b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
194469acd21dSWarner Losh 
194569acd21dSWarner Losh 				/*
194669acd21dSWarner Losh 				 * perform the write pseudo-DMA before
194769acd21dSWarner Losh 				 * the WRITE command is sent
194869acd21dSWarner Losh 				 */
194969acd21dSWarner Losh 				if (!read)
19508177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
19518177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
195269acd21dSWarner Losh 					    fdblk);
195369acd21dSWarner Losh 			}
19546182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
1955dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1956dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1957dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1958dc5df763SJoerg Wunsch 				   head,
1959dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1960dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1961dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1962dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1963dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
19646182fdbdSPeter Wemm 				   0)) {
1965dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
19665f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
196756a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19688177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19698177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19705c1a1eaeSBruce Evans 						    fdc->dmachan);
1971dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19726182fdbdSPeter Wemm 				return (retrier(fdc));
19735b81b6b3SRodney W. Grimes 			}
1974b39c878eSAndrey A. Chernov 		}
1975250300ebSJoerg Wunsch 		if (!rdsectid && (fdc->flags & FDC_NODMA))
197669acd21dSWarner Losh 			/*
197769acd21dSWarner Losh 			 * if this is a read, then simply await interrupt
197869acd21dSWarner Losh 			 * before performing PIO
197969acd21dSWarner Losh 			 */
19808177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
19818177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
19823b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
198369acd21dSWarner Losh 				return(0);      /* will return later */
198469acd21dSWarner Losh 			};
198569acd21dSWarner Losh 
198669acd21dSWarner Losh 		/*
198769acd21dSWarner Losh 		 * write (or format) operation will fall through and
198869acd21dSWarner Losh 		 * await completion interrupt
198969acd21dSWarner Losh 		 */
19905b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
19916182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
19925b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
199369acd21dSWarner Losh 	case PIOREAD:
199469acd21dSWarner Losh 		/*
199569acd21dSWarner Losh 		 * actually perform the PIO read.  The IOCOMPLETE case
199669acd21dSWarner Losh 		 * removes the timeout for us.
199769acd21dSWarner Losh 		 */
19988177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
199969acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
200069acd21dSWarner Losh 		/* FALLTHROUGH */
20015b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
20026182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2003dc5df763SJoerg Wunsch 
20046182fdbdSPeter Wemm 		if (fd_read_status(fdc, fd->fdsu)) {
2005250300ebSJoerg Wunsch 			if (!rdsectid && !(fdc->flags & FDC_NODMA))
20068177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
20078177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
20085c1a1eaeSBruce Evans 					    fdc->dmachan);
2009dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2010dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
20116182fdbdSPeter Wemm 			return (retrier(fdc));
20125b81b6b3SRodney W. Grimes   		}
2013dc5df763SJoerg Wunsch 
20143a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2015dc5df763SJoerg Wunsch 
20163a2f7427SDavid Greenman 		/* FALLTHROUGH */
2017dc5df763SJoerg Wunsch 
20183a2f7427SDavid Greenman 	case IOTIMEDOUT:
2019250300ebSJoerg Wunsch 		if (!rdsectid && !(fdc->flags & FDC_NODMA))
20208177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
20218177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
20226182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
20233a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20243a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2025b39c878eSAndrey A. Chernov                                 /*
20263a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
20273a2f7427SDavid Greenman 				 * and didn't release it in time for the
20283a2f7427SDavid Greenman 				 * next FDC transfer.
20293a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
20303a2f7427SDavid Greenman 				 * count. (vak)
2031b39c878eSAndrey A. Chernov                                  */
2032b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
2033b39c878eSAndrey A. Chernov                                 return (1);
2034b39c878eSAndrey A. Chernov                         }
20353a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
20363a2f7427SDavid Greenman 				&& fdc->retry < 6)
20373a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
20383a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20393a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
20403a2f7427SDavid Greenman 				&& fdc->retry < 3)
20413a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
20426182fdbdSPeter Wemm 			return (retrier(fdc));
20435b81b6b3SRodney W. Grimes 		}
20445b81b6b3SRodney W. Grimes 		/* All OK */
2045250300ebSJoerg Wunsch 		if (rdsectid) {
2046250300ebSJoerg Wunsch 			struct fdc_readid *idp = (struct fdc_readid *)bp->bio_data;
2047250300ebSJoerg Wunsch 			/* copy out ID field contents */
2048250300ebSJoerg Wunsch 			idp->cyl = fdc->status[3];
2049250300ebSJoerg Wunsch 			idp->head = fdc->status[4];
2050250300ebSJoerg Wunsch 			idp->sec = fdc->status[5];
2051250300ebSJoerg Wunsch 			idp->secshift = fdc->status[6];
2052250300ebSJoerg Wunsch 		}
20533a2f7427SDavid Greenman 		fd->skip += fdblk;
2054250300ebSJoerg Wunsch 		if (!rdsectid && !format && fd->skip < bp->bio_bcount - bp->bio_resid) {
20555b81b6b3SRodney W. Grimes 			/* set up next transfer */
20565b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
20576182fdbdSPeter Wemm 		} else {
20585b81b6b3SRodney W. Grimes 			/* ALL DONE */
20595b81b6b3SRodney W. Grimes 			fd->skip = 0;
2060e93e63cbSBruce Evans 			fdc->bp = NULL;
20615f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
2062a468031cSPoul-Henning Kamp 			biofinish(bp, &fd->device_stats, 0);
20635b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
20645b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
20655b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
20665b81b6b3SRodney W. Grimes 		}
20675b81b6b3SRodney W. Grimes 		return (1);
20685b81b6b3SRodney W. Grimes 	case RESETCTLR:
20693a2f7427SDavid Greenman 		fdc_reset(fdc);
20705b81b6b3SRodney W. Grimes 		fdc->retry++;
20715c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
20725c1a1eaeSBruce Evans 		return (0);
20735c1a1eaeSBruce Evans 	case RESETCOMPLETE:
20745c1a1eaeSBruce Evans 		/*
20755c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
20765c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
20775c1a1eaeSBruce Evans 		 */
20780e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
20790e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
20805c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
20815c1a1eaeSBruce Evans 		/* Fall through. */
20825c1a1eaeSBruce Evans 	case STARTRECAL:
20836182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2084dc8603e3SJoerg Wunsch 			/* arrgl */
2085dc8603e3SJoerg Wunsch 			fdc->retry = 6;
20866182fdbdSPeter Wemm 			return (retrier(fdc));
2087dc8603e3SJoerg Wunsch 		}
20885b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
20895b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20905b81b6b3SRodney W. Grimes 	case RECALWAIT:
20915b81b6b3SRodney W. Grimes 		/* allow heads to settle */
20926182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
20935b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
20945b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
20955b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
20963a2f7427SDavid Greenman 		do {
20973a2f7427SDavid Greenman 			/*
2098dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
20993a2f7427SDavid Greenman 			 */
2100dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2101dc5df763SJoerg Wunsch 				return 0;
2102dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2103dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2104dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
21053a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
21063a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
21075b81b6b3SRodney W. Grimes 		{
2108dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2109dc8603e3SJoerg Wunsch 				/*
2110dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
2111dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2112dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2113dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2114dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2115dc8603e3SJoerg Wunsch 				 * time in a line
2116dc8603e3SJoerg Wunsch 				 */
2117dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2118dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
21193a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
21206182fdbdSPeter Wemm 			return (retrier(fdc));
21215b81b6b3SRodney W. Grimes 		}
21225b81b6b3SRodney W. Grimes 		fd->track = 0;
21235b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
21245b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
21255b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21265b81b6b3SRodney W. Grimes 	case MOTORWAIT:
21275b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
21285b81b6b3SRodney W. Grimes 		{
21295b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
21305b81b6b3SRodney W. Grimes 		}
21315c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
21325c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
21335c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
21345c1a1eaeSBruce Evans 		} else {
21355e235068SJordan K. Hubbard 			/*
21365c1a1eaeSBruce Evans 			 * If all motors were off, then the controller was
21375c1a1eaeSBruce Evans 			 * reset, so it has lost track of the current
21385c1a1eaeSBruce Evans 			 * cylinder.  Recalibrate to handle this case.
2139f86e4077SBruce Evans 			 * But first, discard the results of the reset.
21405e235068SJordan K. Hubbard 			 */
2141f86e4077SBruce Evans 			fdc->state = RESETCOMPLETE;
21425c1a1eaeSBruce Evans 		}
21435b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21445b81b6b3SRodney W. Grimes 	default:
2145b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
2146dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
2147a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
21485b81b6b3SRodney W. Grimes 			       fdc->status[0],
21495b81b6b3SRodney W. Grimes 			       fdc->status[1],
21505b81b6b3SRodney W. Grimes 			       fdc->status[2],
21515b81b6b3SRodney W. Grimes 			       fdc->status[3],
21525b81b6b3SRodney W. Grimes 			       fdc->status[4],
21535b81b6b3SRodney W. Grimes 			       fdc->status[5],
21545b81b6b3SRodney W. Grimes 			       fdc->status[6] );
21553a2f7427SDavid Greenman 		else
2156dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2157dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2158dac0f2dbSJoerg Wunsch 		{
2159dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
21605b81b6b3SRodney W. Grimes 			return (0);
21615b81b6b3SRodney W. Grimes 		}
2162dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2163dac0f2dbSJoerg Wunsch 		return (0);
2164dac0f2dbSJoerg Wunsch 	}
2165dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
21665b81b6b3SRodney W. Grimes 	return (1); /* Come back immediatly to new state */
21675b81b6b3SRodney W. Grimes }
21685b81b6b3SRodney W. Grimes 
2169aaf08d94SGarrett Wollman static int
21706182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
21715b81b6b3SRodney W. Grimes {
21728177437dSPoul-Henning Kamp 	struct bio *bp;
21736182fdbdSPeter Wemm 	struct fd_data *fd;
21746182fdbdSPeter Wemm 	int fdu;
21755b81b6b3SRodney W. Grimes 
2176e93e63cbSBruce Evans 	bp = fdc->bp;
21775b81b6b3SRodney W. Grimes 
21786182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
21798177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
21806182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
21816182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
21823a2f7427SDavid Greenman 		goto fail;
21836182fdbdSPeter Wemm 
21846182fdbdSPeter Wemm 	switch (fdc->retry) {
21855b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
21865b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
21875b81b6b3SRodney W. Grimes 		break;
21885b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
21895b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
21905b81b6b3SRodney W. Grimes 		break;
21915b81b6b3SRodney W. Grimes 	case 6:
21925b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
21935b81b6b3SRodney W. Grimes 		break;
21945b81b6b3SRodney W. Grimes 	case 7:
21955b81b6b3SRodney W. Grimes 		break;
21965b81b6b3SRodney W. Grimes 	default:
21973a2f7427SDavid Greenman 	fail:
21985b81b6b3SRodney W. Grimes 		{
21992995d110SJoerg Wunsch 			int printerror = (fd->options & FDOPT_NOERRLOG) == 0;
22002995d110SJoerg Wunsch 
22012995d110SJoerg Wunsch 			if (printerror)
2202cb7e609aSPoul-Henning Kamp 				diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
22033a2f7427SDavid Greenman 					(struct disklabel *)NULL);
22042995d110SJoerg Wunsch 			if (printerror) {
2205dc5df763SJoerg Wunsch 				if (fdc->flags & FDC_STAT_VALID)
2206dc5df763SJoerg Wunsch 				{
2207dc5df763SJoerg Wunsch 					printf(
2208a838d83dSBruce Evans 			" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2209dc5df763SJoerg Wunsch 					       fdc->status[0], NE7_ST0BITS,
2210dc5df763SJoerg Wunsch 					       fdc->status[1], NE7_ST1BITS,
2211dc5df763SJoerg Wunsch 					       fdc->status[2], NE7_ST2BITS,
2212dc5df763SJoerg Wunsch 					       fdc->status[3], fdc->status[4],
2213dc5df763SJoerg Wunsch 					       fdc->status[5]);
2214dc5df763SJoerg Wunsch 				}
2215dc5df763SJoerg Wunsch 			else
2216dc5df763SJoerg Wunsch 					printf(" (No status)\n");
22175b81b6b3SRodney W. Grimes 			}
22182995d110SJoerg Wunsch 		}
22192995d110SJoerg Wunsch 		if ((fd->options & FDOPT_NOERROR) == 0) {
22208177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
22218177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
22228177437dSPoul-Henning Kamp 			bp->bio_resid += bp->bio_bcount - fdc->fd->skip;
22232995d110SJoerg Wunsch 		}
2224e93e63cbSBruce Evans 		fdc->bp = NULL;
22255b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
22265f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
2227a468031cSPoul-Henning Kamp 		biofinish(bp, &fdc->fd->device_stats, 0);
222892ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
22295c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
22305b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
22315b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
223292ed385aSRodney W. Grimes 		return (1);
22335b81b6b3SRodney W. Grimes 	}
22345b81b6b3SRodney W. Grimes 	fdc->retry++;
22355b81b6b3SRodney W. Grimes 	return (1);
22365b81b6b3SRodney W. Grimes }
22375b81b6b3SRodney W. Grimes 
22381fdb6e6cSPoul-Henning Kamp static void
22391fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
22401fdb6e6cSPoul-Henning Kamp {
22411fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
22421fdb6e6cSPoul-Henning Kamp }
22431fdb6e6cSPoul-Henning Kamp 
2244b39c878eSAndrey A. Chernov static int
22454c34deeeSJoerg Wunsch fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
2246b39c878eSAndrey A. Chernov {
2247b39c878eSAndrey A. Chernov  	fdu_t	fdu;
2248b39c878eSAndrey A. Chernov  	fd_p	fd;
2249b39c878eSAndrey A. Chernov 
22501fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2251b39c878eSAndrey A. Chernov 	int rv = 0, s;
22523a2f7427SDavid Greenman 	size_t fdblk;
2253b39c878eSAndrey A. Chernov 
2254b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
22556182fdbdSPeter Wemm 	fd	= devclass_get_softc(fd_devclass, fdu);
22563a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2257b39c878eSAndrey A. Chernov 
2258b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
22591fdb6e6cSPoul-Henning Kamp 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2260b39c878eSAndrey A. Chernov 	if(bp == 0)
22611fdb6e6cSPoul-Henning Kamp 		return ENOMEM;
226282f5379bSJoerg Wunsch 	/*
226382f5379bSJoerg Wunsch 	 * keep the process from being swapped
226482f5379bSJoerg Wunsch 	 */
22652ada239cSPeter Wemm 	PHOLD(p);
22661fdb6e6cSPoul-Henning Kamp 	bzero((void *)bp, sizeof(*bp));
22671fdb6e6cSPoul-Henning Kamp 	bp->bio_cmd = BIO_FORMAT;
2268b39c878eSAndrey A. Chernov 
2269b39c878eSAndrey A. Chernov 	/*
2270b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2271b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2272b39c878eSAndrey A. Chernov 	 */
22731fdb6e6cSPoul-Henning Kamp 	bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
22743a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2275b39c878eSAndrey A. Chernov 
22761fdb6e6cSPoul-Henning Kamp 	bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
22771fdb6e6cSPoul-Henning Kamp 	bp->bio_data = (caddr_t)finfo;
2278b39c878eSAndrey A. Chernov 
2279b39c878eSAndrey A. Chernov 	/* now do the format */
22801fdb6e6cSPoul-Henning Kamp 	bp->bio_dev = dev;
22811fdb6e6cSPoul-Henning Kamp 	bp->bio_done = fdbiodone;
22821fdb6e6cSPoul-Henning Kamp 	fdstrategy(bp);
2283b39c878eSAndrey A. Chernov 
2284b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2285b39c878eSAndrey A. Chernov 	s = splbio();
22861fdb6e6cSPoul-Henning Kamp 	while(!(bp->bio_flags & BIO_DONE)) {
22875e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2288b39c878eSAndrey A. Chernov 		if (rv == EWOULDBLOCK)
2289b39c878eSAndrey A. Chernov 			break;
2290b39c878eSAndrey A. Chernov 	}
2291b39c878eSAndrey A. Chernov 	splx(s);
2292b39c878eSAndrey A. Chernov 
229382f5379bSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2294b39c878eSAndrey A. Chernov 		/* timed out */
2295b39c878eSAndrey A. Chernov 		rv = EIO;
22965f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
229782f5379bSJoerg Wunsch 	}
22981fdb6e6cSPoul-Henning Kamp 	if (bp->bio_flags & BIO_ERROR)
22991fdb6e6cSPoul-Henning Kamp 		rv = bp->bio_error;
230082f5379bSJoerg Wunsch 	/*
230182f5379bSJoerg Wunsch 	 * allow the process to be swapped
230282f5379bSJoerg Wunsch 	 */
23032ada239cSPeter Wemm 	PRELE(p);
2304b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2305b39c878eSAndrey A. Chernov 	return rv;
2306b39c878eSAndrey A. Chernov }
2307b39c878eSAndrey A. Chernov 
2308250300ebSJoerg Wunsch static int
2309250300ebSJoerg Wunsch fdreadid(dev_t dev, struct fdc_readid *idfield)
2310250300ebSJoerg Wunsch {
2311250300ebSJoerg Wunsch  	fdu_t	fdu;
2312250300ebSJoerg Wunsch  	fd_p	fd;
2313250300ebSJoerg Wunsch 
2314250300ebSJoerg Wunsch 	struct bio *bp;
2315250300ebSJoerg Wunsch 	int rv = 0, s;
2316250300ebSJoerg Wunsch 	size_t fdblk;
2317250300ebSJoerg Wunsch 
2318250300ebSJoerg Wunsch  	fdu	= FDUNIT(minor(dev));
2319250300ebSJoerg Wunsch 	fd	= devclass_get_softc(fd_devclass, fdu);
2320250300ebSJoerg Wunsch 	fdblk = 128 << fd->ft->secsize;
2321250300ebSJoerg Wunsch 
2322250300ebSJoerg Wunsch 	/* set up a buffer header for fdstrategy() */
2323250300ebSJoerg Wunsch 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2324250300ebSJoerg Wunsch 	if(bp == 0)
2325250300ebSJoerg Wunsch 		return ENOMEM;
2326250300ebSJoerg Wunsch 
2327250300ebSJoerg Wunsch 	bzero((void *)bp, sizeof(*bp));
2328250300ebSJoerg Wunsch 	bp->bio_cmd = BIO_READ;
2329250300ebSJoerg Wunsch 	bp->bio_flags |= BIO_RDSECTID;
2330250300ebSJoerg Wunsch 
2331250300ebSJoerg Wunsch 	/*
2332250300ebSJoerg Wunsch 	 * calculate a fake blkno, so fdstrategy() would initiate a
2333250300ebSJoerg Wunsch 	 * seek to the requested cylinder
2334250300ebSJoerg Wunsch 	 */
2335250300ebSJoerg Wunsch 	bp->bio_blkno = (idfield->cyl * (fd->ft->sectrac * fd->ft->heads)
2336250300ebSJoerg Wunsch 		+ idfield->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2337250300ebSJoerg Wunsch 	bp->bio_bcount = sizeof(struct fdc_readid);
2338250300ebSJoerg Wunsch 	bp->bio_data = (caddr_t)idfield;
2339250300ebSJoerg Wunsch 	bp->bio_dev = dev;
2340250300ebSJoerg Wunsch 	bp->bio_done = fdbiodone;
2341250300ebSJoerg Wunsch 	fdstrategy(bp);
2342250300ebSJoerg Wunsch 
2343250300ebSJoerg Wunsch 	/* ...and wait for it to complete */
2344250300ebSJoerg Wunsch 	s = splbio();
2345250300ebSJoerg Wunsch 	while(!(bp->bio_flags & BIO_DONE)) {
2346250300ebSJoerg Wunsch 		rv = tsleep((caddr_t)bp, PRIBIO, "fdrdid", 10 * hz);
2347250300ebSJoerg Wunsch 		if (rv == EWOULDBLOCK)
2348250300ebSJoerg Wunsch 			break;
2349250300ebSJoerg Wunsch 	}
2350250300ebSJoerg Wunsch 	splx(s);
2351250300ebSJoerg Wunsch 
2352250300ebSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2353250300ebSJoerg Wunsch 		/* timed out */
2354250300ebSJoerg Wunsch 		rv = EIO;
2355250300ebSJoerg Wunsch 		device_unbusy(fd->dev);
2356250300ebSJoerg Wunsch 	}
2357250300ebSJoerg Wunsch 	if (bp->bio_flags & BIO_ERROR)
2358250300ebSJoerg Wunsch 		rv = bp->bio_error;
2359250300ebSJoerg Wunsch 	free(bp, M_TEMP);
2360250300ebSJoerg Wunsch 	return rv;
2361250300ebSJoerg Wunsch }
2362250300ebSJoerg Wunsch 
2363f5f7ba03SJordan K. Hubbard /*
2364671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2365f5f7ba03SJordan K. Hubbard  */
23665b81b6b3SRodney W. Grimes 
23673e425b96SJulian Elischer static int
23684c34deeeSJoerg Wunsch fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
2369f5f7ba03SJordan K. Hubbard {
23703a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
23716182fdbdSPeter Wemm  	fd_p	fd = devclass_get_softc(fd_devclass, fdu);
23723a2f7427SDavid Greenman 	size_t fdblk;
23733a2f7427SDavid Greenman 
2374f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2375f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
23762995d110SJoerg Wunsch 	struct fdc_status *fsp;
2377250300ebSJoerg Wunsch 	struct fdc_readid *rid;
2378f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
23793a2f7427SDavid Greenman 	int error = 0;
2380f5f7ba03SJordan K. Hubbard 
23813a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2382f5f7ba03SJordan K. Hubbard 
23836182fdbdSPeter Wemm 	switch (cmd) {
2384f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2385f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2386f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
23873a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
23886182fdbdSPeter Wemm 		fdt = fd->ft;
2389f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2390f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2391f5f7ba03SJordan K. Hubbard 
239249ff4debSPoul-Henning Kamp 		if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2393191e1a59SBruce Evans 		    == NULL)
2394f5f7ba03SJordan K. Hubbard 			error = 0;
2395f5f7ba03SJordan K. Hubbard 		else
2396f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2397f5f7ba03SJordan K. Hubbard 
2398f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2399f5f7ba03SJordan K. Hubbard 		break;
2400f5f7ba03SJordan K. Hubbard 
2401f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2402f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2403f5f7ba03SJordan K. Hubbard 			error = EBADF;
2404f5f7ba03SJordan K. Hubbard 		break;
2405f5f7ba03SJordan K. Hubbard 
2406f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2407f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2408f5f7ba03SJordan K. Hubbard 			error = EBADF;
2409f5f7ba03SJordan K. Hubbard 		break;
2410f5f7ba03SJordan K. Hubbard 
2411f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
24126182fdbdSPeter Wemm 		if ((flag & FWRITE) == 0) {
2413f5f7ba03SJordan K. Hubbard 			error = EBADF;
2414f5f7ba03SJordan K. Hubbard 			break;
2415f5f7ba03SJordan K. Hubbard 		}
2416f5f7ba03SJordan K. Hubbard 
2417f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2418f5f7ba03SJordan K. Hubbard 
2419191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2420191e1a59SBruce Evans 					  (u_long)0)) != 0)
2421f5f7ba03SJordan K. Hubbard 			break;
2422f5f7ba03SJordan K. Hubbard 
242349ff4debSPoul-Henning Kamp 		error = writedisklabel(dev, (struct disklabel *)buffer);
2424b39c878eSAndrey A. Chernov 		break;
2425b39c878eSAndrey A. Chernov 	case FD_FORM:
2426b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2427b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2428b39c878eSAndrey A. Chernov 		else if (((struct fd_formb *)addr)->format_version !=
2429b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2430b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2431b39c878eSAndrey A. Chernov 		else
2432b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
2433b39c878eSAndrey A. Chernov 		break;
2434b39c878eSAndrey A. Chernov 
2435b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
24363e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2437f5f7ba03SJordan K. Hubbard 		break;
2438f5f7ba03SJordan K. Hubbard 
24393a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
24403a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2441f711d546SPoul-Henning Kamp 		if (suser(p) != 0)
24423a2f7427SDavid Greenman 			return EPERM;
24433e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
24443a2f7427SDavid Greenman 		break;
24453a2f7427SDavid Greenman 
24463a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
24473e425b96SJulian Elischer 		*(int *)addr = fd->options;
24483a2f7427SDavid Greenman 		break;
24493a2f7427SDavid Greenman 
24503a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
24513e425b96SJulian Elischer 		fd->options = *(int *)addr;
24523a2f7427SDavid Greenman 		break;
24533a2f7427SDavid Greenman 
24542995d110SJoerg Wunsch 	case FD_CLRERR:
24552995d110SJoerg Wunsch 		if (suser(p) != 0)
24562995d110SJoerg Wunsch 			return EPERM;
24572995d110SJoerg Wunsch 		fd->fdc->fdc_errs = 0;
24582995d110SJoerg Wunsch 		break;
24592995d110SJoerg Wunsch 
24602995d110SJoerg Wunsch 	case FD_GSTAT:
24612995d110SJoerg Wunsch 		fsp = (struct fdc_status *)addr;
24622995d110SJoerg Wunsch 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
24632995d110SJoerg Wunsch 			return EINVAL;
24642995d110SJoerg Wunsch 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
24652995d110SJoerg Wunsch 		break;
24662995d110SJoerg Wunsch 
2467250300ebSJoerg Wunsch 	case FD_READID:
2468250300ebSJoerg Wunsch 		rid = (struct fdc_readid *)addr;
2469250300ebSJoerg Wunsch 		if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD)
2470250300ebSJoerg Wunsch 			return EINVAL;
2471250300ebSJoerg Wunsch 		error = fdreadid(dev, rid);
2472250300ebSJoerg Wunsch 		break;
2473250300ebSJoerg Wunsch 
2474f5f7ba03SJordan K. Hubbard 	default:
24753a2f7427SDavid Greenman 		error = ENOTTY;
2476f5f7ba03SJordan K. Hubbard 		break;
2477f5f7ba03SJordan K. Hubbard 	}
2478f5f7ba03SJordan K. Hubbard 	return (error);
2479f5f7ba03SJordan K. Hubbard }
2480