xref: /freebsd/sys/dev/fdc/fdc.c (revision fb919e4d5a2c1baca52ac70d1064f140fffdda71)
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  *
215b81b6b3SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
225b81b6b3SRodney W. Grimes  * modification, are permitted provided that the following conditions
235b81b6b3SRodney W. Grimes  * are met:
245b81b6b3SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
255b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
265b81b6b3SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
275b81b6b3SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
285b81b6b3SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
295b81b6b3SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
305b81b6b3SRodney W. Grimes  *    must display the following acknowledgement:
315b81b6b3SRodney W. Grimes  *	This product includes software developed by the University of
325b81b6b3SRodney W. Grimes  *	California, Berkeley and its contributors.
335b81b6b3SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
345b81b6b3SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
355b81b6b3SRodney W. Grimes  *    without specific prior written permission.
365b81b6b3SRodney W. Grimes  *
375b81b6b3SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
385b81b6b3SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
395b81b6b3SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
405b81b6b3SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
415b81b6b3SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
425b81b6b3SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
435b81b6b3SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
445b81b6b3SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
455b81b6b3SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
465b81b6b3SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
475b81b6b3SRodney W. Grimes  * SUCH DAMAGE.
485b81b6b3SRodney W. Grimes  *
49dc4ff321SRodney W. Grimes  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
50c3aac50fSPeter Wemm  * $FreeBSD$
515b81b6b3SRodney W. Grimes  *
525b81b6b3SRodney W. Grimes  */
535b81b6b3SRodney W. Grimes 
54d2fb4892SJoerg Wunsch #include "opt_fdc.h"
555f830ea2SJoerg Wunsch #include "card.h"
565b81b6b3SRodney W. Grimes 
57b99f0a4aSAndrew Moore #include <sys/param.h>
58b99f0a4aSAndrew Moore #include <sys/systm.h>
599626b608SPoul-Henning Kamp #include <sys/bio.h>
606182fdbdSPeter Wemm #include <sys/bus.h>
616182fdbdSPeter Wemm #include <sys/conf.h>
62b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
63fb919e4dSMark Murray #include <sys/disklabel.h>
646182fdbdSPeter Wemm #include <sys/fcntl.h>
65fb919e4dSMark Murray #include <sys/kernel.h>
66fb919e4dSMark Murray #include <sys/lock.h>
67b99f0a4aSAndrew Moore #include <sys/malloc.h>
686182fdbdSPeter Wemm #include <sys/module.h>
69fb919e4dSMark Murray #include <sys/mutex.h>
703a2f7427SDavid Greenman #include <sys/proc.h>
71b99f0a4aSAndrew Moore #include <sys/syslog.h>
726182fdbdSPeter Wemm 
736182fdbdSPeter Wemm #include <sys/bus.h>
746182fdbdSPeter Wemm #include <machine/bus.h>
756182fdbdSPeter Wemm #include <sys/rman.h>
766182fdbdSPeter Wemm 
776182fdbdSPeter Wemm #include <machine/clock.h>
786182fdbdSPeter Wemm #include <machine/ioctl_fd.h>
796182fdbdSPeter Wemm #include <machine/resource.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
816182fdbdSPeter Wemm 
826182fdbdSPeter Wemm #include <isa/isavar.h>
83a97c75b7SDoug Rabson #include <isa/isareg.h>
84a97c75b7SDoug Rabson #include <isa/fdreg.h>
85a97c75b7SDoug Rabson #include <isa/fdc.h>
86a97c75b7SDoug Rabson #include <isa/rtc.h>
876182fdbdSPeter Wemm 
88b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */
895b81b6b3SRodney W. Grimes 
900722d6abSJoerg Wunsch /* configuration flags */
910722d6abSJoerg Wunsch #define FDC_PRETEND_D0	(1 << 0)	/* pretend drive 0 to be there */
92e34c71eaSJoerg Wunsch #define FDC_NO_FIFO	(1 << 2)	/* do not enable FIFO  */
930722d6abSJoerg Wunsch 
940722d6abSJoerg Wunsch /* internally used only, not really from CMOS: */
950722d6abSJoerg Wunsch #define RTCFDT_144M_PRETENDED	0x1000
960722d6abSJoerg Wunsch 
97dc5df763SJoerg Wunsch /* error returns for fd_cmd() */
98dc5df763SJoerg Wunsch #define FD_FAILED -1
99dc5df763SJoerg Wunsch #define FD_NOT_VALID -2
100dc5df763SJoerg Wunsch #define FDC_ERRMAX	100	/* do not log more */
101dc5df763SJoerg Wunsch 
1026fb89845SKATO Takenori #define NUMTYPES 17
1036fb89845SKATO Takenori #define NUMDENS  (NUMTYPES - 7)
1047ca0641bSAndrey A. Chernov 
1053a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */
106b99f0a4aSAndrew Moore #define F_TAPE_TYPE	0x020	/* bit for fd_types to indicate tape */
107b99f0a4aSAndrew Moore #define NO_TYPE		0	/* must match NO_TYPE in ft.c */
108b99f0a4aSAndrew Moore #define FD_1720         1
109b99f0a4aSAndrew Moore #define FD_1480         2
110b99f0a4aSAndrew Moore #define FD_1440         3
111b99f0a4aSAndrew Moore #define FD_1200         4
112b99f0a4aSAndrew Moore #define FD_820          5
113b99f0a4aSAndrew Moore #define FD_800          6
114b99f0a4aSAndrew Moore #define FD_720          7
115b99f0a4aSAndrew Moore #define FD_360          8
1166fb89845SKATO Takenori #define FD_640          9
1176fb89845SKATO Takenori #define FD_1232         10
118ed2fa05eSAndrey A. Chernov 
1196fb89845SKATO Takenori #define FD_1480in5_25   11
1206fb89845SKATO Takenori #define FD_1440in5_25   12
1216fb89845SKATO Takenori #define FD_820in5_25    13
1226fb89845SKATO Takenori #define FD_800in5_25    14
1236fb89845SKATO Takenori #define FD_720in5_25    15
1246fb89845SKATO Takenori #define FD_360in5_25    16
1256fb89845SKATO Takenori #define FD_640in5_25    17
126b99f0a4aSAndrew Moore 
1277ca0641bSAndrey A. Chernov 
1286f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] =
1295b81b6b3SRodney W. Grimes {
130126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */
131126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */
132126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */
133126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /*  1.2M in HD 5.25/3.5 */
134126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /*  820K in HD 3.5in */
135126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /*  800K in HD 3.5in */
136126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /*  720K in HD 3.5in */
137b0568305SAndrey A. Chernov {  9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /*  360K in DD 5.25in */
1386fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_250KBPS,2,0x50,1 }, /*  640K in DD 5.25in */
1396fb89845SKATO Takenori {  8,3,0xFF,0x35,77,1232,1,FDC_500KBPS,2,0x74,1 }, /* 1.23M in HD 5.25in */
140ed2fa05eSAndrey A. Chernov 
141126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */
142126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */
143126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /*  820K in HD 5.25in */
144126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /*  800K in HD 5.25in */
145126518a1SAndrey A. Chernov {  9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /*  720K in HD 5.25in */
146126518a1SAndrey A. Chernov {  9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /*  360K in HD 5.25in */
1476fb89845SKATO Takenori {  8,2,0xFF,0x2A,80,1280,1,FDC_300KBPS,2,0x50,1 }, /*  640K in HD 5.25in */
1485b81b6b3SRodney W. Grimes };
1495b81b6b3SRodney W. Grimes 
150b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2		/* 2 floppies */
151dc16046fSJoerg Wunsch 
1525b81b6b3SRodney W. Grimes /***********************************************************************\
1535b81b6b3SRodney W. Grimes * Per controller structure.						*
1545b81b6b3SRodney W. Grimes \***********************************************************************/
1556182fdbdSPeter Wemm static devclass_t fdc_devclass;
1565b81b6b3SRodney W. Grimes 
1575b81b6b3SRodney W. Grimes /***********************************************************************\
1585b81b6b3SRodney W. Grimes * Per drive structure.							*
159b99f0a4aSAndrew Moore * N per controller  (DRVS_PER_CTLR)					*
1605b81b6b3SRodney W. Grimes \***********************************************************************/
1616182fdbdSPeter Wemm struct fd_data {
162b99f0a4aSAndrew Moore 	struct	fdc_data *fdc;	/* pointer to controller structure */
1635b81b6b3SRodney W. Grimes 	int	fdsu;		/* this units number on this controller */
1643a2f7427SDavid Greenman 	int	type;		/* Drive type (FD_1440...) */
1655b81b6b3SRodney W. Grimes 	struct	fd_type *ft;	/* pointer to the type descriptor */
1665b81b6b3SRodney W. Grimes 	int	flags;
1675b81b6b3SRodney W. Grimes #define	FD_OPEN		0x01	/* it's open		*/
1685b81b6b3SRodney W. Grimes #define	FD_ACTIVE	0x02	/* it's active		*/
1695b81b6b3SRodney W. Grimes #define	FD_MOTOR	0x04	/* motor should be on	*/
1705b81b6b3SRodney W. Grimes #define	FD_MOTOR_WAIT	0x08	/* motor coming up	*/
1715b81b6b3SRodney W. Grimes 	int	skip;
1725b81b6b3SRodney W. Grimes 	int	hddrv;
173dc5df763SJoerg Wunsch #define FD_NO_TRACK -2
1745b81b6b3SRodney W. Grimes 	int	track;		/* where we think the head is */
1753a2f7427SDavid Greenman 	int	options;	/* user configurable options, see ioctl_fd.h */
17602a19910SJustin T. Gibbs 	struct	callout_handle toffhandle;
17702a19910SJustin T. Gibbs 	struct	callout_handle tohandle;
178b2dfb1f9SJustin T. Gibbs 	struct	devstat device_stats;
1796182fdbdSPeter Wemm 	device_t dev;
1806182fdbdSPeter Wemm 	fdu_t	fdu;
1816182fdbdSPeter Wemm };
18237286586SPeter Wemm 
18337286586SPeter Wemm struct fdc_ivars {
18437286586SPeter Wemm 	int	fdunit;
18537286586SPeter Wemm };
1866182fdbdSPeter Wemm static devclass_t fd_devclass;
1875b81b6b3SRodney W. Grimes 
1885b81b6b3SRodney W. Grimes /***********************************************************************\
1895b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used:		*
1905b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question	*
1915b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller		*
1925b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number					*
1935b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number				*
1945b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit)	*
1955b81b6b3SRodney W. Grimes \***********************************************************************/
196b99f0a4aSAndrew Moore 
1973a2f7427SDavid Greenman /* needed for ft driver, thus exported */
1986182fdbdSPeter Wemm int in_fdc(struct fdc_data *);
1996182fdbdSPeter Wemm int out_fdc(struct fdc_data *, int);
2003a2f7427SDavid Greenman 
2013a2f7427SDavid Greenman /* internal functions */
2026182fdbdSPeter Wemm static	void fdc_intr(void *);
2036182fdbdSPeter Wemm static void set_motor(struct fdc_data *, int, int);
2043a2f7427SDavid Greenman #  define TURNON 1
2053a2f7427SDavid Greenman #  define TURNOFF 0
2063a2f7427SDavid Greenman static timeout_t fd_turnoff;
2073a2f7427SDavid Greenman static timeout_t fd_motor_on;
2086182fdbdSPeter Wemm static void fd_turnon(struct fd_data *);
2093a2f7427SDavid Greenman static void fdc_reset(fdc_p);
2106182fdbdSPeter Wemm static int fd_in(struct fdc_data *, int *);
2116182fdbdSPeter Wemm static void fdstart(struct fdc_data *);
2125c1a1eaeSBruce Evans static timeout_t fd_iotimeout;
2133a2f7427SDavid Greenman static timeout_t fd_pseudointr;
2146182fdbdSPeter Wemm static int fdstate(struct fdc_data *);
2156182fdbdSPeter Wemm static int retrier(struct fdc_data *);
2163a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *);
2173a2f7427SDavid Greenman 
218d66c374fSTor Egge static int enable_fifo(fdc_p fdc);
219d66c374fSTor Egge 
220d66c374fSTor Egge static int fifo_threshold = 8;	/* XXX: should be accessible via sysctl */
221d66c374fSTor Egge 
222aaf08d94SGarrett Wollman 
2235b81b6b3SRodney W. Grimes #define DEVIDLE		0
2245b81b6b3SRodney W. Grimes #define FINDWORK	1
2255b81b6b3SRodney W. Grimes #define	DOSEEK		2
2265b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 	3
2275b81b6b3SRodney W. Grimes #define	IOCOMPLETE	4
2285b81b6b3SRodney W. Grimes #define RECALCOMPLETE	5
2295b81b6b3SRodney W. Grimes #define	STARTRECAL	6
2305b81b6b3SRodney W. Grimes #define	RESETCTLR	7
2315b81b6b3SRodney W. Grimes #define	SEEKWAIT	8
2325b81b6b3SRodney W. Grimes #define	RECALWAIT	9
2335b81b6b3SRodney W. Grimes #define	MOTORWAIT	10
2345b81b6b3SRodney W. Grimes #define	IOTIMEDOUT	11
2355c1a1eaeSBruce Evans #define	RESETCOMPLETE	12
23669acd21dSWarner Losh #define PIOREAD		13
2375b81b6b3SRodney W. Grimes 
238d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
239cba2a7c6SBruce Evans static char const * const fdstates[] =
2405b81b6b3SRodney W. Grimes {
2415b81b6b3SRodney W. Grimes "DEVIDLE",
2425b81b6b3SRodney W. Grimes "FINDWORK",
2435b81b6b3SRodney W. Grimes "DOSEEK",
2445b81b6b3SRodney W. Grimes "SEEKCOMPLETE",
2455b81b6b3SRodney W. Grimes "IOCOMPLETE",
2465b81b6b3SRodney W. Grimes "RECALCOMPLETE",
2475b81b6b3SRodney W. Grimes "STARTRECAL",
2485b81b6b3SRodney W. Grimes "RESETCTLR",
2495b81b6b3SRodney W. Grimes "SEEKWAIT",
2505b81b6b3SRodney W. Grimes "RECALWAIT",
2515b81b6b3SRodney W. Grimes "MOTORWAIT",
2525c1a1eaeSBruce Evans "IOTIMEDOUT",
2535c1a1eaeSBruce Evans "RESETCOMPLETE",
254ff9607b0SBruce Evans "PIOREAD",
2555b81b6b3SRodney W. Grimes };
2565b81b6b3SRodney W. Grimes 
2573a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */
258cba2a7c6SBruce Evans static int volatile fd_debug = 0;
2595b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg)
2605b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2)
261d2fb4892SJoerg Wunsch #else /* FDC_DEBUG */
2625b81b6b3SRodney W. Grimes #define TRACE0(arg)
2635b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2)
264d2fb4892SJoerg Wunsch #endif /* FDC_DEBUG */
2655b81b6b3SRodney W. Grimes 
266427ccf00SDoug Rabson static void
267427ccf00SDoug Rabson fdout_wr(fdc_p fdc, u_int8_t v)
268427ccf00SDoug Rabson {
269427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v);
270427ccf00SDoug Rabson }
271427ccf00SDoug Rabson 
272427ccf00SDoug Rabson static u_int8_t
273427ccf00SDoug Rabson fdsts_rd(fdc_p fdc)
274427ccf00SDoug Rabson {
275427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off);
276427ccf00SDoug Rabson }
277427ccf00SDoug Rabson 
278427ccf00SDoug Rabson static void
279427ccf00SDoug Rabson fddata_wr(fdc_p fdc, u_int8_t v)
280427ccf00SDoug Rabson {
281427ccf00SDoug Rabson 	bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v);
282427ccf00SDoug Rabson }
283427ccf00SDoug Rabson 
284427ccf00SDoug Rabson static u_int8_t
285427ccf00SDoug Rabson fddata_rd(fdc_p fdc)
286427ccf00SDoug Rabson {
287427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off);
288427ccf00SDoug Rabson }
289427ccf00SDoug Rabson 
290427ccf00SDoug Rabson static void
2915f830ea2SJoerg Wunsch fdctl_wr_isa(fdc_p fdc, u_int8_t v)
292427ccf00SDoug Rabson {
293427ccf00SDoug Rabson 	bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v);
294427ccf00SDoug Rabson }
295427ccf00SDoug Rabson 
29658c9d623SPeter Wemm #if NCARD > 0
2975f830ea2SJoerg Wunsch static void
2985f830ea2SJoerg Wunsch fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v)
2995f830ea2SJoerg Wunsch {
3005f830ea2SJoerg Wunsch 	bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v);
3015f830ea2SJoerg Wunsch }
30258c9d623SPeter Wemm #endif
3035f830ea2SJoerg Wunsch 
304427ccf00SDoug Rabson #if 0
305427ccf00SDoug Rabson 
306427ccf00SDoug Rabson static u_int8_t
307427ccf00SDoug Rabson fdin_rd(fdc_p fdc)
308427ccf00SDoug Rabson {
309427ccf00SDoug Rabson 	return bus_space_read_1(fdc->portt, fdc->porth, FDIN);
310427ccf00SDoug Rabson }
311427ccf00SDoug Rabson 
312427ccf00SDoug Rabson #endif
313427ccf00SDoug Rabson 
31487f6c662SJulian Elischer static	d_open_t	Fdopen;	/* NOTE, not fdopen */
31587f6c662SJulian Elischer static	d_close_t	fdclose;
31687f6c662SJulian Elischer static	d_ioctl_t	fdioctl;
31787f6c662SJulian Elischer static	d_strategy_t	fdstrategy;
31887f6c662SJulian Elischer 
31987f6c662SJulian Elischer #define CDEV_MAJOR 9
320f7ea2f55SJulian Elischer 
3214e2f199eSPoul-Henning Kamp static struct cdevsw fd_cdevsw = {
3224e2f199eSPoul-Henning Kamp 	/* open */	Fdopen,
3234e2f199eSPoul-Henning Kamp 	/* close */	fdclose,
3244e2f199eSPoul-Henning Kamp 	/* read */	physread,
3254e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
3264e2f199eSPoul-Henning Kamp 	/* ioctl */	fdioctl,
3274e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
3284e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
3294e2f199eSPoul-Henning Kamp 	/* strategy */	fdstrategy,
3304e2f199eSPoul-Henning Kamp 	/* name */	"fd",
3314e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
3324e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
3334e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
3344e2f199eSPoul-Henning Kamp 	/* flags */	D_DISK,
3354e2f199eSPoul-Henning Kamp };
3364e2f199eSPoul-Henning Kamp 
337dc5df763SJoerg Wunsch static int
3386182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
339dc5df763SJoerg Wunsch {
3406182fdbdSPeter Wemm 	fdc->fdc_errs++;
34116b04b6aSJoerg Wunsch 	if (s) {
342b6e5f28eSPeter Wemm 		if (fdc->fdc_errs < FDC_ERRMAX)
343b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "%s", s);
344b6e5f28eSPeter Wemm 		else if (fdc->fdc_errs == FDC_ERRMAX)
345b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev, "too many errors, not "
346b6e5f28eSPeter Wemm 						    "logging any more\n");
34716b04b6aSJoerg Wunsch 	}
348dc5df763SJoerg Wunsch 
349dc5df763SJoerg Wunsch 	return FD_FAILED;
350dc5df763SJoerg Wunsch }
351dc5df763SJoerg Wunsch 
352dc5df763SJoerg Wunsch /*
353dc5df763SJoerg Wunsch  * fd_cmd: Send a command to the chip.  Takes a varargs with this structure:
354dc5df763SJoerg Wunsch  * Unit number,
355dc5df763SJoerg Wunsch  * # of output bytes, output bytes as ints ...,
356dc5df763SJoerg Wunsch  * # of input bytes, input bytes as ints ...
357dc5df763SJoerg Wunsch  */
3586f4e0bebSPoul-Henning Kamp static int
3596182fdbdSPeter Wemm fd_cmd(struct fdc_data *fdc, int n_out, ...)
360dc5df763SJoerg Wunsch {
361dc5df763SJoerg Wunsch 	u_char cmd;
362dc5df763SJoerg Wunsch 	int n_in;
363dc5df763SJoerg Wunsch 	int n;
364dc5df763SJoerg Wunsch 	va_list ap;
365dc5df763SJoerg Wunsch 
366dc5df763SJoerg Wunsch 	va_start(ap, n_out);
367dc5df763SJoerg Wunsch 	cmd = (u_char)(va_arg(ap, int));
368dc5df763SJoerg Wunsch 	va_end(ap);
369dc5df763SJoerg Wunsch 	va_start(ap, n_out);
370dc5df763SJoerg Wunsch 	for (n = 0; n < n_out; n++)
371dc5df763SJoerg Wunsch 	{
3726182fdbdSPeter Wemm 		if (out_fdc(fdc, va_arg(ap, int)) < 0)
373dc5df763SJoerg Wunsch 		{
374dc5df763SJoerg Wunsch 			char msg[50];
3752127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
376dc5df763SJoerg Wunsch 				"cmd %x failed at out byte %d of %d\n",
377dc5df763SJoerg Wunsch 				cmd, n + 1, n_out);
3786182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
379dc5df763SJoerg Wunsch 		}
380dc5df763SJoerg Wunsch 	}
381dc5df763SJoerg Wunsch 	n_in = va_arg(ap, int);
382dc5df763SJoerg Wunsch 	for (n = 0; n < n_in; n++)
383dc5df763SJoerg Wunsch 	{
384dc5df763SJoerg Wunsch 		int *ptr = va_arg(ap, int *);
3856182fdbdSPeter Wemm 		if (fd_in(fdc, ptr) < 0)
386dc5df763SJoerg Wunsch 		{
387dc5df763SJoerg Wunsch 			char msg[50];
3882127f260SArchie Cobbs 			snprintf(msg, sizeof(msg),
389dc5df763SJoerg Wunsch 				"cmd %02x failed at in byte %d of %d\n",
390dc5df763SJoerg Wunsch 				cmd, n + 1, n_in);
3916182fdbdSPeter Wemm 			return fdc_err(fdc, msg);
392dc5df763SJoerg Wunsch 		}
393dc5df763SJoerg Wunsch 	}
394dc5df763SJoerg Wunsch 
395dc5df763SJoerg Wunsch 	return 0;
396dc5df763SJoerg Wunsch }
397dc5df763SJoerg Wunsch 
3986f4e0bebSPoul-Henning Kamp static int
399d66c374fSTor Egge enable_fifo(fdc_p fdc)
400d66c374fSTor Egge {
401d66c374fSTor Egge 	int i, j;
402d66c374fSTor Egge 
403d66c374fSTor Egge 	if ((fdc->flags & FDC_HAS_FIFO) == 0) {
404d66c374fSTor Egge 
405d66c374fSTor Egge 		/*
406d66c374fSTor Egge 		 * XXX:
407d66c374fSTor Egge 		 * Cannot use fd_cmd the normal way here, since
408d66c374fSTor Egge 		 * this might be an invalid command. Thus we send the
409d66c374fSTor Egge 		 * first byte, and check for an early turn of data directon.
410d66c374fSTor Egge 		 */
411d66c374fSTor Egge 
4126182fdbdSPeter Wemm 		if (out_fdc(fdc, I8207X_CONFIGURE) < 0)
4136182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
414d66c374fSTor Egge 
415d66c374fSTor Egge 		/* If command is invalid, return */
416d66c374fSTor Egge 		j = 100000;
417427ccf00SDoug Rabson 		while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM))
418d66c374fSTor Egge 		       != NE7_RQM && j-- > 0)
419d66c374fSTor Egge 			if (i == (NE7_DIO | NE7_RQM)) {
420d66c374fSTor Egge 				fdc_reset(fdc);
421d66c374fSTor Egge 				return FD_FAILED;
422d66c374fSTor Egge 			}
423d66c374fSTor Egge 		if (j<0 ||
4246182fdbdSPeter Wemm 		    fd_cmd(fdc, 3,
425d66c374fSTor Egge 			   0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) {
426d66c374fSTor Egge 			fdc_reset(fdc);
4276182fdbdSPeter Wemm 			return fdc_err(fdc, "Enable FIFO failed\n");
428d66c374fSTor Egge 		}
429d66c374fSTor Egge 		fdc->flags |= FDC_HAS_FIFO;
430d66c374fSTor Egge 		return 0;
431d66c374fSTor Egge 	}
4326182fdbdSPeter Wemm 	if (fd_cmd(fdc, 4,
433d66c374fSTor Egge 		   I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0)
4346182fdbdSPeter Wemm 		return fdc_err(fdc, "Re-enable FIFO failed\n");
435d66c374fSTor Egge 	return 0;
436d66c374fSTor Egge }
437d66c374fSTor Egge 
438d66c374fSTor Egge static int
439dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p)
440dc5df763SJoerg Wunsch {
441dc5df763SJoerg Wunsch 	int st3;
442dc5df763SJoerg Wunsch 
4436182fdbdSPeter Wemm 	if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3))
444dc5df763SJoerg Wunsch 	{
4456182fdbdSPeter Wemm 		return fdc_err(fdc, "Sense Drive Status failed\n");
446dc5df763SJoerg Wunsch 	}
447dc5df763SJoerg Wunsch 	if (st3p)
448dc5df763SJoerg Wunsch 		*st3p = st3;
449dc5df763SJoerg Wunsch 
450dc5df763SJoerg Wunsch 	return 0;
451dc5df763SJoerg Wunsch }
452dc5df763SJoerg Wunsch 
4536f4e0bebSPoul-Henning Kamp static int
454dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp)
455dc5df763SJoerg Wunsch {
4566182fdbdSPeter Wemm 	int cyl, st0, ret;
457dc5df763SJoerg Wunsch 
4586182fdbdSPeter Wemm 	ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
4596182fdbdSPeter Wemm 	if (ret) {
4606182fdbdSPeter Wemm 		(void)fdc_err(fdc,
461dc5df763SJoerg Wunsch 			      "sense intr err reading stat reg 0\n");
462dc5df763SJoerg Wunsch 		return ret;
463dc5df763SJoerg Wunsch 	}
464dc5df763SJoerg Wunsch 
465dc5df763SJoerg Wunsch 	if (st0p)
466dc5df763SJoerg Wunsch 		*st0p = st0;
467dc5df763SJoerg Wunsch 
4686182fdbdSPeter Wemm 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
469dc5df763SJoerg Wunsch 		/*
470dc5df763SJoerg Wunsch 		 * There doesn't seem to have been an interrupt.
471dc5df763SJoerg Wunsch 		 */
472dc5df763SJoerg Wunsch 		return FD_NOT_VALID;
473dc5df763SJoerg Wunsch 	}
474dc5df763SJoerg Wunsch 
4756182fdbdSPeter Wemm 	if (fd_in(fdc, &cyl) < 0) {
4766182fdbdSPeter Wemm 		return fdc_err(fdc, "can't get cyl num\n");
477dc5df763SJoerg Wunsch 	}
478dc5df763SJoerg Wunsch 
479dc5df763SJoerg Wunsch 	if (cylp)
480dc5df763SJoerg Wunsch 		*cylp = cyl;
481dc5df763SJoerg Wunsch 
482dc5df763SJoerg Wunsch 	return 0;
483dc5df763SJoerg Wunsch }
484dc5df763SJoerg Wunsch 
485dc5df763SJoerg Wunsch 
4866f4e0bebSPoul-Henning Kamp static int
487dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu)
488dc5df763SJoerg Wunsch {
489dc5df763SJoerg Wunsch 	int i, ret;
490b5e8ce9fSBruce Evans 
4916182fdbdSPeter Wemm 	for (i = 0; i < 7; i++) {
492b5e8ce9fSBruce Evans 		/*
493b5e8ce9fSBruce Evans 		 * XXX types are poorly chosen.  Only bytes can by read
494a838d83dSBruce Evans 		 * from the hardware, but fdc->status[] wants u_ints and
495b5e8ce9fSBruce Evans 		 * fd_in() gives ints.
496b5e8ce9fSBruce Evans 		 */
497b5e8ce9fSBruce Evans 		int status;
498b5e8ce9fSBruce Evans 
4996182fdbdSPeter Wemm 		ret = fd_in(fdc, &status);
500b5e8ce9fSBruce Evans 		fdc->status[i] = status;
501b5e8ce9fSBruce Evans 		if (ret != 0)
502dc5df763SJoerg Wunsch 			break;
503dc5df763SJoerg Wunsch 	}
504dc5df763SJoerg Wunsch 
505dc5df763SJoerg Wunsch 	if (ret == 0)
506dc5df763SJoerg Wunsch 		fdc->flags |= FDC_STAT_VALID;
507dc5df763SJoerg Wunsch 	else
508dc5df763SJoerg Wunsch 		fdc->flags &= ~FDC_STAT_VALID;
509dc5df763SJoerg Wunsch 
510dc5df763SJoerg Wunsch 	return ret;
511dc5df763SJoerg Wunsch }
512dc5df763SJoerg Wunsch 
5135b81b6b3SRodney W. Grimes /****************************************************************************/
5145b81b6b3SRodney W. Grimes /*                      autoconfiguration stuff                             */
5155b81b6b3SRodney W. Grimes /****************************************************************************/
516dc5df763SJoerg Wunsch 
5173a2f7427SDavid Greenman static int
51837286586SPeter Wemm fdc_alloc_resources(struct fdc_data *fdc)
5195b81b6b3SRodney W. Grimes {
52037286586SPeter Wemm 	device_t dev;
5215f830ea2SJoerg Wunsch 	int ispnp, ispcmcia;
5225b81b6b3SRodney W. Grimes 
52337286586SPeter Wemm 	dev = fdc->fdc_dev;
5245f830ea2SJoerg Wunsch 	ispnp = (fdc->flags & FDC_ISPNP) != 0;
5255f830ea2SJoerg Wunsch 	ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0;
5266182fdbdSPeter Wemm 	fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0;
5276182fdbdSPeter Wemm 	fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0;
5286182fdbdSPeter Wemm 
529b6e5f28eSPeter Wemm 	/*
5305f830ea2SJoerg Wunsch 	 * On standard ISA, we don't just use an 8 port range
5315f830ea2SJoerg Wunsch 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
5325f830ea2SJoerg Wunsch 	 * register at 0x3f6.
5335f830ea2SJoerg Wunsch 	 *
534b6e5f28eSPeter Wemm 	 * Isn't PC hardware wonderful.
5355f830ea2SJoerg Wunsch 	 *
5365f830ea2SJoerg Wunsch 	 * The Y-E Data PCMCIA FDC doesn't have this problem, it
5375f830ea2SJoerg Wunsch 	 * uses the register with offset 6 for pseudo-DMA, and the
5385f830ea2SJoerg Wunsch 	 * one with offset 7 as control register.
539b6e5f28eSPeter Wemm 	 */
5406182fdbdSPeter Wemm 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
5416182fdbdSPeter Wemm 					     &fdc->rid_ioport, 0ul, ~0ul,
5425f830ea2SJoerg Wunsch 					     ispcmcia ? 8 : (ispnp ? 1 : 6),
5435f830ea2SJoerg Wunsch 					     RF_ACTIVE);
5446182fdbdSPeter Wemm 	if (fdc->res_ioport == 0) {
545427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve I/O port range\n");
54637286586SPeter Wemm 		return ENXIO;
5476182fdbdSPeter Wemm 	}
548427ccf00SDoug Rabson 	fdc->portt = rman_get_bustag(fdc->res_ioport);
549427ccf00SDoug Rabson 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
550427ccf00SDoug Rabson 
5515f830ea2SJoerg Wunsch 	if (!ispcmcia) {
552427ccf00SDoug Rabson 		/*
5535f830ea2SJoerg Wunsch 		 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7
5545f830ea2SJoerg Wunsch 		 * and some at 0x3f0-0x3f5,0x3f7. We detect the former
5555f830ea2SJoerg Wunsch 		 * by checking the size and adjust the port address
5565f830ea2SJoerg Wunsch 		 * accordingly.
557b6e5f28eSPeter Wemm 		 */
558b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4)
559b6e5f28eSPeter Wemm 			fdc->port_off = -2;
560b6e5f28eSPeter Wemm 
561b6e5f28eSPeter Wemm 		/*
5625f830ea2SJoerg Wunsch 		 * Register the control port range as rid 1 if it
5635f830ea2SJoerg Wunsch 		 * isn't there already. Most PnP BIOSen will have
5645f830ea2SJoerg Wunsch 		 * already done this but non-PnP configurations don't.
565a2639a18SPeter Wemm 		 *
5665f830ea2SJoerg Wunsch 		 * And some (!!) report 0x3f2-0x3f5 and completely
5675f830ea2SJoerg Wunsch 		 * leave out the control register!  It seems that some
5685f830ea2SJoerg Wunsch 		 * non-antique controller chips have a different
5695f830ea2SJoerg Wunsch 		 * method of programming the transfer speed which
5705f830ea2SJoerg Wunsch 		 * doesn't require the control register, but it's
5715f830ea2SJoerg Wunsch 		 * mighty bogus as the chip still responds to the
5725f830ea2SJoerg Wunsch 		 * address for the control register.
573427ccf00SDoug Rabson 		 */
574b6e5f28eSPeter Wemm 		if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) {
575b9da888fSPeter Wemm 			u_long ctlstart;
576b9da888fSPeter Wemm 
577b6e5f28eSPeter Wemm 			/* Find the control port, usually 0x3f7 */
5785f830ea2SJoerg Wunsch 			ctlstart = rman_get_start(fdc->res_ioport) +
5795f830ea2SJoerg Wunsch 				fdc->port_off + 7;
580b6e5f28eSPeter Wemm 
581b6e5f28eSPeter Wemm 			bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1);
582b9da888fSPeter Wemm 		}
583b6e5f28eSPeter Wemm 
584b6e5f28eSPeter Wemm 		/*
585b6e5f28eSPeter Wemm 		 * Now (finally!) allocate the control port.
586b6e5f28eSPeter Wemm 		 */
587427ccf00SDoug Rabson 		fdc->rid_ctl = 1;
5885f830ea2SJoerg Wunsch 		fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT,
5895f830ea2SJoerg Wunsch 						  &fdc->rid_ctl,
590b6e5f28eSPeter Wemm 						  0ul, ~0ul, 1, RF_ACTIVE);
591427ccf00SDoug Rabson 		if (fdc->res_ctl == 0) {
5925f830ea2SJoerg Wunsch 			device_printf(dev,
5935f830ea2SJoerg Wunsch 				      "cannot reserve control I/O port range\n");
59437286586SPeter Wemm 			return ENXIO;
595427ccf00SDoug Rabson 		}
596427ccf00SDoug Rabson 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
597427ccf00SDoug Rabson 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
5985f830ea2SJoerg Wunsch 	}
5996182fdbdSPeter Wemm 
6006182fdbdSPeter Wemm 	fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ,
6016182fdbdSPeter Wemm 					  &fdc->rid_irq, 0ul, ~0ul, 1,
6026182fdbdSPeter Wemm 					  RF_ACTIVE);
6036182fdbdSPeter Wemm 	if (fdc->res_irq == 0) {
604427ccf00SDoug Rabson 		device_printf(dev, "cannot reserve interrupt line\n");
60537286586SPeter Wemm 		return ENXIO;
6066182fdbdSPeter Wemm 	}
6075f830ea2SJoerg Wunsch 
6085f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
6096182fdbdSPeter Wemm 		fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ,
6106182fdbdSPeter Wemm 						  &fdc->rid_drq, 0ul, ~0ul, 1,
6116182fdbdSPeter Wemm 						  RF_ACTIVE);
6126182fdbdSPeter Wemm 		if (fdc->res_drq == 0) {
613427ccf00SDoug Rabson 			device_printf(dev, "cannot reserve DMA request line\n");
61437286586SPeter Wemm 			return ENXIO;
6156182fdbdSPeter Wemm 		}
6166182fdbdSPeter Wemm 		fdc->dmachan = fdc->res_drq->r_start;
6175f830ea2SJoerg Wunsch 	}
61837286586SPeter Wemm 
61937286586SPeter Wemm 	return 0;
62037286586SPeter Wemm }
62137286586SPeter Wemm 
62237286586SPeter Wemm static void
62337286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
62437286586SPeter Wemm {
62537286586SPeter Wemm 	device_t dev;
62637286586SPeter Wemm 
62737286586SPeter Wemm 	dev = fdc->fdc_dev;
62837286586SPeter Wemm 	if (fdc->res_irq != 0) {
62937286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
63037286586SPeter Wemm 					fdc->res_irq);
63137286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
63237286586SPeter Wemm 				     fdc->res_irq);
63337286586SPeter Wemm 	}
63437286586SPeter Wemm 	if (fdc->res_ctl != 0) {
63537286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
63637286586SPeter Wemm 					fdc->res_ctl);
63737286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
63837286586SPeter Wemm 				     fdc->res_ctl);
63937286586SPeter Wemm 	}
64037286586SPeter Wemm 	if (fdc->res_ioport != 0) {
64137286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
64237286586SPeter Wemm 					fdc->res_ioport);
64337286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
64437286586SPeter Wemm 				     fdc->res_ioport);
64537286586SPeter Wemm 	}
64637286586SPeter Wemm 	if (fdc->res_drq != 0) {
64737286586SPeter Wemm 		bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
64837286586SPeter Wemm 					fdc->res_drq);
64937286586SPeter Wemm 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
65037286586SPeter Wemm 				     fdc->res_drq);
65137286586SPeter Wemm 	}
65237286586SPeter Wemm }
65337286586SPeter Wemm 
65437286586SPeter Wemm /****************************************************************************/
65537286586SPeter Wemm /*                      autoconfiguration stuff                             */
65637286586SPeter Wemm /****************************************************************************/
65737286586SPeter Wemm 
65837286586SPeter Wemm static struct isa_pnp_id fdc_ids[] = {
65937286586SPeter Wemm 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
66037286586SPeter Wemm 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
66137286586SPeter Wemm 	{0}
66237286586SPeter Wemm };
66337286586SPeter Wemm 
66437286586SPeter Wemm static int
6653aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
66637286586SPeter Wemm {
66737286586SPeter Wemm 	struct fdc_ivars *ivars = device_get_ivars(child);
66837286586SPeter Wemm 
66937286586SPeter Wemm 	switch (which) {
67037286586SPeter Wemm 	case FDC_IVAR_FDUNIT:
67137286586SPeter Wemm 		*result = ivars->fdunit;
67237286586SPeter Wemm 		break;
67337286586SPeter Wemm 	default:
67437286586SPeter Wemm 		return ENOENT;
67537286586SPeter Wemm 	}
67637286586SPeter Wemm 	return 0;
67737286586SPeter Wemm }
67837286586SPeter Wemm 
67937286586SPeter Wemm /*
68037286586SPeter Wemm  * fdc controller section.
68137286586SPeter Wemm  */
68237286586SPeter Wemm static int
68337286586SPeter Wemm fdc_probe(device_t dev)
68437286586SPeter Wemm {
68537286586SPeter Wemm 	int	error, ic_type;
68637286586SPeter Wemm 	struct	fdc_data *fdc;
68737286586SPeter Wemm 
68837286586SPeter Wemm 	fdc = device_get_softc(dev);
68937286586SPeter Wemm 	bzero(fdc, sizeof *fdc);
69037286586SPeter Wemm 	fdc->fdc_dev = dev;
6915f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_isa;
69237286586SPeter Wemm 
69337286586SPeter Wemm 	/* Check pnp ids */
69437286586SPeter Wemm 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
69537286586SPeter Wemm 	if (error == ENXIO)
69637286586SPeter Wemm 		return ENXIO;
6975f830ea2SJoerg Wunsch 	if (error == 0)
6985f830ea2SJoerg Wunsch 		fdc->flags |= FDC_ISPNP;
69937286586SPeter Wemm 
70037286586SPeter Wemm 	/* Attempt to allocate our resources for the duration of the probe */
70137286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
70237286586SPeter Wemm 	if (error)
70337286586SPeter Wemm 		goto out;
7045b81b6b3SRodney W. Grimes 
70516111cedSAndrew Moore 	/* First - lets reset the floppy controller */
706427ccf00SDoug Rabson 	fdout_wr(fdc, 0);
70716111cedSAndrew Moore 	DELAY(100);
708427ccf00SDoug Rabson 	fdout_wr(fdc, FDO_FRST);
70916111cedSAndrew Moore 
7105b81b6b3SRodney W. Grimes 	/* see if it can handle a command */
7116182fdbdSPeter Wemm 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7126182fdbdSPeter Wemm 		   NE7_SPEC_2(2, 0), 0)) {
7136182fdbdSPeter Wemm 		error = ENXIO;
7146182fdbdSPeter Wemm 		goto out;
7155b81b6b3SRodney W. Grimes 	}
7166182fdbdSPeter Wemm 
7176182fdbdSPeter Wemm 	if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) {
7186182fdbdSPeter Wemm 		ic_type = (u_char)ic_type;
7196182fdbdSPeter Wemm 		switch (ic_type) {
7206182fdbdSPeter Wemm 		case 0x80:
7216182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 765 or clone");
7226182fdbdSPeter Wemm 			fdc->fdct = FDC_NE765;
7236182fdbdSPeter Wemm 			break;
7246182fdbdSPeter Wemm 		case 0x81:
7256182fdbdSPeter Wemm 			device_set_desc(dev, "Intel 82077 or clone");
7266182fdbdSPeter Wemm 			fdc->fdct = FDC_I82077;
7276182fdbdSPeter Wemm 			break;
7286182fdbdSPeter Wemm 		case 0x90:
7296182fdbdSPeter Wemm 			device_set_desc(dev, "NEC 72065B or clone");
7306182fdbdSPeter Wemm 			fdc->fdct = FDC_NE72065;
7316182fdbdSPeter Wemm 			break;
7326182fdbdSPeter Wemm 		default:
7336182fdbdSPeter Wemm 			device_set_desc(dev, "generic floppy controller");
7346182fdbdSPeter Wemm 			fdc->fdct = FDC_UNKNOWN;
7356182fdbdSPeter Wemm 			break;
7366182fdbdSPeter Wemm 		}
7376182fdbdSPeter Wemm 	}
7386182fdbdSPeter Wemm 
7396182fdbdSPeter Wemm out:
74037286586SPeter Wemm 	fdc_release_resources(fdc);
7416182fdbdSPeter Wemm 	return (error);
7425b81b6b3SRodney W. Grimes }
7435b81b6b3SRodney W. Grimes 
7445f830ea2SJoerg Wunsch #if NCARD > 0
7455f830ea2SJoerg Wunsch 
7465f830ea2SJoerg Wunsch static int
7475f830ea2SJoerg Wunsch fdc_pccard_probe(device_t dev)
7485f830ea2SJoerg Wunsch {
7495f830ea2SJoerg Wunsch 	int	error;
7505f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7515f830ea2SJoerg Wunsch 
7525f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7535f830ea2SJoerg Wunsch 	bzero(fdc, sizeof *fdc);
7545f830ea2SJoerg Wunsch 	fdc->fdc_dev = dev;
7555f830ea2SJoerg Wunsch 	fdc->fdctl_wr = fdctl_wr_pcmcia;
7565f830ea2SJoerg Wunsch 
7575f830ea2SJoerg Wunsch 	fdc->flags |= FDC_ISPCMCIA | FDC_NODMA;
7585f830ea2SJoerg Wunsch 
7595f830ea2SJoerg Wunsch 	/* Attempt to allocate our resources for the duration of the probe */
7605f830ea2SJoerg Wunsch 	error = fdc_alloc_resources(fdc);
7615f830ea2SJoerg Wunsch 	if (error)
7625f830ea2SJoerg Wunsch 		goto out;
7635f830ea2SJoerg Wunsch 
7645f830ea2SJoerg Wunsch 	/* First - lets reset the floppy controller */
7655f830ea2SJoerg Wunsch 	fdout_wr(fdc, 0);
7665f830ea2SJoerg Wunsch 	DELAY(100);
7675f830ea2SJoerg Wunsch 	fdout_wr(fdc, FDO_FRST);
7685f830ea2SJoerg Wunsch 
7695f830ea2SJoerg Wunsch 	/* see if it can handle a command */
7705f830ea2SJoerg Wunsch 	if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240),
7715f830ea2SJoerg Wunsch 		   NE7_SPEC_2(2, 0), 0)) {
7725f830ea2SJoerg Wunsch 		error = ENXIO;
7735f830ea2SJoerg Wunsch 		goto out;
7745f830ea2SJoerg Wunsch 	}
7755f830ea2SJoerg Wunsch 
7765f830ea2SJoerg Wunsch 	device_set_desc(dev, "Y-E Data PCMCIA floppy");
7775f830ea2SJoerg Wunsch 	fdc->fdct = FDC_NE765;
7785f830ea2SJoerg Wunsch 
7795f830ea2SJoerg Wunsch out:
7805f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
7815f830ea2SJoerg Wunsch 	return (error);
7825f830ea2SJoerg Wunsch }
7835f830ea2SJoerg Wunsch 
7845f830ea2SJoerg Wunsch static int
7855f830ea2SJoerg Wunsch fdc_pccard_detach(device_t dev)
7865f830ea2SJoerg Wunsch {
7875f830ea2SJoerg Wunsch 	struct	fdc_data *fdc;
7885f830ea2SJoerg Wunsch 	int	error;
7895f830ea2SJoerg Wunsch 
7905f830ea2SJoerg Wunsch 	fdc = device_get_softc(dev);
7915f830ea2SJoerg Wunsch 
7925f830ea2SJoerg Wunsch 	/* have our children detached first */
7935f830ea2SJoerg Wunsch 	if ((error = bus_generic_detach(dev)))
7945f830ea2SJoerg Wunsch 		return (error);
7955f830ea2SJoerg Wunsch 
7965f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_ATTACHED) == 0) {
7975f830ea2SJoerg Wunsch 		device_printf(dev, "already unloaded\n");
7985f830ea2SJoerg Wunsch 		return (0);
7995f830ea2SJoerg Wunsch 	}
8005f830ea2SJoerg Wunsch 	fdc->flags &= ~FDC_ATTACHED;
8015f830ea2SJoerg Wunsch 
8025f830ea2SJoerg Wunsch 	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq,
8035f830ea2SJoerg Wunsch 			  fdc->fdc_intr);
8045f830ea2SJoerg Wunsch 	fdc_release_resources(fdc);
8055f830ea2SJoerg Wunsch 	device_printf(dev, "unload\n");
8065f830ea2SJoerg Wunsch 	return (0);
8075f830ea2SJoerg Wunsch }
8085f830ea2SJoerg Wunsch 
8095f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
8105f830ea2SJoerg Wunsch 
8115b81b6b3SRodney W. Grimes /*
81237286586SPeter Wemm  * Add a child device to the fdc controller.  It will then be probed etc.
8135b81b6b3SRodney W. Grimes  */
8146182fdbdSPeter Wemm static void
81537286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
8165b81b6b3SRodney W. Grimes {
81737286586SPeter Wemm 	int	disabled;
81837286586SPeter Wemm 	struct fdc_ivars *ivar;
8196182fdbdSPeter Wemm 	device_t child;
82092200632SGarrett Wollman 
8217cc0979fSDavid Malone 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
82237286586SPeter Wemm 	if (ivar == NULL)
8236182fdbdSPeter Wemm 		return;
82437286586SPeter Wemm 	if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0)
82537286586SPeter Wemm 		ivar->fdunit = 0;
826fe0d4089SMatthew N. Dodd 	child = device_add_child(dev, name, unit);
82737286586SPeter Wemm 	if (child == NULL)
8286182fdbdSPeter Wemm 		return;
82937286586SPeter Wemm 	device_set_ivars(child, ivar);
830a97c75b7SDoug Rabson 	if (resource_int_value(name, unit, "disabled", &disabled) == 0
831a97c75b7SDoug Rabson 	    && disabled != 0)
8326182fdbdSPeter Wemm 		device_disable(child);
8336182fdbdSPeter Wemm }
8346182fdbdSPeter Wemm 
8356182fdbdSPeter Wemm static int
8366182fdbdSPeter Wemm fdc_attach(device_t dev)
8376182fdbdSPeter Wemm {
83837286586SPeter Wemm 	struct	fdc_data *fdc;
83937286586SPeter Wemm 	int	i, error;
840ada54f9eSPeter Wemm 	const char *name;
841427ccf00SDoug Rabson 
84237286586SPeter Wemm 	fdc = device_get_softc(dev);
84337286586SPeter Wemm 	error = fdc_alloc_resources(fdc);
84437286586SPeter Wemm 	if (error) {
84537286586SPeter Wemm 		device_printf(dev, "cannot re-aquire resources\n");
84637286586SPeter Wemm 		return error;
84737286586SPeter Wemm 	}
84837286586SPeter Wemm 	error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq,
8493c36743eSMark Murray 			       INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc,
8503c36743eSMark Murray 			       &fdc->fdc_intr);
85137286586SPeter Wemm 	if (error) {
85237286586SPeter Wemm 		device_printf(dev, "cannot setup interrupt\n");
85337286586SPeter Wemm 		return error;
85437286586SPeter Wemm 	}
85537286586SPeter Wemm 	fdc->fdcu = device_get_unit(dev);
8565b81b6b3SRodney W. Grimes 	fdc->flags |= FDC_ATTACHED;
8576182fdbdSPeter Wemm 
8585f830ea2SJoerg Wunsch 	if ((fdc->flags & FDC_NODMA) == 0) {
859100f78bbSSujal Patel 		/* Acquire the DMA channel forever, The driver will do the rest */
8606182fdbdSPeter Wemm 				/* XXX should integrate with rman */
861100f78bbSSujal Patel 		isa_dma_acquire(fdc->dmachan);
862dd87702aSBruce Evans 		isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */);
8635f830ea2SJoerg Wunsch 	}
8645b81b6b3SRodney W. Grimes 	fdc->state = DEVIDLE;
8656182fdbdSPeter Wemm 
8663a2f7427SDavid Greenman 	/* reset controller, turn motor off, clear fdout mirror reg */
867427ccf00SDoug Rabson 	fdout_wr(fdc, ((fdc->fdout = 0)));
8688177437dSPoul-Henning Kamp 	bioq_init(&fdc->head);
8695b81b6b3SRodney W. Grimes 
8706182fdbdSPeter Wemm 	/*
87137286586SPeter Wemm 	 * Probe and attach any children.  We should probably detect
87237286586SPeter Wemm 	 * devices from the BIOS unless overridden.
8736182fdbdSPeter Wemm 	 */
874ada54f9eSPeter Wemm 	name = device_get_nameunit(dev);
875ada54f9eSPeter Wemm 	i = -1;
876ada54f9eSPeter Wemm 	while ((i = resource_query_string(i, "at", name)) != -1)
87737286586SPeter Wemm 		fdc_add_child(dev, resource_query_name(i),
87837286586SPeter Wemm 			       resource_query_unit(i));
87937286586SPeter Wemm 
8806182fdbdSPeter Wemm 	return (bus_generic_attach(dev));
8816182fdbdSPeter Wemm }
8826182fdbdSPeter Wemm 
88315317dd8SMatthew N. Dodd static int
8846182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
8856182fdbdSPeter Wemm {
88615317dd8SMatthew N. Dodd 	int retval = 0;
88715317dd8SMatthew N. Dodd 
88815317dd8SMatthew N. Dodd 	retval += bus_print_child_header(me, child);
88915317dd8SMatthew N. Dodd 	retval += printf(" on %s drive %d\n", device_get_nameunit(me),
89037286586SPeter Wemm 	       fdc_get_fdunit(child));
89115317dd8SMatthew N. Dodd 
89215317dd8SMatthew N. Dodd 	return (retval);
8936182fdbdSPeter Wemm }
8946182fdbdSPeter Wemm 
89516e68fc6SPeter Wemm static device_method_t fdc_methods[] = {
89616e68fc6SPeter Wemm 	/* Device interface */
89716e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fdc_probe),
89816e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fdc_attach),
89916e68fc6SPeter Wemm 	DEVMETHOD(device_detach,	bus_generic_detach),
90016e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
90116e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend),
90216e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume),
90316e68fc6SPeter Wemm 
90416e68fc6SPeter Wemm 	/* Bus interface */
90516e68fc6SPeter Wemm 	DEVMETHOD(bus_print_child,	fdc_print_child),
90637286586SPeter Wemm 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
90716e68fc6SPeter Wemm 	/* Our children never use any other bus interface methods. */
90816e68fc6SPeter Wemm 
90916e68fc6SPeter Wemm 	{ 0, 0 }
91016e68fc6SPeter Wemm };
91116e68fc6SPeter Wemm 
91216e68fc6SPeter Wemm static driver_t fdc_driver = {
91316e68fc6SPeter Wemm 	"fdc",
91416e68fc6SPeter Wemm 	fdc_methods,
91516e68fc6SPeter Wemm 	sizeof(struct fdc_data)
91616e68fc6SPeter Wemm };
91716e68fc6SPeter Wemm 
91816e68fc6SPeter Wemm DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
91916e68fc6SPeter Wemm 
9205f830ea2SJoerg Wunsch #if NCARD > 0
9215f830ea2SJoerg Wunsch 
9225f830ea2SJoerg Wunsch static device_method_t fdc_pccard_methods[] = {
9235f830ea2SJoerg Wunsch 	/* Device interface */
9245f830ea2SJoerg Wunsch 	DEVMETHOD(device_probe,		fdc_pccard_probe),
9255f830ea2SJoerg Wunsch 	DEVMETHOD(device_attach,	fdc_attach),
9265f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fdc_pccard_detach),
9275f830ea2SJoerg Wunsch 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
9285f830ea2SJoerg Wunsch 	DEVMETHOD(device_suspend,	bus_generic_suspend),
9295f830ea2SJoerg Wunsch 	DEVMETHOD(device_resume,	bus_generic_resume),
9305f830ea2SJoerg Wunsch 
9315f830ea2SJoerg Wunsch 	/* Bus interface */
9325f830ea2SJoerg Wunsch 	DEVMETHOD(bus_print_child,	fdc_print_child),
9335f830ea2SJoerg Wunsch 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
9345f830ea2SJoerg Wunsch 	/* Our children never use any other bus interface methods. */
9355f830ea2SJoerg Wunsch 
9365f830ea2SJoerg Wunsch 	{ 0, 0 }
9375f830ea2SJoerg Wunsch };
9385f830ea2SJoerg Wunsch 
9395f830ea2SJoerg Wunsch static driver_t fdc_pccard_driver = {
9405f830ea2SJoerg Wunsch 	"fdc",
9415f830ea2SJoerg Wunsch 	fdc_pccard_methods,
9425f830ea2SJoerg Wunsch 	sizeof(struct fdc_data)
9435f830ea2SJoerg Wunsch };
9445f830ea2SJoerg Wunsch 
9455f830ea2SJoerg Wunsch DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0);
9465f830ea2SJoerg Wunsch 
9475f830ea2SJoerg Wunsch #endif /* NCARD > 0 */
9485f830ea2SJoerg Wunsch 
9493f54a085SPoul-Henning Kamp static void fd_clone __P((void *arg, char *name, int namelen, dev_t *dev));
9503f54a085SPoul-Henning Kamp 
9513f54a085SPoul-Henning Kamp static struct {
9523f54a085SPoul-Henning Kamp 	char *match;
9533f54a085SPoul-Henning Kamp 	int minor;
9543f54a085SPoul-Henning Kamp 	int link;
9553f54a085SPoul-Henning Kamp } fd_suffix[] = {
9563f54a085SPoul-Henning Kamp 	{ "a",		0,	1 },
9573f54a085SPoul-Henning Kamp 	{ "b",		0,	1 },
9583f54a085SPoul-Henning Kamp 	{ "c",		0,	1 },
9593f54a085SPoul-Henning Kamp 	{ "d",		0,	1 },
9603f54a085SPoul-Henning Kamp 	{ "e",		0,	1 },
9613f54a085SPoul-Henning Kamp 	{ "f",		0,	1 },
9623f54a085SPoul-Henning Kamp 	{ "g",		0,	1 },
9633f54a085SPoul-Henning Kamp 	{ "h",		0,	1 },
9643f54a085SPoul-Henning Kamp 	{ ".1720",	1,	0 },
9653f54a085SPoul-Henning Kamp 	{ ".1480",	2,	0 },
9663f54a085SPoul-Henning Kamp 	{ ".1440",	3,	0 },
9673f54a085SPoul-Henning Kamp 	{ ".1200",	4,	0 },
9683f54a085SPoul-Henning Kamp 	{ ".820",	5,	0 },
9693f54a085SPoul-Henning Kamp 	{ ".800",	6,	0 },
9703f54a085SPoul-Henning Kamp 	{ ".720",	7,	0 },
9713f54a085SPoul-Henning Kamp 	{ ".360",	8,	0 },
9723f54a085SPoul-Henning Kamp 	{ ".640",	9,	0 },
9733f54a085SPoul-Henning Kamp 	{ ".1232",	10,	0 },
9743f54a085SPoul-Henning Kamp 	{ 0, 0 }
9753f54a085SPoul-Henning Kamp };
9763f54a085SPoul-Henning Kamp static void
9773f54a085SPoul-Henning Kamp fd_clone(arg, name, namelen, dev)
9783f54a085SPoul-Henning Kamp 	void *arg;
9793f54a085SPoul-Henning Kamp 	char *name;
9803f54a085SPoul-Henning Kamp 	int namelen;
9813f54a085SPoul-Henning Kamp 	dev_t *dev;
9823f54a085SPoul-Henning Kamp {
9833f54a085SPoul-Henning Kamp 	int u, d, i;
9843f54a085SPoul-Henning Kamp 	char *n;
9853f54a085SPoul-Henning Kamp 	dev_t pdev;
9863f54a085SPoul-Henning Kamp 
9873f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
9883f54a085SPoul-Henning Kamp 		return;
989db901281SPoul-Henning Kamp 	if (dev_stdclone(name, &n, "fd", &u) != 2)
9903f54a085SPoul-Henning Kamp 		return;
9913f54a085SPoul-Henning Kamp 	for (i = 0; ; i++) {
9923f54a085SPoul-Henning Kamp 		if (fd_suffix[i].match == NULL)
9933f54a085SPoul-Henning Kamp 			return;
9943f54a085SPoul-Henning Kamp 		if (strcmp(n, fd_suffix[i].match))
9953f54a085SPoul-Henning Kamp 			continue;
9963f54a085SPoul-Henning Kamp 		d = fd_suffix[i].minor;
9973f54a085SPoul-Henning Kamp 		break;
9983f54a085SPoul-Henning Kamp 	}
9993f54a085SPoul-Henning Kamp 	if (fd_suffix[i].link == 0) {
10003f54a085SPoul-Henning Kamp 		*dev = make_dev(&fd_cdevsw, (u << 6) + d,
10013f54a085SPoul-Henning Kamp 			UID_ROOT, GID_OPERATOR, 0640, name);
10023f54a085SPoul-Henning Kamp 	} else {
10033f54a085SPoul-Henning Kamp 		pdev = makedev(fd_cdevsw.d_maj, (u << 6) + d);
10043f54a085SPoul-Henning Kamp 		*dev = make_dev_alias(pdev, name);
10053f54a085SPoul-Henning Kamp 	}
10063f54a085SPoul-Henning Kamp }
10073f54a085SPoul-Henning Kamp 
100816e68fc6SPeter Wemm /******************************************************************/
100916e68fc6SPeter Wemm /*
101016e68fc6SPeter Wemm  * devices attached to the controller section.
101116e68fc6SPeter Wemm  */
10126182fdbdSPeter Wemm static int
10136182fdbdSPeter Wemm fd_probe(device_t dev)
10146182fdbdSPeter Wemm {
10156182fdbdSPeter Wemm 	int	i;
10166182fdbdSPeter Wemm 	u_int	fdt, st0, st3;
10176182fdbdSPeter Wemm 	struct	fd_data *fd;
10186182fdbdSPeter Wemm 	struct	fdc_data *fdc;
10196182fdbdSPeter Wemm 	fdsu_t	fdsu;
10206182fdbdSPeter Wemm 	static int fd_fifo = 0;
10216182fdbdSPeter Wemm 
10226182fdbdSPeter Wemm 	fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */
10236182fdbdSPeter Wemm 	fd = device_get_softc(dev);
10246182fdbdSPeter Wemm 	fdc = device_get_softc(device_get_parent(dev));
10256182fdbdSPeter Wemm 
10266182fdbdSPeter Wemm 	bzero(fd, sizeof *fd);
10276182fdbdSPeter Wemm 	fd->dev = dev;
10286182fdbdSPeter Wemm 	fd->fdc = fdc;
10296182fdbdSPeter Wemm 	fd->fdsu = fdsu;
10306182fdbdSPeter Wemm 	fd->fdu = device_get_unit(dev);
10316182fdbdSPeter Wemm 
1032a97c75b7SDoug Rabson #ifdef __i386__
1033b99f0a4aSAndrew Moore 	/* look up what bios thinks we have */
10346182fdbdSPeter Wemm 	switch (fd->fdu) {
10356182fdbdSPeter Wemm 	case 0:
10365f830ea2SJoerg Wunsch 		if ((fdc->flags & FDC_ISPCMCIA))
10375f830ea2SJoerg Wunsch 			fdt = RTCFDT_144M;
10385f830ea2SJoerg Wunsch 		else if (device_get_flags(fdc->fdc_dev) & FDC_PRETEND_D0)
10390722d6abSJoerg Wunsch 			fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED;
10400722d6abSJoerg Wunsch 		else
10410722d6abSJoerg Wunsch 			fdt = (rtcin(RTC_FDISKETTE) & 0xf0);
1042b99f0a4aSAndrew Moore 		break;
10436182fdbdSPeter Wemm 	case 1:
10446182fdbdSPeter Wemm 		fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0);
1045dc5df763SJoerg Wunsch 		break;
1046dc5df763SJoerg Wunsch 	default:
10476182fdbdSPeter Wemm 		fdt = RTCFDT_NONE;
1048dc5df763SJoerg Wunsch 		break;
10496b7bd95bSJoerg Wunsch 	}
1050a97c75b7SDoug Rabson #else
1051a97c75b7SDoug Rabson 	fdt = RTCFDT_144M;	/* XXX probably */
1052a97c75b7SDoug Rabson #endif
10536182fdbdSPeter Wemm 
10546182fdbdSPeter Wemm 	/* is there a unit? */
10556182fdbdSPeter Wemm 	if (fdt == RTCFDT_NONE)
10566182fdbdSPeter Wemm 		return (ENXIO);
10576182fdbdSPeter Wemm 
10586182fdbdSPeter Wemm 	/* select it */
10596182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNON);
10606182fdbdSPeter Wemm 	DELAY(1000000);	/* 1 sec */
10616182fdbdSPeter Wemm 
10628de0675cSPeter Wemm 	/* XXX This doesn't work before the first set_motor() */
10636182fdbdSPeter Wemm 	if (fd_fifo == 0 && fdc->fdct != FDC_NE765 && fdc->fdct != FDC_UNKNOWN
1064e34c71eaSJoerg Wunsch 	    && (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0
10656182fdbdSPeter Wemm 	    && enable_fifo(fdc) == 0) {
1066b6e5f28eSPeter Wemm 		device_printf(device_get_parent(dev),
1067b6e5f28eSPeter Wemm 		    "FIFO enabled, %d bytes threshold\n", fifo_threshold);
1068d66c374fSTor Egge 	}
10696182fdbdSPeter Wemm 	fd_fifo = 1;
10706182fdbdSPeter Wemm 
10716182fdbdSPeter Wemm 	if ((fd_cmd(fdc, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0)
10726182fdbdSPeter Wemm 	    && (st3 & NE7_ST3_T0)) {
1073dc5df763SJoerg Wunsch 		/* if at track 0, first seek inwards */
1074dc5df763SJoerg Wunsch 		/* seek some steps: */
10756182fdbdSPeter Wemm 		fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0);
1076dc5df763SJoerg Wunsch 		DELAY(300000); /* ...wait a moment... */
10776182fdbdSPeter Wemm 		fd_sense_int(fdc, 0, 0); /* make ctrlr happy */
1078dc5df763SJoerg Wunsch 	}
1079dc5df763SJoerg Wunsch 
1080dc5df763SJoerg Wunsch 	/* If we're at track 0 first seek inwards. */
10816182fdbdSPeter Wemm 	if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) {
1082dc5df763SJoerg Wunsch 		/* Seek some steps... */
10836182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1084dc5df763SJoerg Wunsch 			/* ...wait a moment... */
1085dc5df763SJoerg Wunsch 			DELAY(300000);
1086dc5df763SJoerg Wunsch 			/* make ctrlr happy: */
10876182fdbdSPeter Wemm 			fd_sense_int(fdc, 0, 0);
1088dc5df763SJoerg Wunsch 		}
1089dc5df763SJoerg Wunsch 	}
1090dc5df763SJoerg Wunsch 
10916b7bd95bSJoerg Wunsch 	for (i = 0; i < 2; i++) {
10926b7bd95bSJoerg Wunsch 		/*
10936b7bd95bSJoerg Wunsch 		 * we must recalibrate twice, just in case the
10946b7bd95bSJoerg Wunsch 		 * heads have been beyond cylinder 76, since most
10956b7bd95bSJoerg Wunsch 		 * FDCs still barf when attempting to recalibrate
10966b7bd95bSJoerg Wunsch 		 * more than 77 steps
10976b7bd95bSJoerg Wunsch 		 */
1098dc5df763SJoerg Wunsch 		/* go back to 0: */
10996182fdbdSPeter Wemm 		if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
11006b7bd95bSJoerg Wunsch 			/* a second being enough for full stroke seek*/
11016b7bd95bSJoerg Wunsch 			DELAY(i == 0 ? 1000000 : 300000);
11025b81b6b3SRodney W. Grimes 
11036b7bd95bSJoerg Wunsch 			/* anything responding? */
1104dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, 0) == 0 &&
1105dc5df763SJoerg Wunsch 			    (st0 & NE7_ST0_EC) == 0)
11066b7bd95bSJoerg Wunsch 				break; /* already probed succesfully */
11076b7bd95bSJoerg Wunsch 		}
1108dc5df763SJoerg Wunsch 	}
11096b7bd95bSJoerg Wunsch 
11106182fdbdSPeter Wemm 	set_motor(fdc, fdsu, TURNOFF);
11113a2f7427SDavid Greenman 
11123a2f7427SDavid Greenman 	if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */
11136182fdbdSPeter Wemm 		return (ENXIO);
11145b81b6b3SRodney W. Grimes 
1115dc5df763SJoerg Wunsch 	fd->track = FD_NO_TRACK;
1116b99f0a4aSAndrew Moore 	fd->fdc = fdc;
1117b99f0a4aSAndrew Moore 	fd->fdsu = fdsu;
11183a2f7427SDavid Greenman 	fd->options = 0;
111902a19910SJustin T. Gibbs 	callout_handle_init(&fd->toffhandle);
112002a19910SJustin T. Gibbs 	callout_handle_init(&fd->tohandle);
11215b81b6b3SRodney W. Grimes 
1122b99f0a4aSAndrew Moore 	switch (fdt) {
11237ca0641bSAndrey A. Chernov 	case RTCFDT_12M:
11246182fdbdSPeter Wemm 		device_set_desc(dev, "1200-KB 5.25\" drive");
1125b99f0a4aSAndrew Moore 		fd->type = FD_1200;
11267ca0641bSAndrey A. Chernov 		break;
11270722d6abSJoerg Wunsch 	case RTCFDT_144M | RTCFDT_144M_PRETENDED:
11286182fdbdSPeter Wemm 		device_set_desc(dev, "config-pretended 1440-MB 3.5\" drive");
11290722d6abSJoerg Wunsch 		fdt = RTCFDT_144M;
11306182fdbdSPeter Wemm 		fd->type = FD_1440;
11317ca0641bSAndrey A. Chernov 	case RTCFDT_144M:
11326182fdbdSPeter Wemm 		device_set_desc(dev, "1440-KB 3.5\" drive");
1133b99f0a4aSAndrew Moore 		fd->type = FD_1440;
11347ca0641bSAndrey A. Chernov 		break;
1135290dd077SJoerg Wunsch 	case RTCFDT_288M:
113686a727d9SJoerg Wunsch 	case RTCFDT_288M_1:
11376182fdbdSPeter Wemm 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1138290dd077SJoerg Wunsch 		fd->type = FD_1440;
1139290dd077SJoerg Wunsch 		break;
11407ca0641bSAndrey A. Chernov 	case RTCFDT_360K:
11416182fdbdSPeter Wemm 		device_set_desc(dev, "360-KB 5.25\" drive");
1142b99f0a4aSAndrew Moore 		fd->type = FD_360;
11437ca0641bSAndrey A. Chernov 		break;
1144ed2fa05eSAndrey A. Chernov 	case RTCFDT_720K:
11456182fdbdSPeter Wemm 		printf("720-KB 3.5\" drive");
1146b99f0a4aSAndrew Moore 		fd->type = FD_720;
1147ed2fa05eSAndrey A. Chernov 		break;
11487ca0641bSAndrey A. Chernov 	default:
11496182fdbdSPeter Wemm 		return (ENXIO);
11505b81b6b3SRodney W. Grimes 	}
11516182fdbdSPeter Wemm 	return (0);
11526182fdbdSPeter Wemm }
11536182fdbdSPeter Wemm 
11546182fdbdSPeter Wemm static int
11556182fdbdSPeter Wemm fd_attach(device_t dev)
11566182fdbdSPeter Wemm {
11576182fdbdSPeter Wemm 	struct	fd_data *fd;
1158db901281SPoul-Henning Kamp 	static int cdevsw_add_done = 0;
11596182fdbdSPeter Wemm 
11606182fdbdSPeter Wemm 	fd = device_get_softc(dev);
11616182fdbdSPeter Wemm 
116202211baeSNick Hibma 	if (!cdevsw_add_done) {
1163475ad603SPeter Wemm 		cdevsw_add(&fd_cdevsw);	/* XXX */
116402211baeSNick Hibma 		cdevsw_add_done++;
116502211baeSNick Hibma 	}
1166db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, fd_clone, 0, 1000);
11673f54a085SPoul-Henning Kamp 	make_dev(&fd_cdevsw, (fd->fdu << 6),
11683f54a085SPoul-Henning Kamp 		UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu);
1169ada9bd8cSJulian Elischer 
1170671e2ceeSBruce Evans 	/*
1171b2dfb1f9SJustin T. Gibbs 	 * Export the drive to the devstat interface.
1172671e2ceeSBruce Evans 	 */
11736182fdbdSPeter Wemm 	devstat_add_entry(&fd->device_stats, device_get_name(dev),
11746182fdbdSPeter Wemm 			  device_get_unit(dev), 512, DEVSTAT_NO_ORDERED_TAGS,
11752a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER,
11762a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_FD);
11776182fdbdSPeter Wemm 	return (0);
11785b81b6b3SRodney W. Grimes }
11795b81b6b3SRodney W. Grimes 
11805f830ea2SJoerg Wunsch static int
11815f830ea2SJoerg Wunsch fd_detach(device_t dev)
11825f830ea2SJoerg Wunsch {
11835f830ea2SJoerg Wunsch 	struct	fd_data *fd;
11845f830ea2SJoerg Wunsch 
11855f830ea2SJoerg Wunsch 	fd = device_get_softc(dev);
11865f830ea2SJoerg Wunsch 	untimeout(fd_turnoff, fd, fd->toffhandle);
11875f830ea2SJoerg Wunsch 
11885f830ea2SJoerg Wunsch 	return (0);
11895f830ea2SJoerg Wunsch }
11905f830ea2SJoerg Wunsch 
119116e68fc6SPeter Wemm static device_method_t fd_methods[] = {
119216e68fc6SPeter Wemm 	/* Device interface */
119316e68fc6SPeter Wemm 	DEVMETHOD(device_probe,		fd_probe),
119416e68fc6SPeter Wemm 	DEVMETHOD(device_attach,	fd_attach),
11955f830ea2SJoerg Wunsch 	DEVMETHOD(device_detach,	fd_detach),
119616e68fc6SPeter Wemm 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
119716e68fc6SPeter Wemm 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
119816e68fc6SPeter Wemm 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
119916e68fc6SPeter Wemm 
120016e68fc6SPeter Wemm 	{ 0, 0 }
120116e68fc6SPeter Wemm };
120216e68fc6SPeter Wemm 
120316e68fc6SPeter Wemm static driver_t fd_driver = {
120416e68fc6SPeter Wemm 	"fd",
120516e68fc6SPeter Wemm 	fd_methods,
120616e68fc6SPeter Wemm 	sizeof(struct fd_data)
120716e68fc6SPeter Wemm };
120816e68fc6SPeter Wemm 
1209475ad603SPeter Wemm DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);
121016e68fc6SPeter Wemm 
12115b81b6b3SRodney W. Grimes /****************************************************************************/
12125b81b6b3SRodney W. Grimes /*                            motor control stuff                           */
12135e235068SJordan K. Hubbard /*		remember to not deselect the drive we're working on         */
12145b81b6b3SRodney W. Grimes /****************************************************************************/
12153a2f7427SDavid Greenman static void
12166182fdbdSPeter Wemm set_motor(struct fdc_data *fdc, int fdsu, int turnon)
12175b81b6b3SRodney W. Grimes {
12186182fdbdSPeter Wemm 	int fdout = fdc->fdout;
12193a2f7427SDavid Greenman 	int needspecify = 0;
12203a2f7427SDavid Greenman 
12213a2f7427SDavid Greenman 	if(turnon) {
12223a2f7427SDavid Greenman 		fdout &= ~FDO_FDSEL;
12233a2f7427SDavid Greenman 		fdout |= (FDO_MOEN0 << fdsu) + fdsu;
12243a2f7427SDavid Greenman 	} else
12253a2f7427SDavid Greenman 		fdout &= ~(FDO_MOEN0 << fdsu);
12263a2f7427SDavid Greenman 
12275e235068SJordan K. Hubbard 	if(!turnon
12285e235068SJordan K. Hubbard 	   && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0)
12295e235068SJordan K. Hubbard 		/* gonna turn off the last drive, put FDC to bed */
12305e235068SJordan K. Hubbard 		fdout &= ~ (FDO_FRST|FDO_FDMAEN);
12315e235068SJordan K. Hubbard 	else {
12323a2f7427SDavid Greenman 		/* make sure controller is selected and specified */
12333a2f7427SDavid Greenman 		if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0)
12343a2f7427SDavid Greenman 			needspecify = 1;
12353a2f7427SDavid Greenman 		fdout |= (FDO_FRST|FDO_FDMAEN);
12365b81b6b3SRodney W. Grimes 	}
12375b81b6b3SRodney W. Grimes 
1238427ccf00SDoug Rabson 	fdout_wr(fdc, fdout);
12396182fdbdSPeter Wemm 	fdc->fdout = fdout;
12403a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdout);
12413a2f7427SDavid Greenman 
12423a2f7427SDavid Greenman 	if (needspecify) {
1243dc8603e3SJoerg Wunsch 		/*
1244dc5df763SJoerg Wunsch 		 * XXX
1245dc8603e3SJoerg Wunsch 		 * special case: since we have just woken up the FDC
1246dc8603e3SJoerg Wunsch 		 * from its sleep, we silently assume the command will
1247dc8603e3SJoerg Wunsch 		 * be accepted, and do not test for a timeout
1248dc8603e3SJoerg Wunsch 		 */
12496182fdbdSPeter Wemm 		(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1250dc5df763SJoerg Wunsch 			     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1251dc5df763SJoerg Wunsch 			     0);
12526182fdbdSPeter Wemm 		if (fdc->flags & FDC_HAS_FIFO)
12536182fdbdSPeter Wemm 			(void) enable_fifo(fdc);
12543a2f7427SDavid Greenman 	}
12553a2f7427SDavid Greenman }
12563a2f7427SDavid Greenman 
1257381fe1aaSGarrett Wollman static void
12586182fdbdSPeter Wemm fd_turnoff(void *xfd)
12595b81b6b3SRodney W. Grimes {
1260f5f7ba03SJordan K. Hubbard 	int	s;
12616182fdbdSPeter Wemm 	fd_p fd = xfd;
1262dc16046fSJoerg Wunsch 
12636182fdbdSPeter Wemm 	TRACE1("[fd%d: turnoff]", fd->fdu);
12648335c1b8SBruce Evans 
12655f830ea2SJoerg Wunsch 	s = splbio();
12668335c1b8SBruce Evans 	/*
12678335c1b8SBruce Evans 	 * Don't turn off the motor yet if the drive is active.
12685f830ea2SJoerg Wunsch 	 *
12695f830ea2SJoerg Wunsch 	 * If we got here, this could only mean we missed an interrupt.
12705f830ea2SJoerg Wunsch 	 * This can e. g. happen on the Y-E Date PCMCIA floppy controller
12715f830ea2SJoerg Wunsch 	 * after a controller reset.  Just schedule a pseudo-interrupt
12725f830ea2SJoerg Wunsch 	 * so the state machine gets re-entered.
12738335c1b8SBruce Evans 	 */
12746182fdbdSPeter Wemm 	if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) {
12755f830ea2SJoerg Wunsch 		fdc_intr(fd->fdc);
12765f830ea2SJoerg Wunsch 		splx(s);
12778335c1b8SBruce Evans 		return;
12788335c1b8SBruce Evans 	}
12798335c1b8SBruce Evans 
12805b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR;
12816182fdbdSPeter Wemm 	set_motor(fd->fdc, fd->fdsu, TURNOFF);
1282f5f7ba03SJordan K. Hubbard 	splx(s);
12835b81b6b3SRodney W. Grimes }
12845b81b6b3SRodney W. Grimes 
12853a2f7427SDavid Greenman static void
12866182fdbdSPeter Wemm fd_motor_on(void *xfd)
12875b81b6b3SRodney W. Grimes {
1288f5f7ba03SJordan K. Hubbard 	int	s;
12896182fdbdSPeter Wemm 	fd_p fd = xfd;
1290f5f7ba03SJordan K. Hubbard 
1291f5f7ba03SJordan K. Hubbard 	s = splbio();
12925b81b6b3SRodney W. Grimes 	fd->flags &= ~FD_MOTOR_WAIT;
12935b81b6b3SRodney W. Grimes 	if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT))
12945b81b6b3SRodney W. Grimes 	{
12956182fdbdSPeter Wemm 		fdc_intr(fd->fdc);
12965b81b6b3SRodney W. Grimes 	}
1297f5f7ba03SJordan K. Hubbard 	splx(s);
12985b81b6b3SRodney W. Grimes }
12995b81b6b3SRodney W. Grimes 
13003a2f7427SDavid Greenman static void
13016182fdbdSPeter Wemm fd_turnon(fd_p fd)
13025b81b6b3SRodney W. Grimes {
13035b81b6b3SRodney W. Grimes 	if(!(fd->flags & FD_MOTOR))
13045b81b6b3SRodney W. Grimes 	{
13053a2f7427SDavid Greenman 		fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT);
13066182fdbdSPeter Wemm 		set_motor(fd->fdc, fd->fdsu, TURNON);
13076182fdbdSPeter Wemm 		timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */
13085b81b6b3SRodney W. Grimes 	}
13095b81b6b3SRodney W. Grimes }
13105b81b6b3SRodney W. Grimes 
1311381fe1aaSGarrett Wollman static void
1312dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc)
13135b81b6b3SRodney W. Grimes {
13143a2f7427SDavid Greenman 	/* Try a reset, keep motor on */
1315427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
13163a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
13173a2f7427SDavid Greenman 	DELAY(100);
13183a2f7427SDavid Greenman 	/* enable FDC, but defer interrupts a moment */
1319427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
13203a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN);
13213a2f7427SDavid Greenman 	DELAY(100);
1322427ccf00SDoug Rabson 	fdout_wr(fdc, fdc->fdout);
13233a2f7427SDavid Greenman 	TRACE1("[0x%x->FDOUT]", fdc->fdout);
13243a2f7427SDavid Greenman 
1325dc5df763SJoerg Wunsch 	/* XXX after a reset, silently believe the FDC will accept commands */
13266182fdbdSPeter Wemm 	(void)fd_cmd(fdc, 3, NE7CMD_SPECIFY,
1327dc5df763SJoerg Wunsch 		     NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0),
1328dc5df763SJoerg Wunsch 		     0);
1329d66c374fSTor Egge 	if (fdc->flags & FDC_HAS_FIFO)
1330d66c374fSTor Egge 		(void) enable_fifo(fdc);
13315b81b6b3SRodney W. Grimes }
13325b81b6b3SRodney W. Grimes 
13335b81b6b3SRodney W. Grimes /****************************************************************************/
13345b81b6b3SRodney W. Grimes /*                             fdc in/out                                   */
13355b81b6b3SRodney W. Grimes /****************************************************************************/
13365b81b6b3SRodney W. Grimes int
13376182fdbdSPeter Wemm in_fdc(struct fdc_data *fdc)
13385b81b6b3SRodney W. Grimes {
13395b81b6b3SRodney W. Grimes 	int i, j = 100000;
1340427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
13415b81b6b3SRodney W. Grimes 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1342dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13436182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
13445b81b6b3SRodney W. Grimes 	if (j <= 0)
13456182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1346d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1347427ccf00SDoug Rabson 	i = fddata_rd(fdc);
13483a2f7427SDavid Greenman 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
13495b81b6b3SRodney W. Grimes 	return(i);
1350d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1351427ccf00SDoug Rabson 	return fddata_rd(fdc);
1352d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
13535b81b6b3SRodney W. Grimes }
13545b81b6b3SRodney W. Grimes 
1355dc5df763SJoerg Wunsch /*
1356dc5df763SJoerg Wunsch  * fd_in: Like in_fdc, but allows you to see if it worked.
1357dc5df763SJoerg Wunsch  */
1358b5e8ce9fSBruce Evans static int
13596182fdbdSPeter Wemm fd_in(struct fdc_data *fdc, int *ptr)
1360dc5df763SJoerg Wunsch {
1361dc5df763SJoerg Wunsch 	int i, j = 100000;
1362427ccf00SDoug Rabson 	while ((i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM))
1363dc5df763SJoerg Wunsch 		!= (NE7_DIO|NE7_RQM) && j-- > 0)
1364dc5df763SJoerg Wunsch 		if (i == NE7_RQM)
13656182fdbdSPeter Wemm 			return fdc_err(fdc, "ready for output in input\n");
1366dc5df763SJoerg Wunsch 	if (j <= 0)
13676182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "input ready timeout\n": 0);
1368d2fb4892SJoerg Wunsch #ifdef	FDC_DEBUG
1369427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1370dc5df763SJoerg Wunsch 	TRACE1("[FDDATA->0x%x]", (unsigned char)i);
1371dc5df763SJoerg Wunsch 	*ptr = i;
1372dc5df763SJoerg Wunsch 	return 0;
1373d2fb4892SJoerg Wunsch #else	/* !FDC_DEBUG */
1374427ccf00SDoug Rabson 	i = fddata_rd(fdc);
1375dc5df763SJoerg Wunsch 	if (ptr)
1376dc5df763SJoerg Wunsch 		*ptr = i;
1377dc5df763SJoerg Wunsch 	return 0;
1378d2fb4892SJoerg Wunsch #endif	/* FDC_DEBUG */
1379dc5df763SJoerg Wunsch }
1380dc5df763SJoerg Wunsch 
1381dc5df763SJoerg Wunsch int
13826182fdbdSPeter Wemm out_fdc(struct fdc_data *fdc, int x)
13835b81b6b3SRodney W. Grimes {
13843b3837dbSRodney W. Grimes 	int i;
13855b81b6b3SRodney W. Grimes 
13863b3837dbSRodney W. Grimes 	/* Check that the direction bit is set */
13873b3837dbSRodney W. Grimes 	i = 100000;
1388427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_DIO) && i-- > 0);
13896182fdbdSPeter Wemm 	if (i <= 0) return fdc_err(fdc, "direction bit not set\n");
13903b3837dbSRodney W. Grimes 
13913b3837dbSRodney W. Grimes 	/* Check that the floppy controller is ready for a command */
13923b3837dbSRodney W. Grimes 	i = 100000;
1393427ccf00SDoug Rabson 	while ((fdsts_rd(fdc) & NE7_RQM) == 0 && i-- > 0);
139416b04b6aSJoerg Wunsch 	if (i <= 0)
13956182fdbdSPeter Wemm 		return fdc_err(fdc, bootverbose? "output ready timeout\n": 0);
13963b3837dbSRodney W. Grimes 
13973b3837dbSRodney W. Grimes 	/* Send the command and return */
1398427ccf00SDoug Rabson 	fddata_wr(fdc, x);
13993a2f7427SDavid Greenman 	TRACE1("[0x%x->FDDATA]", x);
14005b81b6b3SRodney W. Grimes 	return (0);
14015b81b6b3SRodney W. Grimes }
14025b81b6b3SRodney W. Grimes 
14035b81b6b3SRodney W. Grimes /****************************************************************************/
14045b81b6b3SRodney W. Grimes /*                           fdopen/fdclose                                 */
14055b81b6b3SRodney W. Grimes /****************************************************************************/
1406381fe1aaSGarrett Wollman int
1407671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p)
14085b81b6b3SRodney W. Grimes {
14095b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
141020a29168SAndrey A. Chernov 	int type = FDTYPE(minor(dev));
14116182fdbdSPeter Wemm 	fd_p	fd;
1412b99f0a4aSAndrew Moore 	fdc_p	fdc;
14135b81b6b3SRodney W. Grimes 
14145b81b6b3SRodney W. Grimes 	/* check bounds */
14156182fdbdSPeter Wemm 	if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0)
1416b99f0a4aSAndrew Moore 		return (ENXIO);
14176182fdbdSPeter Wemm 	fdc = fd->fdc;
14186182fdbdSPeter Wemm 	if ((fdc == NULL) || (fd->type == NO_TYPE))
1419b99f0a4aSAndrew Moore 		return (ENXIO);
1420b99f0a4aSAndrew Moore 	if (type > NUMDENS)
1421b99f0a4aSAndrew Moore 		return (ENXIO);
14227ca0641bSAndrey A. Chernov 	if (type == 0)
14236182fdbdSPeter Wemm 		type = fd->type;
14247ca0641bSAndrey A. Chernov 	else {
14253e425b96SJulian Elischer 		/*
14263e425b96SJulian Elischer 		 * For each type of basic drive, make sure we are trying
14273e425b96SJulian Elischer 		 * to open a type it can do,
14283e425b96SJulian Elischer 		 */
14296182fdbdSPeter Wemm 		if (type != fd->type) {
14306182fdbdSPeter Wemm 			switch (fd->type) {
14317ca0641bSAndrey A. Chernov 			case FD_360:
14327ca0641bSAndrey A. Chernov 				return (ENXIO);
1433ed2fa05eSAndrey A. Chernov 			case FD_720:
1434b39c878eSAndrey A. Chernov 				if (   type != FD_820
1435b39c878eSAndrey A. Chernov 				    && type != FD_800
14366fb89845SKATO Takenori 				    && type != FD_640
1437ed2fa05eSAndrey A. Chernov 				   )
1438ed2fa05eSAndrey A. Chernov 					return (ENXIO);
1439ed2fa05eSAndrey A. Chernov 				break;
14407ca0641bSAndrey A. Chernov 			case FD_1200:
1441b39c878eSAndrey A. Chernov 				switch (type) {
1442b39c878eSAndrey A. Chernov 				case FD_1480:
1443b39c878eSAndrey A. Chernov 					type = FD_1480in5_25;
1444fa4700b4SAndrey A. Chernov 					break;
14457ca0641bSAndrey A. Chernov 				case FD_1440:
1446b39c878eSAndrey A. Chernov 					type = FD_1440in5_25;
1447b39c878eSAndrey A. Chernov 					break;
14486fb89845SKATO Takenori 				case FD_1232:
14496fb89845SKATO Takenori 					break;
1450b39c878eSAndrey A. Chernov 				case FD_820:
1451b39c878eSAndrey A. Chernov 					type = FD_820in5_25;
1452b39c878eSAndrey A. Chernov 					break;
1453b39c878eSAndrey A. Chernov 				case FD_800:
1454b39c878eSAndrey A. Chernov 					type = FD_800in5_25;
1455b39c878eSAndrey A. Chernov 					break;
1456b39c878eSAndrey A. Chernov 				case FD_720:
1457b39c878eSAndrey A. Chernov 					type = FD_720in5_25;
1458b39c878eSAndrey A. Chernov 					break;
14596fb89845SKATO Takenori 				case FD_640:
14606fb89845SKATO Takenori 					type = FD_640in5_25;
14616fb89845SKATO Takenori 					break;
1462b39c878eSAndrey A. Chernov 				case FD_360:
1463b39c878eSAndrey A. Chernov 					type = FD_360in5_25;
1464b39c878eSAndrey A. Chernov 					break;
1465b39c878eSAndrey A. Chernov 				default:
1466b39c878eSAndrey A. Chernov 					return(ENXIO);
1467b39c878eSAndrey A. Chernov 				}
1468b39c878eSAndrey A. Chernov 				break;
1469b39c878eSAndrey A. Chernov 			case FD_1440:
1470b39c878eSAndrey A. Chernov 				if (   type != FD_1720
1471b39c878eSAndrey A. Chernov 				    && type != FD_1480
1472ed2fa05eSAndrey A. Chernov 				    && type != FD_1200
1473b39c878eSAndrey A. Chernov 				    && type != FD_820
1474b39c878eSAndrey A. Chernov 				    && type != FD_800
1475b39c878eSAndrey A. Chernov 				    && type != FD_720
14766fb89845SKATO Takenori 				    && type != FD_640
14777ca0641bSAndrey A. Chernov 				    )
1478dffff499SAndrey A. Chernov 					return(ENXIO);
1479fa4700b4SAndrey A. Chernov 				break;
14807ca0641bSAndrey A. Chernov 			}
14817ca0641bSAndrey A. Chernov 		}
1482fa4700b4SAndrey A. Chernov 	}
14836182fdbdSPeter Wemm 	fd->ft = fd_types + type - 1;
14846182fdbdSPeter Wemm 	fd->flags |= FD_OPEN;
14855f830ea2SJoerg Wunsch 
14865b81b6b3SRodney W. Grimes 	return 0;
14875b81b6b3SRodney W. Grimes }
14885b81b6b3SRodney W. Grimes 
1489381fe1aaSGarrett Wollman int
1490671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p)
14915b81b6b3SRodney W. Grimes {
14925b81b6b3SRodney W. Grimes  	fdu_t fdu = FDUNIT(minor(dev));
14936182fdbdSPeter Wemm 	struct fd_data *fd;
1494b99f0a4aSAndrew Moore 
14956182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
14966182fdbdSPeter Wemm 	fd->flags &= ~FD_OPEN;
14976182fdbdSPeter Wemm 	fd->options &= ~FDOPT_NORETRY;
1498dc16046fSJoerg Wunsch 
14995b81b6b3SRodney W. Grimes 	return (0);
15005b81b6b3SRodney W. Grimes }
15015b81b6b3SRodney W. Grimes 
15023a2f7427SDavid Greenman /****************************************************************************/
15033a2f7427SDavid Greenman /*                               fdstrategy                                 */
15043a2f7427SDavid Greenman /****************************************************************************/
15053a2f7427SDavid Greenman void
15068177437dSPoul-Henning Kamp fdstrategy(struct bio *bp)
15073a2f7427SDavid Greenman {
1508bb6382faSJoerg Wunsch 	unsigned nblocks, blknum, cando;
15093a2f7427SDavid Greenman  	int	s;
15103a2f7427SDavid Greenman  	fdu_t	fdu;
15113a2f7427SDavid Greenman  	fdc_p	fdc;
15123a2f7427SDavid Greenman  	fd_p	fd;
15133a2f7427SDavid Greenman 	size_t	fdblk;
15143a2f7427SDavid Greenman 
15158177437dSPoul-Henning Kamp  	fdu = FDUNIT(minor(bp->bio_dev));
15166182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
15176182fdbdSPeter Wemm 	if (fd == 0)
15186182fdbdSPeter Wemm 		panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)",
15198177437dSPoul-Henning Kamp 		      (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev));
15203a2f7427SDavid Greenman 	fdc = fd->fdc;
152169acd21dSWarner Losh 	if (fd->type == NO_TYPE) {
15228177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
15238177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15243b178206SWarner Losh 		goto bad;
152569acd21dSWarner Losh 	};
15263a2f7427SDavid Greenman 
1527d3628763SRodney W. Grimes 	fdblk = 128 << (fd->ft->secsize);
15288177437dSPoul-Henning Kamp 	if (!(bp->bio_cmd & BIO_FORMAT)) {
15298177437dSPoul-Henning Kamp 		if (bp->bio_blkno < 0) {
1530dc5df763SJoerg Wunsch 			printf(
15316a0e6f42SRodney W. Grimes 		"fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n",
15328177437dSPoul-Henning Kamp 			       fdu, (u_long)bp->bio_blkno, bp->bio_bcount);
15338177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15348177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15353a2f7427SDavid Greenman 			goto bad;
15363a2f7427SDavid Greenman 		}
15378177437dSPoul-Henning Kamp 		if ((bp->bio_bcount % fdblk) != 0) {
15388177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15398177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
15403a2f7427SDavid Greenman 			goto bad;
15413a2f7427SDavid Greenman 		}
15423a2f7427SDavid Greenman 	}
15433a2f7427SDavid Greenman 
15443a2f7427SDavid Greenman 	/*
15453a2f7427SDavid Greenman 	 * Set up block calculations.
15463a2f7427SDavid Greenman 	 */
15478177437dSPoul-Henning Kamp 	if (bp->bio_blkno > 20000000) {
1548bb6382faSJoerg Wunsch 		/*
1549bb6382faSJoerg Wunsch 		 * Reject unreasonably high block number, prevent the
1550bb6382faSJoerg Wunsch 		 * multiplication below from overflowing.
1551bb6382faSJoerg Wunsch 		 */
15528177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
15538177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
15543a2f7427SDavid Greenman 		goto bad;
15553a2f7427SDavid Greenman 	}
15568177437dSPoul-Henning Kamp 	blknum = (unsigned) bp->bio_blkno * DEV_BSIZE/fdblk;
1557bb6382faSJoerg Wunsch  	nblocks = fd->ft->size;
15588177437dSPoul-Henning Kamp 	bp->bio_resid = 0;
15598177437dSPoul-Henning Kamp 	if (blknum + (bp->bio_bcount / fdblk) > nblocks) {
1560bb6382faSJoerg Wunsch 		if (blknum <= nblocks) {
1561bb6382faSJoerg Wunsch 			cando = (nblocks - blknum) * fdblk;
15628177437dSPoul-Henning Kamp 			bp->bio_resid = bp->bio_bcount - cando;
1563bb6382faSJoerg Wunsch 			if (cando == 0)
1564bb6382faSJoerg Wunsch 				goto bad;	/* not actually bad but EOF */
1565bb6382faSJoerg Wunsch 		} else {
15668177437dSPoul-Henning Kamp 			bp->bio_error = EINVAL;
15678177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
1568bb6382faSJoerg Wunsch 			goto bad;
1569bb6382faSJoerg Wunsch 		}
1570bb6382faSJoerg Wunsch 	}
15718177437dSPoul-Henning Kamp  	bp->bio_pblkno = bp->bio_blkno;
15723a2f7427SDavid Greenman 	s = splbio();
15738177437dSPoul-Henning Kamp 	bioqdisksort(&fdc->head, bp);
15746182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */
1575b2dfb1f9SJustin T. Gibbs 
1576b2dfb1f9SJustin T. Gibbs 	/* Tell devstat we are starting on the transaction */
1577b2dfb1f9SJustin T. Gibbs 	devstat_start_transaction(&fd->device_stats);
15785f830ea2SJoerg Wunsch 	device_busy(fd->dev);
1579b2dfb1f9SJustin T. Gibbs 
15806182fdbdSPeter Wemm 	fdstart(fdc);
15813a2f7427SDavid Greenman 	splx(s);
15823a2f7427SDavid Greenman 	return;
15833a2f7427SDavid Greenman 
15843a2f7427SDavid Greenman bad:
15853a2f7427SDavid Greenman 	biodone(bp);
15863a2f7427SDavid Greenman }
15873a2f7427SDavid Greenman 
15885b81b6b3SRodney W. Grimes /***************************************************************\
15895b81b6b3SRodney W. Grimes *				fdstart				*
15905b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy	*
15915b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command	*
15925b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more*
15935b81b6b3SRodney W. Grimes * work to do and picks up what we just added.			*
15945b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it	*
15955b81b6b3SRodney W. Grimes * will pick up our work when the present work completes		*
15965b81b6b3SRodney W. Grimes \***************************************************************/
1597381fe1aaSGarrett Wollman static void
15986182fdbdSPeter Wemm fdstart(struct fdc_data *fdc)
15995b81b6b3SRodney W. Grimes {
16005b81b6b3SRodney W. Grimes 	int s;
16015b81b6b3SRodney W. Grimes 
16025b81b6b3SRodney W. Grimes 	s = splbio();
16036182fdbdSPeter Wemm 	if(fdc->state == DEVIDLE)
16045b81b6b3SRodney W. Grimes 	{
16056182fdbdSPeter Wemm 		fdc_intr(fdc);
16065b81b6b3SRodney W. Grimes 	}
16075b81b6b3SRodney W. Grimes 	splx(s);
16085b81b6b3SRodney W. Grimes }
16095b81b6b3SRodney W. Grimes 
1610381fe1aaSGarrett Wollman static void
16116182fdbdSPeter Wemm fd_iotimeout(void *xfdc)
16125b81b6b3SRodney W. Grimes {
16135c1a1eaeSBruce Evans  	fdc_p fdc;
1614f5f7ba03SJordan K. Hubbard 	int s;
16155b81b6b3SRodney W. Grimes 
16166182fdbdSPeter Wemm 	fdc = xfdc;
16175c1a1eaeSBruce Evans 	TRACE1("fd%d[fd_iotimeout()]", fdc->fdu);
16185b81b6b3SRodney W. Grimes 
16193a2f7427SDavid Greenman 	/*
16203a2f7427SDavid Greenman 	 * Due to IBM's brain-dead design, the FDC has a faked ready
16213a2f7427SDavid Greenman 	 * signal, hardwired to ready == true. Thus, any command
16223a2f7427SDavid Greenman 	 * issued if there's no diskette in the drive will _never_
16233a2f7427SDavid Greenman 	 * complete, and must be aborted by resetting the FDC.
16243a2f7427SDavid Greenman 	 * Many thanks, Big Blue!
16255c1a1eaeSBruce Evans 	 * The FDC must not be reset directly, since that would
16265c1a1eaeSBruce Evans 	 * interfere with the state machine.  Instead, pretend that
16275c1a1eaeSBruce Evans 	 * the command completed but was invalid.  The state machine
16285c1a1eaeSBruce Evans 	 * will reset the FDC and retry once.
16293a2f7427SDavid Greenman 	 */
16303a2f7427SDavid Greenman 	s = splbio();
16315c1a1eaeSBruce Evans 	fdc->status[0] = NE7_ST0_IC_IV;
16325c1a1eaeSBruce Evans 	fdc->flags &= ~FDC_STAT_VALID;
16335c1a1eaeSBruce Evans 	fdc->state = IOTIMEDOUT;
16346182fdbdSPeter Wemm 	fdc_intr(fdc);
1635f5f7ba03SJordan K. Hubbard 	splx(s);
16365b81b6b3SRodney W. Grimes }
16375b81b6b3SRodney W. Grimes 
16385b81b6b3SRodney W. Grimes /* just ensure it has the right spl */
1639381fe1aaSGarrett Wollman static void
16406182fdbdSPeter Wemm fd_pseudointr(void *xfdc)
16415b81b6b3SRodney W. Grimes {
16425b81b6b3SRodney W. Grimes 	int	s;
16433a2f7427SDavid Greenman 
16445b81b6b3SRodney W. Grimes 	s = splbio();
16456182fdbdSPeter Wemm 	fdc_intr(xfdc);
16465b81b6b3SRodney W. Grimes 	splx(s);
16475b81b6b3SRodney W. Grimes }
16485b81b6b3SRodney W. Grimes 
16495b81b6b3SRodney W. Grimes /***********************************************************************\
16505b81b6b3SRodney W. Grimes *                                 fdintr				*
16515b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0			*
16525b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO 						*
16535b81b6b3SRodney W. Grimes \***********************************************************************/
1654fe310de8SBruce Evans static void
16556182fdbdSPeter Wemm fdc_intr(void *xfdc)
16565b81b6b3SRodney W. Grimes {
16576182fdbdSPeter Wemm 	fdc_p fdc = xfdc;
16586182fdbdSPeter Wemm 	while(fdstate(fdc))
1659381fe1aaSGarrett Wollman 		;
16605b81b6b3SRodney W. Grimes }
16615b81b6b3SRodney W. Grimes 
166269acd21dSWarner Losh /*
166369acd21dSWarner Losh  * magic pseudo-DMA initialization for YE FDC. Sets count and
166469acd21dSWarner Losh  * direction
166569acd21dSWarner Losh  */
16663b178206SWarner Losh #define SET_BCDR(fdc,wr,cnt,port) \
16673b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port,	 \
16683b178206SWarner Losh 	    ((cnt)-1) & 0xff);						 \
16693b178206SWarner Losh 	bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \
16703b178206SWarner Losh 	    ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f)));
167169acd21dSWarner Losh 
167269acd21dSWarner Losh /*
167369acd21dSWarner Losh  * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy
167469acd21dSWarner Losh  */
16753b178206SWarner Losh static int fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count)
167669acd21dSWarner Losh {
167769acd21dSWarner Losh 	u_char *cptr = (u_char *)addr;
167869acd21dSWarner Losh 
167921144e3bSPoul-Henning Kamp 	if (flags == BIO_READ) {
168069acd21dSWarner Losh 		if (fdc->state != PIOREAD) {
168169acd21dSWarner Losh 			fdc->state = PIOREAD;
168269acd21dSWarner Losh 			return(0);
168369acd21dSWarner Losh 		};
16843b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
16853b178206SWarner Losh 		bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16863b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
168769acd21dSWarner Losh 	} else {
16883b178206SWarner Losh 		bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
16893b178206SWarner Losh 		    FDC_YE_DATAPORT, cptr, count);
16903b178206SWarner Losh 		SET_BCDR(fdc, 0, count, 0);
169169acd21dSWarner Losh 	};
169269acd21dSWarner Losh 	return(1);
169369acd21dSWarner Losh }
169469acd21dSWarner Losh 
16955b81b6b3SRodney W. Grimes /***********************************************************************\
16965b81b6b3SRodney W. Grimes * The controller state machine.						*
16975b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly	*
16985b81b6b3SRodney W. Grimes \***********************************************************************/
16993a2f7427SDavid Greenman static int
17006182fdbdSPeter Wemm fdstate(fdc_p fdc)
17015b81b6b3SRodney W. Grimes {
170256a23089SPoul-Henning Kamp 	int read, format, head, i, sec = 0, sectrac, st0, cyl, st3, idf;
1703bb6382faSJoerg Wunsch 	unsigned blknum = 0, b_cylinder = 0;
17045b81b6b3SRodney W. Grimes 	fdu_t fdu = fdc->fdu;
17055b81b6b3SRodney W. Grimes 	fd_p fd;
17068177437dSPoul-Henning Kamp 	register struct bio *bp;
1707b39c878eSAndrey A. Chernov 	struct fd_formb *finfo = NULL;
17083a2f7427SDavid Greenman 	size_t fdblk;
17095b81b6b3SRodney W. Grimes 
1710e93e63cbSBruce Evans 	bp = fdc->bp;
1711e93e63cbSBruce Evans 	if (bp == NULL) {
17128177437dSPoul-Henning Kamp 		bp = bioq_first(&fdc->head);
1713e93e63cbSBruce Evans 		if (bp != NULL) {
17148177437dSPoul-Henning Kamp 			bioq_remove(&fdc->head, bp);
1715e93e63cbSBruce Evans 			fdc->bp = bp;
1716e93e63cbSBruce Evans 		}
1717e93e63cbSBruce Evans 	}
1718e93e63cbSBruce Evans 	if (bp == NULL) {
17195b81b6b3SRodney W. Grimes 		/***********************************************\
17205b81b6b3SRodney W. Grimes 		* nothing left for this controller to do	*
17215b81b6b3SRodney W. Grimes 		* Force into the IDLE state,			*
17225b81b6b3SRodney W. Grimes 		\***********************************************/
17235b81b6b3SRodney W. Grimes 		fdc->state = DEVIDLE;
17246182fdbdSPeter Wemm 		if (fdc->fd) {
1725b6e5f28eSPeter Wemm 			device_printf(fdc->fdc_dev,
1726b6e5f28eSPeter Wemm 			    "unexpected valid fd pointer\n");
17275b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
17285b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
17295b81b6b3SRodney W. Grimes 		}
17306182fdbdSPeter Wemm 		TRACE1("[fdc%d IDLE]", fdc->fdcu);
17315b81b6b3SRodney W. Grimes  		return (0);
17325b81b6b3SRodney W. Grimes 	}
17338177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
17346182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
17353a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
1736b6e5f28eSPeter Wemm 	if (fdc->fd && (fd != fdc->fd))
1737b6e5f28eSPeter Wemm 		device_printf(fd->dev, "confused fd pointers\n");
17388177437dSPoul-Henning Kamp 	read = bp->bio_cmd == BIO_READ;
173956a23089SPoul-Henning Kamp 	if (read)
174056a23089SPoul-Henning Kamp 		idf = ISADMA_READ;
174156a23089SPoul-Henning Kamp 	else
174256a23089SPoul-Henning Kamp 		idf = ISADMA_WRITE;
17438177437dSPoul-Henning Kamp 	format = bp->bio_cmd & BIO_FORMAT;
1744bb6382faSJoerg Wunsch 	if (format) {
17458177437dSPoul-Henning Kamp 		finfo = (struct fd_formb *)bp->bio_data;
1746bb6382faSJoerg Wunsch 		fd->skip = (char *)&(finfo->fd_formb_cylno(0))
1747bb6382faSJoerg Wunsch 			- (char *)finfo;
1748bb6382faSJoerg Wunsch 	}
1749bb6382faSJoerg Wunsch 	if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) {
17508177437dSPoul-Henning Kamp 		blknum = (unsigned) bp->bio_pblkno * DEV_BSIZE/fdblk +
1751bb6382faSJoerg Wunsch 			fd->skip/fdblk;
1752bb6382faSJoerg Wunsch 		b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads);
1753bb6382faSJoerg Wunsch 	}
17545b81b6b3SRodney W. Grimes 	TRACE1("fd%d", fdu);
17555b81b6b3SRodney W. Grimes 	TRACE1("[%s]", fdstates[fdc->state]);
17565b81b6b3SRodney W. Grimes 	TRACE1("(0x%x)", fd->flags);
17576182fdbdSPeter Wemm 	untimeout(fd_turnoff, fd, fd->toffhandle);
17586182fdbdSPeter Wemm 	fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz);
17595b81b6b3SRodney W. Grimes 	switch (fdc->state)
17605b81b6b3SRodney W. Grimes 	{
17615b81b6b3SRodney W. Grimes 	case DEVIDLE:
17625b81b6b3SRodney W. Grimes 	case FINDWORK:	/* we have found new work */
17635b81b6b3SRodney W. Grimes 		fdc->retry = 0;
17645b81b6b3SRodney W. Grimes 		fd->skip = 0;
17655b81b6b3SRodney W. Grimes 		fdc->fd = fd;
17665b81b6b3SRodney W. Grimes 		fdc->fdu = fdu;
17675f830ea2SJoerg Wunsch 		fdc->fdctl_wr(fdc, fd->ft->trans);
17683a2f7427SDavid Greenman 		TRACE1("[0x%x->FDCTL]", fd->ft->trans);
17695b81b6b3SRodney W. Grimes 		/*******************************************************\
17705b81b6b3SRodney W. Grimes 		* If the next drive has a motor startup pending, then	*
1771dc733423SDag-Erling Smørgrav 		* it will start up in its own good time		*
17725b81b6b3SRodney W. Grimes 		\*******************************************************/
17736182fdbdSPeter Wemm 		if(fd->flags & FD_MOTOR_WAIT) {
17745b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17755b81b6b3SRodney W. Grimes 			return (0); /* come back later */
17765b81b6b3SRodney W. Grimes 		}
17775b81b6b3SRodney W. Grimes 		/*******************************************************\
17785b81b6b3SRodney W. Grimes 		* Maybe if it's not starting, it SHOULD be starting	*
17795b81b6b3SRodney W. Grimes 		\*******************************************************/
17805b81b6b3SRodney W. Grimes 		if (!(fd->flags & FD_MOTOR))
17815b81b6b3SRodney W. Grimes 		{
17825b81b6b3SRodney W. Grimes 			fdc->state = MOTORWAIT;
17836182fdbdSPeter Wemm 			fd_turnon(fd);
17845b81b6b3SRodney W. Grimes 			return (0);
17855b81b6b3SRodney W. Grimes 		}
17865b81b6b3SRodney W. Grimes 		else	/* at least make sure we are selected */
17875b81b6b3SRodney W. Grimes 		{
17886182fdbdSPeter Wemm 			set_motor(fdc, fd->fdsu, TURNON);
17895b81b6b3SRodney W. Grimes 		}
17905c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
17915c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
17925c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
17935c1a1eaeSBruce Evans 		} else
17945e235068SJordan K. Hubbard 			fdc->state = DOSEEK;
17955b81b6b3SRodney W. Grimes 		break;
17965b81b6b3SRodney W. Grimes 	case DOSEEK:
1797bb6382faSJoerg Wunsch 		if (b_cylinder == (unsigned)fd->track)
17985b81b6b3SRodney W. Grimes 		{
17995b81b6b3SRodney W. Grimes 			fdc->state = SEEKCOMPLETE;
18005b81b6b3SRodney W. Grimes 			break;
18015b81b6b3SRodney W. Grimes 		}
18026182fdbdSPeter Wemm 		if (fd_cmd(fdc, 3, NE7CMD_SEEK,
1803bb6382faSJoerg Wunsch 			   fd->fdsu, b_cylinder * fd->ft->steptrac,
1804dc5df763SJoerg Wunsch 			   0))
1805dc8603e3SJoerg Wunsch 		{
1806dc8603e3SJoerg Wunsch 			/*
1807dc8603e3SJoerg Wunsch 			 * seek command not accepted, looks like
1808dc8603e3SJoerg Wunsch 			 * the FDC went off to the Saints...
1809dc8603e3SJoerg Wunsch 			 */
1810dc8603e3SJoerg Wunsch 			fdc->retry = 6;	/* try a reset */
18116182fdbdSPeter Wemm 			return(retrier(fdc));
1812dc8603e3SJoerg Wunsch 		}
1813dc5df763SJoerg Wunsch 		fd->track = FD_NO_TRACK;
18145b81b6b3SRodney W. Grimes 		fdc->state = SEEKWAIT;
18155b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18165b81b6b3SRodney W. Grimes 	case SEEKWAIT:
18175b81b6b3SRodney W. Grimes 		/* allow heads to settle */
18186182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 16);
18195b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
18205b81b6b3SRodney W. Grimes 		return(0);	/* will return later */
18215b81b6b3SRodney W. Grimes 	case SEEKCOMPLETE : /* SEEK DONE, START DMA */
18225b81b6b3SRodney W. Grimes 		/* Make sure seek really happened*/
18236182fdbdSPeter Wemm 		if(fd->track == FD_NO_TRACK) {
1824bb6382faSJoerg Wunsch 			int descyl = b_cylinder * fd->ft->steptrac;
18253a2f7427SDavid Greenman 			do {
18263a2f7427SDavid Greenman 				/*
1827dc5df763SJoerg Wunsch 				 * This might be a "ready changed" interrupt,
1828dc5df763SJoerg Wunsch 				 * which cannot really happen since the
1829dc5df763SJoerg Wunsch 				 * RDY pin is hardwired to + 5 volts.  This
1830dc5df763SJoerg Wunsch 				 * generally indicates a "bouncing" intr
1831dc5df763SJoerg Wunsch 				 * line, so do one of the following:
1832dc5df763SJoerg Wunsch 				 *
1833dc5df763SJoerg Wunsch 				 * When running on an enhanced FDC that is
1834dc5df763SJoerg Wunsch 				 * known to not go stuck after responding
1835dc5df763SJoerg Wunsch 				 * with INVALID, fetch all interrupt states
1836dc5df763SJoerg Wunsch 				 * until seeing either an INVALID or a
1837dc5df763SJoerg Wunsch 				 * real interrupt condition.
1838dc5df763SJoerg Wunsch 				 *
1839dc5df763SJoerg Wunsch 				 * When running on a dumb old NE765, give
1840dc5df763SJoerg Wunsch 				 * up immediately.  The controller will
1841dc5df763SJoerg Wunsch 				 * provide up to four dummy RC interrupt
1842dc5df763SJoerg Wunsch 				 * conditions right after reset (for the
1843dc5df763SJoerg Wunsch 				 * corresponding four drives), so this is
1844dc5df763SJoerg Wunsch 				 * our only chance to get notice that it
1845dc5df763SJoerg Wunsch 				 * was not the FDC that caused the interrupt.
18463a2f7427SDavid Greenman 				 */
1847dc5df763SJoerg Wunsch 				if (fd_sense_int(fdc, &st0, &cyl)
1848dc5df763SJoerg Wunsch 				    == FD_NOT_VALID)
1849dc5df763SJoerg Wunsch 					return 0;
1850dc5df763SJoerg Wunsch 				if(fdc->fdct == FDC_NE765
1851dc5df763SJoerg Wunsch 				   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
1852dc5df763SJoerg Wunsch 					return 0; /* hope for a real intr */
18533a2f7427SDavid Greenman 			} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
1854dc5df763SJoerg Wunsch 
18556182fdbdSPeter Wemm 			if (0 == descyl) {
1856dc5df763SJoerg Wunsch 				int failed = 0;
18573a2f7427SDavid Greenman 				/*
18583a2f7427SDavid Greenman 				 * seek to cyl 0 requested; make sure we are
18593a2f7427SDavid Greenman 				 * really there
18603a2f7427SDavid Greenman 				 */
1861dc5df763SJoerg Wunsch 				if (fd_sense_drive_status(fdc, &st3))
1862dc5df763SJoerg Wunsch 					failed = 1;
18633a2f7427SDavid Greenman 				if ((st3 & NE7_ST3_T0) == 0) {
18643a2f7427SDavid Greenman 					printf(
18653a2f7427SDavid Greenman 		"fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n",
18663a2f7427SDavid Greenman 					       fdu, st3, NE7_ST3BITS);
1867dc5df763SJoerg Wunsch 					failed = 1;
1868dc5df763SJoerg Wunsch 				}
1869dc5df763SJoerg Wunsch 
18706182fdbdSPeter Wemm 				if (failed) {
18713a2f7427SDavid Greenman 					if(fdc->retry < 3)
18723a2f7427SDavid Greenman 						fdc->retry = 3;
18736182fdbdSPeter Wemm 					return (retrier(fdc));
18743a2f7427SDavid Greenman 				}
18753a2f7427SDavid Greenman 			}
1876dc5df763SJoerg Wunsch 
18776182fdbdSPeter Wemm 			if (cyl != descyl) {
18783a2f7427SDavid Greenman 				printf(
18793a2f7427SDavid Greenman 		"fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n",
18802d9d0204SRodney W. Grimes 				       fdu, descyl, cyl, st0);
1881e5d7d243SBruce Evans 				if (fdc->retry < 3)
1882e5d7d243SBruce Evans 					fdc->retry = 3;
18836182fdbdSPeter Wemm 				return (retrier(fdc));
18845b81b6b3SRodney W. Grimes 			}
18855b81b6b3SRodney W. Grimes 		}
18865b81b6b3SRodney W. Grimes 
1887bb6382faSJoerg Wunsch 		fd->track = b_cylinder;
18883b178206SWarner Losh 		if (!(fdc->flags & FDC_NODMA))
18898177437dSPoul-Henning Kamp 			isa_dmastart(idf, bp->bio_data+fd->skip,
18908177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
18915b81b6b3SRodney W. Grimes 		sectrac = fd->ft->sectrac;
18925b81b6b3SRodney W. Grimes 		sec = blknum %  (sectrac * fd->ft->heads);
18935b81b6b3SRodney W. Grimes 		head = sec / sectrac;
18945b81b6b3SRodney W. Grimes 		sec = sec % sectrac + 1;
18953a2f7427SDavid Greenman 		fd->hddrv = ((head&1)<<2)+fdu;
18963a2f7427SDavid Greenman 
18973a2f7427SDavid Greenman 		if(format || !read)
18983a2f7427SDavid Greenman 		{
18993a2f7427SDavid Greenman 			/* make sure the drive is writable */
1900dc5df763SJoerg Wunsch 			if(fd_sense_drive_status(fdc, &st3) != 0)
1901dc8603e3SJoerg Wunsch 			{
1902dc8603e3SJoerg Wunsch 				/* stuck controller? */
19035f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
190456a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19058177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19068177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19075c1a1eaeSBruce Evans 						    fdc->dmachan);
1908dc8603e3SJoerg Wunsch 				fdc->retry = 6;	/* reset the beast */
19096182fdbdSPeter Wemm 				return (retrier(fdc));
1910dc8603e3SJoerg Wunsch 			}
19113a2f7427SDavid Greenman 			if(st3 & NE7_ST3_WP)
19123a2f7427SDavid Greenman 			{
19133a2f7427SDavid Greenman 				/*
19143a2f7427SDavid Greenman 				 * XXX YES! this is ugly.
19153a2f7427SDavid Greenman 				 * in order to force the current operation
19163a2f7427SDavid Greenman 				 * to fail, we will have to fake an FDC
19173a2f7427SDavid Greenman 				 * error - all error handling is done
19183a2f7427SDavid Greenman 				 * by the retrier()
19193a2f7427SDavid Greenman 				 */
19203a2f7427SDavid Greenman 				fdc->status[0] = NE7_ST0_IC_AT;
19213a2f7427SDavid Greenman 				fdc->status[1] = NE7_ST1_NW;
19223a2f7427SDavid Greenman 				fdc->status[2] = 0;
19233a2f7427SDavid Greenman 				fdc->status[3] = fd->track;
19243a2f7427SDavid Greenman 				fdc->status[4] = head;
19253a2f7427SDavid Greenman 				fdc->status[5] = sec;
19263a2f7427SDavid Greenman 				fdc->retry = 8;	/* break out immediately */
19273a2f7427SDavid Greenman 				fdc->state = IOTIMEDOUT; /* not really... */
19283a2f7427SDavid Greenman 				return (1);
19293a2f7427SDavid Greenman 			}
19303a2f7427SDavid Greenman 		}
19315b81b6b3SRodney W. Grimes 
19326182fdbdSPeter Wemm 		if (format) {
19335f830ea2SJoerg Wunsch 			if (fdc->flags & FDC_NODMA) {
19345f830ea2SJoerg Wunsch 				/*
19355f830ea2SJoerg Wunsch 				 * This seems to be necessary for
19365f830ea2SJoerg Wunsch 				 * whatever obscure reason; if we omit
19375f830ea2SJoerg Wunsch 				 * it, we end up filling the sector ID
19385f830ea2SJoerg Wunsch 				 * fields of the newly formatted track
19395f830ea2SJoerg Wunsch 				 * entirely with garbage, causing
19405f830ea2SJoerg Wunsch 				 * `wrong cylinder' errors all over
19415f830ea2SJoerg Wunsch 				 * the place when trying to read them
19425f830ea2SJoerg Wunsch 				 * back.
19435f830ea2SJoerg Wunsch 				 *
19445f830ea2SJoerg Wunsch 				 * Umpf.
19455f830ea2SJoerg Wunsch 				 */
19468177437dSPoul-Henning Kamp 				SET_BCDR(fdc, 1, bp->bio_bcount, 0);
19475f830ea2SJoerg Wunsch 
19488177437dSPoul-Henning Kamp 				(void)fdcpio(fdc,bp->bio_cmd,
19498177437dSPoul-Henning Kamp 					bp->bio_data+fd->skip,
19508177437dSPoul-Henning Kamp 					bp->bio_bcount);
19515f830ea2SJoerg Wunsch 
19525f830ea2SJoerg Wunsch 			}
1953b39c878eSAndrey A. Chernov 			/* formatting */
19546182fdbdSPeter Wemm 			if(fd_cmd(fdc, 6,  NE7CMD_FORMAT, head << 2 | fdu,
1955dc5df763SJoerg Wunsch 				  finfo->fd_formb_secshift,
1956dc5df763SJoerg Wunsch 				  finfo->fd_formb_nsecs,
1957dc5df763SJoerg Wunsch 				  finfo->fd_formb_gaplen,
19586182fdbdSPeter Wemm 				  finfo->fd_formb_fillbyte, 0)) {
1959dc8603e3SJoerg Wunsch 				/* controller fell over */
19605f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
196156a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19628177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
19638177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
19645c1a1eaeSBruce Evans 						    fdc->dmachan);
1965dc8603e3SJoerg Wunsch 				fdc->retry = 6;
19666182fdbdSPeter Wemm 				return (retrier(fdc));
1967dc8603e3SJoerg Wunsch 			}
19686182fdbdSPeter Wemm 		} else {
19693b178206SWarner Losh 			if (fdc->flags & FDC_NODMA) {
197069acd21dSWarner Losh 				/*
197169acd21dSWarner Losh 				 * this seems to be necessary even when
197269acd21dSWarner Losh 				 * reading data
197369acd21dSWarner Losh 				 */
19743b178206SWarner Losh 				SET_BCDR(fdc, 1, fdblk, 0);
197569acd21dSWarner Losh 
197669acd21dSWarner Losh 				/*
197769acd21dSWarner Losh 				 * perform the write pseudo-DMA before
197869acd21dSWarner Losh 				 * the WRITE command is sent
197969acd21dSWarner Losh 				 */
198069acd21dSWarner Losh 				if (!read)
19818177437dSPoul-Henning Kamp 					(void)fdcpio(fdc,bp->bio_cmd,
19828177437dSPoul-Henning Kamp 					    bp->bio_data+fd->skip,
198369acd21dSWarner Losh 					    fdblk);
198469acd21dSWarner Losh 			}
19856182fdbdSPeter Wemm 			if (fd_cmd(fdc, 9,
1986dc5df763SJoerg Wunsch 				   (read ? NE7CMD_READ : NE7CMD_WRITE),
1987dc5df763SJoerg Wunsch 				   head << 2 | fdu,  /* head & unit */
1988dc5df763SJoerg Wunsch 				   fd->track,        /* track */
1989dc5df763SJoerg Wunsch 				   head,
1990dc5df763SJoerg Wunsch 				   sec,              /* sector + 1 */
1991dc5df763SJoerg Wunsch 				   fd->ft->secsize,  /* sector size */
1992dc5df763SJoerg Wunsch 				   sectrac,          /* sectors/track */
1993dc5df763SJoerg Wunsch 				   fd->ft->gap,      /* gap size */
1994dc5df763SJoerg Wunsch 				   fd->ft->datalen,  /* data length */
19956182fdbdSPeter Wemm 				   0)) {
1996dc8603e3SJoerg Wunsch 				/* the beast is sleeping again */
19975f830ea2SJoerg Wunsch 				if (!(fdc->flags & FDC_NODMA))
199856a23089SPoul-Henning Kamp 					isa_dmadone(idf,
19998177437dSPoul-Henning Kamp 						    bp->bio_data + fd->skip,
20008177437dSPoul-Henning Kamp 						    format ? bp->bio_bcount : fdblk,
20015c1a1eaeSBruce Evans 						    fdc->dmachan);
2002dc8603e3SJoerg Wunsch 				fdc->retry = 6;
20036182fdbdSPeter Wemm 				return (retrier(fdc));
20045b81b6b3SRodney W. Grimes 			}
2005b39c878eSAndrey A. Chernov 		}
20063b178206SWarner Losh 		if (fdc->flags & FDC_NODMA)
200769acd21dSWarner Losh 			/*
200869acd21dSWarner Losh 			 * if this is a read, then simply await interrupt
200969acd21dSWarner Losh 			 * before performing PIO
201069acd21dSWarner Losh 			 */
20118177437dSPoul-Henning Kamp 			if (read && !fdcpio(fdc,bp->bio_cmd,
20128177437dSPoul-Henning Kamp 			    bp->bio_data+fd->skip,fdblk)) {
20133b178206SWarner Losh 				fd->tohandle = timeout(fd_iotimeout, fdc, hz);
201469acd21dSWarner Losh 				return(0);      /* will return later */
201569acd21dSWarner Losh 			};
201669acd21dSWarner Losh 
201769acd21dSWarner Losh 		/*
201869acd21dSWarner Losh 		 * write (or format) operation will fall through and
201969acd21dSWarner Losh 		 * await completion interrupt
202069acd21dSWarner Losh 		 */
20215b81b6b3SRodney W. Grimes 		fdc->state = IOCOMPLETE;
20226182fdbdSPeter Wemm 		fd->tohandle = timeout(fd_iotimeout, fdc, hz);
20235b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
202469acd21dSWarner Losh 	case PIOREAD:
202569acd21dSWarner Losh 		/*
202669acd21dSWarner Losh 		 * actually perform the PIO read.  The IOCOMPLETE case
202769acd21dSWarner Losh 		 * removes the timeout for us.
202869acd21dSWarner Losh 		 */
20298177437dSPoul-Henning Kamp 		(void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk);
203069acd21dSWarner Losh 		fdc->state = IOCOMPLETE;
203169acd21dSWarner Losh 		/* FALLTHROUGH */
20325b81b6b3SRodney W. Grimes 	case IOCOMPLETE: /* IO DONE, post-analyze */
20336182fdbdSPeter Wemm 		untimeout(fd_iotimeout, fdc, fd->tohandle);
2034dc5df763SJoerg Wunsch 
20356182fdbdSPeter Wemm 		if (fd_read_status(fdc, fd->fdsu)) {
20365f830ea2SJoerg Wunsch 			if (!(fdc->flags & FDC_NODMA))
20378177437dSPoul-Henning Kamp 				isa_dmadone(idf, bp->bio_data + fd->skip,
20388177437dSPoul-Henning Kamp 					    format ? bp->bio_bcount : fdblk,
20395c1a1eaeSBruce Evans 					    fdc->dmachan);
2040dc5df763SJoerg Wunsch 			if (fdc->retry < 6)
2041dc5df763SJoerg Wunsch 				fdc->retry = 6;	/* force a reset */
20426182fdbdSPeter Wemm 			return (retrier(fdc));
20435b81b6b3SRodney W. Grimes   		}
2044dc5df763SJoerg Wunsch 
20453a2f7427SDavid Greenman 		fdc->state = IOTIMEDOUT;
2046dc5df763SJoerg Wunsch 
20473a2f7427SDavid Greenman 		/* FALLTHROUGH */
2048dc5df763SJoerg Wunsch 
20493a2f7427SDavid Greenman 	case IOTIMEDOUT:
20503b178206SWarner Losh 		if (!(fdc->flags & FDC_NODMA))
20518177437dSPoul-Henning Kamp 			isa_dmadone(idf, bp->bio_data + fd->skip,
20528177437dSPoul-Henning Kamp 				format ? bp->bio_bcount : fdblk, fdc->dmachan);
20536182fdbdSPeter Wemm 		if (fdc->status[0] & NE7_ST0_IC) {
20543a2f7427SDavid Greenman                         if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20553a2f7427SDavid Greenman 			    && fdc->status[1] & NE7_ST1_OR) {
2056b39c878eSAndrey A. Chernov                                 /*
20573a2f7427SDavid Greenman 				 * DMA overrun. Someone hogged the bus
20583a2f7427SDavid Greenman 				 * and didn't release it in time for the
20593a2f7427SDavid Greenman 				 * next FDC transfer.
20603a2f7427SDavid Greenman 				 * Just restart it, don't increment retry
20613a2f7427SDavid Greenman 				 * count. (vak)
2062b39c878eSAndrey A. Chernov                                  */
2063b39c878eSAndrey A. Chernov                                 fdc->state = SEEKCOMPLETE;
2064b39c878eSAndrey A. Chernov                                 return (1);
2065b39c878eSAndrey A. Chernov                         }
20663a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV
20673a2f7427SDavid Greenman 				&& fdc->retry < 6)
20683a2f7427SDavid Greenman 				fdc->retry = 6;	/* force a reset */
20693a2f7427SDavid Greenman 			else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT
20703a2f7427SDavid Greenman 				&& fdc->status[2] & NE7_ST2_WC
20713a2f7427SDavid Greenman 				&& fdc->retry < 3)
20723a2f7427SDavid Greenman 				fdc->retry = 3;	/* force recalibrate */
20736182fdbdSPeter Wemm 			return (retrier(fdc));
20745b81b6b3SRodney W. Grimes 		}
20755b81b6b3SRodney W. Grimes 		/* All OK */
20763a2f7427SDavid Greenman 		fd->skip += fdblk;
20778177437dSPoul-Henning Kamp 		if (!format && fd->skip < bp->bio_bcount - bp->bio_resid) {
20785b81b6b3SRodney W. Grimes 			/* set up next transfer */
20795b81b6b3SRodney W. Grimes 			fdc->state = DOSEEK;
20806182fdbdSPeter Wemm 		} else {
20815b81b6b3SRodney W. Grimes 			/* ALL DONE */
20825b81b6b3SRodney W. Grimes 			fd->skip = 0;
2083e93e63cbSBruce Evans 			fdc->bp = NULL;
20845f830ea2SJoerg Wunsch 			device_unbusy(fd->dev);
20858177437dSPoul-Henning Kamp 			devstat_end_transaction_bio(&fd->device_stats, bp);
20865b81b6b3SRodney W. Grimes 			biodone(bp);
20875b81b6b3SRodney W. Grimes 			fdc->fd = (fd_p) 0;
20885b81b6b3SRodney W. Grimes 			fdc->fdu = -1;
20895b81b6b3SRodney W. Grimes 			fdc->state = FINDWORK;
20905b81b6b3SRodney W. Grimes 		}
20915b81b6b3SRodney W. Grimes 		return (1);
20925b81b6b3SRodney W. Grimes 	case RESETCTLR:
20933a2f7427SDavid Greenman 		fdc_reset(fdc);
20945b81b6b3SRodney W. Grimes 		fdc->retry++;
20955c1a1eaeSBruce Evans 		fdc->state = RESETCOMPLETE;
20965c1a1eaeSBruce Evans 		return (0);
20975c1a1eaeSBruce Evans 	case RESETCOMPLETE:
20985c1a1eaeSBruce Evans 		/*
20995c1a1eaeSBruce Evans 		 * Discard all the results from the reset so that they
21005c1a1eaeSBruce Evans 		 * can't cause an unexpected interrupt later.
21015c1a1eaeSBruce Evans 		 */
21020e317d05SJoerg Wunsch 		for (i = 0; i < 4; i++)
21030e317d05SJoerg Wunsch 			(void)fd_sense_int(fdc, &st0, &cyl);
21045c1a1eaeSBruce Evans 		fdc->state = STARTRECAL;
21055c1a1eaeSBruce Evans 		/* Fall through. */
21065c1a1eaeSBruce Evans 	case STARTRECAL:
21076182fdbdSPeter Wemm 		if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) {
2108dc8603e3SJoerg Wunsch 			/* arrgl */
2109dc8603e3SJoerg Wunsch 			fdc->retry = 6;
21106182fdbdSPeter Wemm 			return (retrier(fdc));
2111dc8603e3SJoerg Wunsch 		}
21125b81b6b3SRodney W. Grimes 		fdc->state = RECALWAIT;
21135b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
21145b81b6b3SRodney W. Grimes 	case RECALWAIT:
21155b81b6b3SRodney W. Grimes 		/* allow heads to settle */
21166182fdbdSPeter Wemm 		timeout(fd_pseudointr, fdc, hz / 8);
21175b81b6b3SRodney W. Grimes 		fdc->state = RECALCOMPLETE;
21185b81b6b3SRodney W. Grimes 		return (0);	/* will return later */
21195b81b6b3SRodney W. Grimes 	case RECALCOMPLETE:
21203a2f7427SDavid Greenman 		do {
21213a2f7427SDavid Greenman 			/*
2122dc5df763SJoerg Wunsch 			 * See SEEKCOMPLETE for a comment on this:
21233a2f7427SDavid Greenman 			 */
2124dc5df763SJoerg Wunsch 			if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
2125dc5df763SJoerg Wunsch 				return 0;
2126dc5df763SJoerg Wunsch 			if(fdc->fdct == FDC_NE765
2127dc5df763SJoerg Wunsch 			   && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC)
2128dc5df763SJoerg Wunsch 				return 0; /* hope for a real intr */
21293a2f7427SDavid Greenman 		} while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC);
21303a2f7427SDavid Greenman 		if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0)
21315b81b6b3SRodney W. Grimes 		{
2132dc8603e3SJoerg Wunsch 			if(fdc->retry > 3)
2133dc8603e3SJoerg Wunsch 				/*
2134dc8603e3SJoerg Wunsch 				 * a recalibrate from beyond cylinder 77
2135dc8603e3SJoerg Wunsch 				 * will "fail" due to the FDC limitations;
2136dc8603e3SJoerg Wunsch 				 * since people used to complain much about
2137dc8603e3SJoerg Wunsch 				 * the failure message, try not logging
2138dc8603e3SJoerg Wunsch 				 * this one if it seems to be the first
2139dc8603e3SJoerg Wunsch 				 * time in a line
2140dc8603e3SJoerg Wunsch 				 */
2141dc8603e3SJoerg Wunsch 				printf("fd%d: recal failed ST0 %b cyl %d\n",
2142dc8603e3SJoerg Wunsch 				       fdu, st0, NE7_ST0BITS, cyl);
21433a2f7427SDavid Greenman 			if(fdc->retry < 3) fdc->retry = 3;
21446182fdbdSPeter Wemm 			return (retrier(fdc));
21455b81b6b3SRodney W. Grimes 		}
21465b81b6b3SRodney W. Grimes 		fd->track = 0;
21475b81b6b3SRodney W. Grimes 		/* Seek (probably) necessary */
21485b81b6b3SRodney W. Grimes 		fdc->state = DOSEEK;
21495b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21505b81b6b3SRodney W. Grimes 	case MOTORWAIT:
21515b81b6b3SRodney W. Grimes 		if(fd->flags & FD_MOTOR_WAIT)
21525b81b6b3SRodney W. Grimes 		{
21535b81b6b3SRodney W. Grimes 			return (0); /* time's not up yet */
21545b81b6b3SRodney W. Grimes 		}
21555c1a1eaeSBruce Evans 		if (fdc->flags & FDC_NEEDS_RESET) {
21565c1a1eaeSBruce Evans 			fdc->state = RESETCTLR;
21575c1a1eaeSBruce Evans 			fdc->flags &= ~FDC_NEEDS_RESET;
21585c1a1eaeSBruce Evans 		} else {
21595e235068SJordan K. Hubbard 			/*
21605c1a1eaeSBruce Evans 			 * If all motors were off, then the controller was
21615c1a1eaeSBruce Evans 			 * reset, so it has lost track of the current
21625c1a1eaeSBruce Evans 			 * cylinder.  Recalibrate to handle this case.
2163f86e4077SBruce Evans 			 * But first, discard the results of the reset.
21645e235068SJordan K. Hubbard 			 */
2165f86e4077SBruce Evans 			fdc->state = RESETCOMPLETE;
21665c1a1eaeSBruce Evans 		}
21675b81b6b3SRodney W. Grimes 		return (1);	/* will return immediatly */
21685b81b6b3SRodney W. Grimes 	default:
2169b6e5f28eSPeter Wemm 		device_printf(fdc->fdc_dev, "unexpected FD int->");
2170dc5df763SJoerg Wunsch 		if (fd_read_status(fdc, fd->fdsu) == 0)
2171a838d83dSBruce Evans 			printf("FDC status :%x %x %x %x %x %x %x   ",
21725b81b6b3SRodney W. Grimes 			       fdc->status[0],
21735b81b6b3SRodney W. Grimes 			       fdc->status[1],
21745b81b6b3SRodney W. Grimes 			       fdc->status[2],
21755b81b6b3SRodney W. Grimes 			       fdc->status[3],
21765b81b6b3SRodney W. Grimes 			       fdc->status[4],
21775b81b6b3SRodney W. Grimes 			       fdc->status[5],
21785b81b6b3SRodney W. Grimes 			       fdc->status[6] );
21793a2f7427SDavid Greenman 		else
2180dac0f2dbSJoerg Wunsch 			printf("No status available   ");
2181dac0f2dbSJoerg Wunsch 		if (fd_sense_int(fdc, &st0, &cyl) != 0)
2182dac0f2dbSJoerg Wunsch 		{
2183dac0f2dbSJoerg Wunsch 			printf("[controller is dead now]\n");
21845b81b6b3SRodney W. Grimes 			return (0);
21855b81b6b3SRodney W. Grimes 		}
2186dac0f2dbSJoerg Wunsch 		printf("ST0 = %x, PCN = %x\n", st0, cyl);
2187dac0f2dbSJoerg Wunsch 		return (0);
2188dac0f2dbSJoerg Wunsch 	}
2189dac0f2dbSJoerg Wunsch 	/*XXX confusing: some branches return immediately, others end up here*/
21905b81b6b3SRodney W. Grimes 	return (1); /* Come back immediatly to new state */
21915b81b6b3SRodney W. Grimes }
21925b81b6b3SRodney W. Grimes 
2193aaf08d94SGarrett Wollman static int
21946182fdbdSPeter Wemm retrier(struct fdc_data *fdc)
21955b81b6b3SRodney W. Grimes {
21968177437dSPoul-Henning Kamp 	struct bio *bp;
21976182fdbdSPeter Wemm 	struct fd_data *fd;
21986182fdbdSPeter Wemm 	int fdu;
21995b81b6b3SRodney W. Grimes 
2200e93e63cbSBruce Evans 	bp = fdc->bp;
22015b81b6b3SRodney W. Grimes 
22026182fdbdSPeter Wemm 	/* XXX shouldn't this be cached somewhere?  */
22038177437dSPoul-Henning Kamp 	fdu = FDUNIT(minor(bp->bio_dev));
22046182fdbdSPeter Wemm 	fd = devclass_get_softc(fd_devclass, fdu);
22056182fdbdSPeter Wemm 	if (fd->options & FDOPT_NORETRY)
22063a2f7427SDavid Greenman 		goto fail;
22076182fdbdSPeter Wemm 
22086182fdbdSPeter Wemm 	switch (fdc->retry) {
22095b81b6b3SRodney W. Grimes 	case 0: case 1: case 2:
22105b81b6b3SRodney W. Grimes 		fdc->state = SEEKCOMPLETE;
22115b81b6b3SRodney W. Grimes 		break;
22125b81b6b3SRodney W. Grimes 	case 3: case 4: case 5:
22135b81b6b3SRodney W. Grimes 		fdc->state = STARTRECAL;
22145b81b6b3SRodney W. Grimes 		break;
22155b81b6b3SRodney W. Grimes 	case 6:
22165b81b6b3SRodney W. Grimes 		fdc->state = RESETCTLR;
22175b81b6b3SRodney W. Grimes 		break;
22185b81b6b3SRodney W. Grimes 	case 7:
22195b81b6b3SRodney W. Grimes 		break;
22205b81b6b3SRodney W. Grimes 	default:
22213a2f7427SDavid Greenman 	fail:
22225b81b6b3SRodney W. Grimes 		{
22238177437dSPoul-Henning Kamp 			dev_t sav_bio_dev = bp->bio_dev;
22247ca0641bSAndrey A. Chernov 			/* Trick diskerr */
22258177437dSPoul-Henning Kamp 			bp->bio_dev = makedev(major(bp->bio_dev),
22268177437dSPoul-Henning Kamp 				    (FDUNIT(minor(bp->bio_dev))<<3)|RAW_PART);
2227cb7e609aSPoul-Henning Kamp 			diskerr(bp, "hard error", fdc->fd->skip / DEV_BSIZE,
22283a2f7427SDavid Greenman 				(struct disklabel *)NULL);
22298177437dSPoul-Henning Kamp 			bp->bio_dev = sav_bio_dev;
2230dc5df763SJoerg Wunsch 			if (fdc->flags & FDC_STAT_VALID)
2231dc5df763SJoerg Wunsch 			{
2232dc5df763SJoerg Wunsch 				printf(
2233a838d83dSBruce Evans 			" (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n",
2234dc5df763SJoerg Wunsch 				       fdc->status[0], NE7_ST0BITS,
2235dc5df763SJoerg Wunsch 				       fdc->status[1], NE7_ST1BITS,
2236dc5df763SJoerg Wunsch 				       fdc->status[2], NE7_ST2BITS,
2237dc5df763SJoerg Wunsch 				       fdc->status[3], fdc->status[4],
2238dc5df763SJoerg Wunsch 				       fdc->status[5]);
2239dc5df763SJoerg Wunsch 			}
2240dc5df763SJoerg Wunsch 			else
2241dc5df763SJoerg Wunsch 				printf(" (No status)\n");
22425b81b6b3SRodney W. Grimes 		}
22438177437dSPoul-Henning Kamp 		bp->bio_flags |= BIO_ERROR;
22448177437dSPoul-Henning Kamp 		bp->bio_error = EIO;
22458177437dSPoul-Henning Kamp 		bp->bio_resid += bp->bio_bcount - fdc->fd->skip;
2246e93e63cbSBruce Evans 		fdc->bp = NULL;
22475b81b6b3SRodney W. Grimes 		fdc->fd->skip = 0;
22485f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
22498177437dSPoul-Henning Kamp 		devstat_end_transaction_bio(&fdc->fd->device_stats, bp);
22505b81b6b3SRodney W. Grimes 		biodone(bp);
225192ed385aSRodney W. Grimes 		fdc->state = FINDWORK;
22525c1a1eaeSBruce Evans 		fdc->flags |= FDC_NEEDS_RESET;
22535b81b6b3SRodney W. Grimes 		fdc->fd = (fd_p) 0;
22545b81b6b3SRodney W. Grimes 		fdc->fdu = -1;
225592ed385aSRodney W. Grimes 		return (1);
22565b81b6b3SRodney W. Grimes 	}
22575b81b6b3SRodney W. Grimes 	fdc->retry++;
22585b81b6b3SRodney W. Grimes 	return (1);
22595b81b6b3SRodney W. Grimes }
22605b81b6b3SRodney W. Grimes 
22611fdb6e6cSPoul-Henning Kamp static void
22621fdb6e6cSPoul-Henning Kamp fdbiodone(struct bio *bp)
22631fdb6e6cSPoul-Henning Kamp {
22641fdb6e6cSPoul-Henning Kamp 	wakeup(bp);
22651fdb6e6cSPoul-Henning Kamp }
22661fdb6e6cSPoul-Henning Kamp 
2267b39c878eSAndrey A. Chernov static int
2268b39c878eSAndrey A. Chernov fdformat(dev, finfo, p)
2269b39c878eSAndrey A. Chernov 	dev_t dev;
2270b39c878eSAndrey A. Chernov 	struct fd_formb *finfo;
2271b39c878eSAndrey A. Chernov 	struct proc *p;
2272b39c878eSAndrey A. Chernov {
2273b39c878eSAndrey A. Chernov  	fdu_t	fdu;
2274b39c878eSAndrey A. Chernov  	fd_p	fd;
2275b39c878eSAndrey A. Chernov 
22761fdb6e6cSPoul-Henning Kamp 	struct bio *bp;
2277b39c878eSAndrey A. Chernov 	int rv = 0, s;
22783a2f7427SDavid Greenman 	size_t fdblk;
2279b39c878eSAndrey A. Chernov 
2280b39c878eSAndrey A. Chernov  	fdu	= FDUNIT(minor(dev));
22816182fdbdSPeter Wemm 	fd	= devclass_get_softc(fd_devclass, fdu);
22823a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2283b39c878eSAndrey A. Chernov 
2284b39c878eSAndrey A. Chernov 	/* set up a buffer header for fdstrategy() */
22851fdb6e6cSPoul-Henning Kamp 	bp = (struct bio *)malloc(sizeof(struct bio), M_TEMP, M_NOWAIT);
2286b39c878eSAndrey A. Chernov 	if(bp == 0)
22871fdb6e6cSPoul-Henning Kamp 		return ENOMEM;
228882f5379bSJoerg Wunsch 	/*
228982f5379bSJoerg Wunsch 	 * keep the process from being swapped
229082f5379bSJoerg Wunsch 	 */
22912ada239cSPeter Wemm 	PHOLD(p);
22921fdb6e6cSPoul-Henning Kamp 	bzero((void *)bp, sizeof(*bp));
22931fdb6e6cSPoul-Henning Kamp 	bp->bio_cmd = BIO_FORMAT;
2294b39c878eSAndrey A. Chernov 
2295b39c878eSAndrey A. Chernov 	/*
2296b39c878eSAndrey A. Chernov 	 * calculate a fake blkno, so fdstrategy() would initiate a
2297b39c878eSAndrey A. Chernov 	 * seek to the requested cylinder
2298b39c878eSAndrey A. Chernov 	 */
22991fdb6e6cSPoul-Henning Kamp 	bp->bio_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads)
23003a2f7427SDavid Greenman 		+ finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE;
2301b39c878eSAndrey A. Chernov 
23021fdb6e6cSPoul-Henning Kamp 	bp->bio_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
23031fdb6e6cSPoul-Henning Kamp 	bp->bio_data = (caddr_t)finfo;
2304b39c878eSAndrey A. Chernov 
2305b39c878eSAndrey A. Chernov 	/* now do the format */
23061fdb6e6cSPoul-Henning Kamp 	bp->bio_dev = dev;
23071fdb6e6cSPoul-Henning Kamp 	bp->bio_done = fdbiodone;
23081fdb6e6cSPoul-Henning Kamp 	fdstrategy(bp);
2309b39c878eSAndrey A. Chernov 
2310b39c878eSAndrey A. Chernov 	/* ...and wait for it to complete */
2311b39c878eSAndrey A. Chernov 	s = splbio();
23121fdb6e6cSPoul-Henning Kamp 	while(!(bp->bio_flags & BIO_DONE)) {
23135e235068SJordan K. Hubbard 		rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2314b39c878eSAndrey A. Chernov 		if (rv == EWOULDBLOCK)
2315b39c878eSAndrey A. Chernov 			break;
2316b39c878eSAndrey A. Chernov 	}
2317b39c878eSAndrey A. Chernov 	splx(s);
2318b39c878eSAndrey A. Chernov 
231982f5379bSJoerg Wunsch 	if (rv == EWOULDBLOCK) {
2320b39c878eSAndrey A. Chernov 		/* timed out */
2321b39c878eSAndrey A. Chernov 		rv = EIO;
23225f830ea2SJoerg Wunsch 		device_unbusy(fd->dev);
232382f5379bSJoerg Wunsch 	}
23241fdb6e6cSPoul-Henning Kamp 	if (bp->bio_flags & BIO_ERROR)
23251fdb6e6cSPoul-Henning Kamp 		rv = bp->bio_error;
232682f5379bSJoerg Wunsch 	/*
232782f5379bSJoerg Wunsch 	 * allow the process to be swapped
232882f5379bSJoerg Wunsch 	 */
23292ada239cSPeter Wemm 	PRELE(p);
2330b39c878eSAndrey A. Chernov 	free(bp, M_TEMP);
2331b39c878eSAndrey A. Chernov 	return rv;
2332b39c878eSAndrey A. Chernov }
2333b39c878eSAndrey A. Chernov 
2334f5f7ba03SJordan K. Hubbard /*
2335671e2ceeSBruce Evans  * TODO: don't allocate buffer on stack.
2336f5f7ba03SJordan K. Hubbard  */
23375b81b6b3SRodney W. Grimes 
23383e425b96SJulian Elischer static int
2339b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p)
2340f5f7ba03SJordan K. Hubbard 	dev_t dev;
2341ecbb00a2SDoug Rabson 	u_long cmd;
2342f5f7ba03SJordan K. Hubbard 	caddr_t addr;
2343f5f7ba03SJordan K. Hubbard 	int flag;
2344b39c878eSAndrey A. Chernov 	struct proc *p;
2345f5f7ba03SJordan K. Hubbard {
23463a2f7427SDavid Greenman  	fdu_t	fdu = FDUNIT(minor(dev));
23476182fdbdSPeter Wemm  	fd_p	fd = devclass_get_softc(fd_devclass, fdu);
23483a2f7427SDavid Greenman 	size_t fdblk;
23493a2f7427SDavid Greenman 
2350f5f7ba03SJordan K. Hubbard 	struct fd_type *fdt;
2351f5f7ba03SJordan K. Hubbard 	struct disklabel *dl;
2352f5f7ba03SJordan K. Hubbard 	char buffer[DEV_BSIZE];
23533a2f7427SDavid Greenman 	int error = 0;
2354f5f7ba03SJordan K. Hubbard 
23553a2f7427SDavid Greenman 	fdblk = 128 << fd->ft->secsize;
2356f5f7ba03SJordan K. Hubbard 
23576182fdbdSPeter Wemm 	switch (cmd) {
2358f5f7ba03SJordan K. Hubbard 	case DIOCGDINFO:
2359f5f7ba03SJordan K. Hubbard 		bzero(buffer, sizeof (buffer));
2360f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)buffer;
23613a2f7427SDavid Greenman 		dl->d_secsize = fdblk;
23626182fdbdSPeter Wemm 		fdt = fd->ft;
2363f5f7ba03SJordan K. Hubbard 		dl->d_secpercyl = fdt->size / fdt->tracks;
2364f5f7ba03SJordan K. Hubbard 		dl->d_type = DTYPE_FLOPPY;
2365f5f7ba03SJordan K. Hubbard 
236649ff4debSPoul-Henning Kamp 		if (readdisklabel(dkmodpart(dev, RAW_PART), dl)
2367191e1a59SBruce Evans 		    == NULL)
2368f5f7ba03SJordan K. Hubbard 			error = 0;
2369f5f7ba03SJordan K. Hubbard 		else
2370f5f7ba03SJordan K. Hubbard 			error = EINVAL;
2371f5f7ba03SJordan K. Hubbard 
2372f5f7ba03SJordan K. Hubbard 		*(struct disklabel *)addr = *dl;
2373f5f7ba03SJordan K. Hubbard 		break;
2374f5f7ba03SJordan K. Hubbard 
2375f5f7ba03SJordan K. Hubbard 	case DIOCSDINFO:
2376f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2377f5f7ba03SJordan K. Hubbard 			error = EBADF;
2378f5f7ba03SJordan K. Hubbard 		break;
2379f5f7ba03SJordan K. Hubbard 
2380f5f7ba03SJordan K. Hubbard 	case DIOCWLABEL:
2381f5f7ba03SJordan K. Hubbard 		if ((flag & FWRITE) == 0)
2382f5f7ba03SJordan K. Hubbard 			error = EBADF;
2383f5f7ba03SJordan K. Hubbard 		break;
2384f5f7ba03SJordan K. Hubbard 
2385f5f7ba03SJordan K. Hubbard 	case DIOCWDINFO:
23866182fdbdSPeter Wemm 		if ((flag & FWRITE) == 0) {
2387f5f7ba03SJordan K. Hubbard 			error = EBADF;
2388f5f7ba03SJordan K. Hubbard 			break;
2389f5f7ba03SJordan K. Hubbard 		}
2390f5f7ba03SJordan K. Hubbard 
2391f5f7ba03SJordan K. Hubbard 		dl = (struct disklabel *)addr;
2392f5f7ba03SJordan K. Hubbard 
2393191e1a59SBruce Evans 		if ((error = setdisklabel((struct disklabel *)buffer, dl,
2394191e1a59SBruce Evans 					  (u_long)0)) != 0)
2395f5f7ba03SJordan K. Hubbard 			break;
2396f5f7ba03SJordan K. Hubbard 
239749ff4debSPoul-Henning Kamp 		error = writedisklabel(dev, (struct disklabel *)buffer);
2398b39c878eSAndrey A. Chernov 		break;
2399b39c878eSAndrey A. Chernov 	case FD_FORM:
2400b39c878eSAndrey A. Chernov 		if ((flag & FWRITE) == 0)
2401b39c878eSAndrey A. Chernov 			error = EBADF;	/* must be opened for writing */
2402b39c878eSAndrey A. Chernov 		else if (((struct fd_formb *)addr)->format_version !=
2403b39c878eSAndrey A. Chernov 			FD_FORMAT_VERSION)
2404b39c878eSAndrey A. Chernov 			error = EINVAL;	/* wrong version of formatting prog */
2405b39c878eSAndrey A. Chernov 		else
2406b39c878eSAndrey A. Chernov 			error = fdformat(dev, (struct fd_formb *)addr, p);
2407b39c878eSAndrey A. Chernov 		break;
2408b39c878eSAndrey A. Chernov 
2409b39c878eSAndrey A. Chernov 	case FD_GTYPE:                  /* get drive type */
24103e425b96SJulian Elischer 		*(struct fd_type *)addr = *fd->ft;
2411f5f7ba03SJordan K. Hubbard 		break;
2412f5f7ba03SJordan K. Hubbard 
24133a2f7427SDavid Greenman 	case FD_STYPE:                  /* set drive type */
24143a2f7427SDavid Greenman 		/* this is considered harmful; only allow for superuser */
2415f711d546SPoul-Henning Kamp 		if (suser(p) != 0)
24163a2f7427SDavid Greenman 			return EPERM;
24173e425b96SJulian Elischer 		*fd->ft = *(struct fd_type *)addr;
24183a2f7427SDavid Greenman 		break;
24193a2f7427SDavid Greenman 
24203a2f7427SDavid Greenman 	case FD_GOPTS:			/* get drive options */
24213e425b96SJulian Elischer 		*(int *)addr = fd->options;
24223a2f7427SDavid Greenman 		break;
24233a2f7427SDavid Greenman 
24243a2f7427SDavid Greenman 	case FD_SOPTS:			/* set drive options */
24253e425b96SJulian Elischer 		fd->options = *(int *)addr;
24263a2f7427SDavid Greenman 		break;
24273a2f7427SDavid Greenman 
2428f5f7ba03SJordan K. Hubbard 	default:
24293a2f7427SDavid Greenman 		error = ENOTTY;
2430f5f7ba03SJordan K. Hubbard 		break;
2431f5f7ba03SJordan K. Hubbard 	}
2432f5f7ba03SJordan K. Hubbard 	return (error);
2433f5f7ba03SJordan K. Hubbard }
2434f5f7ba03SJordan K. Hubbard 
24353a2f7427SDavid Greenman /*
24363a2f7427SDavid Greenman  * Hello emacs, these are the
24373a2f7427SDavid Greenman  * Local Variables:
24383a2f7427SDavid Greenman  *  c-indent-level:               8
24393a2f7427SDavid Greenman  *  c-continued-statement-offset: 8
24403a2f7427SDavid Greenman  *  c-continued-brace-offset:     0
24413a2f7427SDavid Greenman  *  c-brace-offset:              -8
24423a2f7427SDavid Greenman  *  c-brace-imaginary-offset:     0
24433a2f7427SDavid Greenman  *  c-argdecl-indent:             8
24443a2f7427SDavid Greenman  *  c-label-offset:              -8
24453a2f7427SDavid Greenman  *  c++-hanging-braces:           1
24463a2f7427SDavid Greenman  *  c++-access-specifier-offset: -8
24473a2f7427SDavid Greenman  *  c++-empty-arglist-indent:     8
24483a2f7427SDavid Greenman  *  c++-friend-offset:            0
24493a2f7427SDavid Greenman  * End:
24503a2f7427SDavid Greenman  */
2451