xref: /freebsd/sys/dev/fdc/fdc_isa.c (revision 534e7194f80fc08577a369572c99e5e2aa304aaa)
10bdf1a55SNate Lawson /*-
26d6fa4fdSWarner Losh  * Copyright (c) 2004 M. Warner Losh.
36d6fa4fdSWarner Losh  * All rights reserved.
46d6fa4fdSWarner Losh  *
56d6fa4fdSWarner Losh  * Redistribution and use in source and binary forms, with or without
66d6fa4fdSWarner Losh  * modification, are permitted provided that the following conditions
76d6fa4fdSWarner Losh  * are met:
86d6fa4fdSWarner Losh  * 1. Redistributions of source code must retain the above copyright
96d6fa4fdSWarner Losh  *    notice, this list of conditions, and the following disclaimer,
106d6fa4fdSWarner Losh  *    without modification, immediately at the beginning of the file.
116d6fa4fdSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
126d6fa4fdSWarner Losh  *    notice, this list of conditions and the following disclaimer in
136d6fa4fdSWarner Losh  *    the documentation and/or other materials provided with the
146d6fa4fdSWarner Losh  *    distribution.
156d6fa4fdSWarner Losh  *
166d6fa4fdSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176d6fa4fdSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186d6fa4fdSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196d6fa4fdSWarner Losh  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
206d6fa4fdSWarner Losh  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216d6fa4fdSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226d6fa4fdSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236d6fa4fdSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246d6fa4fdSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256d6fa4fdSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266d6fa4fdSWarner Losh  * SUCH DAMAGE.
276d6fa4fdSWarner Losh  */
286d6fa4fdSWarner Losh 
296d6fa4fdSWarner Losh #include <sys/cdefs.h>
306d6fa4fdSWarner Losh __FBSDID("$FreeBSD$");
316d6fa4fdSWarner Losh 
326d6fa4fdSWarner Losh #include <sys/param.h>
336d6fa4fdSWarner Losh #include <sys/bio.h>
346d6fa4fdSWarner Losh #include <sys/bus.h>
356d6fa4fdSWarner Losh #include <sys/kernel.h>
361b67be7bSPoul-Henning Kamp #include <sys/lock.h>
376d6fa4fdSWarner Losh #include <sys/module.h>
381b67be7bSPoul-Henning Kamp #include <sys/mutex.h>
396d6fa4fdSWarner Losh #include <sys/rman.h>
406d6fa4fdSWarner Losh #include <sys/systm.h>
416d6fa4fdSWarner Losh 
42c3ae4c40SWarner Losh #include <machine/bus.h>
43c3ae4c40SWarner Losh 
446d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h>
456d6fa4fdSWarner Losh 
466d6fa4fdSWarner Losh #include <isa/isavar.h>
476d6fa4fdSWarner Losh #include <isa/isareg.h>
486d6fa4fdSWarner Losh 
496d6fa4fdSWarner Losh static int fdc_isa_probe(device_t);
506d6fa4fdSWarner Losh 
516d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = {
526d6fa4fdSWarner Losh 	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
536d6fa4fdSWarner Losh 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
546d6fa4fdSWarner Losh 	{0}
556d6fa4fdSWarner Losh };
566d6fa4fdSWarner Losh 
570bdf1a55SNate Lawson int
58aad64165SWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
59aad64165SWarner Losh {
60b4046cd7SWarner Losh 	int nports = 6;
61aad64165SWarner Losh 
6216629bd9SWarner Losh 	fdc->fdc_dev = dev;
63aad64165SWarner Losh 	fdc->rid_ioport = 0;
64aad64165SWarner Losh 	fdc->rid_irq = 0;
65aad64165SWarner Losh 	fdc->rid_drq = 0;
66aad64165SWarner Losh 	fdc->rid_ctl = 1;
67aad64165SWarner Losh 
68aad64165SWarner Losh 	/*
69aad64165SWarner Losh 	 * On standard ISA, we don't just use an 8 port range
70aad64165SWarner Losh 	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
71b4046cd7SWarner Losh 	 * register at 0x3f6.  So, on older hardware, we use
72b4046cd7SWarner Losh 	 * 0x3f0-0x3f5 and 0x3f7.  However, some BIOSs omit the
73b4046cd7SWarner Losh 	 * control port, while others start at 0x3f2.  Of the latter,
74b4046cd7SWarner Losh 	 * sometimes we have two resources, other times we have one.
75b4046cd7SWarner Losh 	 * We have to deal with the following cases:
76aad64165SWarner Losh 	 *
77b4046cd7SWarner Losh 	 * 1:	0x3f0-0x3f5			# very rare
78b4046cd7SWarner Losh 	 * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
79b4046cd7SWarner Losh 	 * 3:	0x3f0-0x3f5,0x3f7		# Most common
80b4046cd7SWarner Losh 	 * 4:	0x3f2-0x3f5,0x3f7		# Second most common
81b4046cd7SWarner Losh 	 * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
82b4046cd7SWarner Losh 	 * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
83b4046cd7SWarner Losh 	 * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
84083ba097SWarner Losh 	 * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
85534e7194SWarner Losh 	 * 9:	0x3f0-0x3f3,0x3f4-0x3f5,0x3f7
86b4046cd7SWarner Losh 	 *
87b4046cd7SWarner Losh 	 * The following code is generic for any value of 0x3fx :-)
88aad64165SWarner Losh 	 */
89aad64165SWarner Losh 
90aad64165SWarner Losh 	/*
91b4046cd7SWarner Losh 	 * First, allocated the main range of ports.  In the best of
92b4046cd7SWarner Losh 	 * worlds, this is 4 or 6 ports.  In others, well, that's
93b4046cd7SWarner Losh 	 * why this function is so complicated.
94aad64165SWarner Losh 	 */
95083ba097SWarner Losh again_ioport:
96aad64165SWarner Losh 	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
97aad64165SWarner Losh 	    &fdc->rid_ioport, 0ul, ~0ul, nports, RF_ACTIVE);
98aad64165SWarner Losh 	if (fdc->res_ioport == 0) {
99b4046cd7SWarner Losh 		device_printf(dev, "cannot allocate I/O port (%d ports)\n",
100aad64165SWarner Losh 		    nports);
101b4046cd7SWarner Losh 		return (ENXIO);
102aad64165SWarner Losh 	}
103534e7194SWarner Losh 	if ((rman_get_end(fdc->res_ioport) & 0x7) == 1) {
104083ba097SWarner Losh 		/* Case 8 */
105083ba097SWarner Losh 		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
106083ba097SWarner Losh 		    fdc->res_ioport);
107083ba097SWarner Losh 		fdc->rid_ioport++;
108083ba097SWarner Losh 		goto again_ioport;
109083ba097SWarner Losh 	}
110aad64165SWarner Losh 	fdc->portt = rman_get_bustag(fdc->res_ioport);
111aad64165SWarner Losh 	fdc->porth = rman_get_bushandle(fdc->res_ioport);
112aad64165SWarner Losh 
113aad64165SWarner Losh 	/*
114083ba097SWarner Losh 	 * Handle cases 4-8 above
115aad64165SWarner Losh 	 */
116b4046cd7SWarner Losh 	fdc->port_off = -(fdc->porth & 0x7);
117aad64165SWarner Losh 
118aad64165SWarner Losh 	/*
119534e7194SWarner Losh 	 * Deal with case 6-9: FDSTS and FDDATA.
120aad64165SWarner Losh 	 */
121534e7194SWarner Losh 	if ((rman_get_end(fdc->res_ioport) & 0x7) == 3) {
122083ba097SWarner Losh 		fdc->rid_sts = fdc->rid_ioport + 1;
123b4046cd7SWarner Losh 		fdc->res_sts = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
124b4046cd7SWarner Losh 		    &fdc->rid_sts, RF_ACTIVE);
125b4046cd7SWarner Losh 		if (fdc->res_sts == NULL) {
126b4046cd7SWarner Losh 			device_printf(dev, "Can't alloc rid 1");
127b4046cd7SWarner Losh 			fdc_release_resources(fdc);
128b4046cd7SWarner Losh 			return (ENXIO);
129b4046cd7SWarner Losh 		}
130b4046cd7SWarner Losh 		fdc->sts_off = -4;
131b4046cd7SWarner Losh 		fdc->stst = rman_get_bustag(fdc->res_sts);
132b4046cd7SWarner Losh 		fdc->stsh = rman_get_bushandle(fdc->res_sts);
133b4046cd7SWarner Losh 	} else {
134b4046cd7SWarner Losh 		fdc->res_sts = NULL;
135b4046cd7SWarner Losh 		fdc->sts_off = fdc->port_off;
136b4046cd7SWarner Losh 		fdc->stst = fdc->portt;
137b4046cd7SWarner Losh 		fdc->stsh = fdc->porth;
138aad64165SWarner Losh 	}
139aad64165SWarner Losh 
140aad64165SWarner Losh 	/*
141b4046cd7SWarner Losh 	 * allocate the control port.  For cases 1, 2, 5 and 7, we
142b4046cd7SWarner Losh 	 * fake it from the ioports resource.  XXX IS THIS THE RIGHT THING
143b4046cd7SWarner Losh 	 * TO DO, OR SHOULD WE CREATE A NEW RID? (I think we need a new rid)
144aad64165SWarner Losh 	 */
145083ba097SWarner Losh 	fdc->rid_ctl = fdc->rid_sts + 1;
146aad64165SWarner Losh 	fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
147aad64165SWarner Losh 	    &fdc->rid_ctl, RF_ACTIVE);
148b4046cd7SWarner Losh 	if (fdc->res_ctl == NULL) {
149b4046cd7SWarner Losh 		fdc->ctl_off = 7 + fdc->port_off;
150b4046cd7SWarner Losh 		fdc->res_ctl = NULL;
151b4046cd7SWarner Losh 		fdc->ctlt = fdc->portt;
152b4046cd7SWarner Losh 		fdc->ctlh = fdc->porth;
153b4046cd7SWarner Losh 	} else {
154b4046cd7SWarner Losh 		fdc->ctl_off = 0;
155aad64165SWarner Losh 		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
156aad64165SWarner Losh 		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
157b4046cd7SWarner Losh 	}
158aad64165SWarner Losh 
159aad64165SWarner Losh 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
160aad64165SWarner Losh 	    RF_ACTIVE | RF_SHAREABLE);
161aad64165SWarner Losh 	if (fdc->res_irq == 0) {
162aad64165SWarner Losh 		device_printf(dev, "cannot reserve interrupt line\n");
163b4046cd7SWarner Losh 		return (ENXIO);
164aad64165SWarner Losh 	}
165aad64165SWarner Losh 
166aad64165SWarner Losh 	if ((fdc->flags & FDC_NODMA) == 0) {
167aad64165SWarner Losh 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
168aad64165SWarner Losh 		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
169aad64165SWarner Losh 		if (fdc->res_drq == 0) {
170aad64165SWarner Losh 			device_printf(dev, "cannot reserve DMA request line\n");
171b4046cd7SWarner Losh 			/* This is broken and doesn't work for ISA case */
172aad64165SWarner Losh 			fdc->flags |= FDC_NODMA;
173aad64165SWarner Losh 		} else
174aad64165SWarner Losh 			fdc->dmachan = rman_get_start(fdc->res_drq);
175aad64165SWarner Losh 	}
176aad64165SWarner Losh 
177b4046cd7SWarner Losh 	return (0);
178aad64165SWarner Losh }
179aad64165SWarner Losh 
180aad64165SWarner Losh static int
1816d6fa4fdSWarner Losh fdc_isa_probe(device_t dev)
1826d6fa4fdSWarner Losh {
1831b67be7bSPoul-Henning Kamp 	int	error;
1846d6fa4fdSWarner Losh 	struct	fdc_data *fdc;
1856d6fa4fdSWarner Losh 
1866d6fa4fdSWarner Losh 	fdc = device_get_softc(dev);
1876d6fa4fdSWarner Losh 	fdc->fdc_dev = dev;
1886d6fa4fdSWarner Losh 
1896d6fa4fdSWarner Losh 	/* Check pnp ids */
1906d6fa4fdSWarner Losh 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
1916d6fa4fdSWarner Losh 	if (error == ENXIO)
192aad64165SWarner Losh 		return (ENXIO);
1936d6fa4fdSWarner Losh 
1946d6fa4fdSWarner Losh 	/* Attempt to allocate our resources for the duration of the probe */
195aad64165SWarner Losh 	error = fdc_isa_alloc_resources(dev, fdc);
1961b67be7bSPoul-Henning Kamp 	if (error == 0)
1971b67be7bSPoul-Henning Kamp 		error = fdc_initial_reset(dev, fdc);
1986d6fa4fdSWarner Losh 
1996d6fa4fdSWarner Losh 	fdc_release_resources(fdc);
2006d6fa4fdSWarner Losh 	return (error);
2016d6fa4fdSWarner Losh }
2026d6fa4fdSWarner Losh 
203aad64165SWarner Losh static int
204aad64165SWarner Losh fdc_isa_attach(device_t dev)
205aad64165SWarner Losh {
206aad64165SWarner Losh 	struct	fdc_data *fdc;
207aad64165SWarner Losh 	int error;
208aad64165SWarner Losh 
209aad64165SWarner Losh 	fdc = device_get_softc(dev);
2100bdf1a55SNate Lawson 	error = fdc_isa_alloc_resources(dev, fdc);
2111b67be7bSPoul-Henning Kamp 	if (error == 0)
2120bdf1a55SNate Lawson 		error = fdc_attach(dev);
2131b67be7bSPoul-Henning Kamp 	if (error == 0)
2140bdf1a55SNate Lawson 		error = fdc_hints_probe(dev);
2150bdf1a55SNate Lawson 	if (error)
2160bdf1a55SNate Lawson 		fdc_release_resources(fdc);
2170bdf1a55SNate Lawson 	return (error);
218aad64165SWarner Losh }
219aad64165SWarner Losh 
2206d6fa4fdSWarner Losh static device_method_t fdc_methods[] = {
2216d6fa4fdSWarner Losh 	/* Device interface */
2226d6fa4fdSWarner Losh 	DEVMETHOD(device_probe,		fdc_isa_probe),
223aad64165SWarner Losh 	DEVMETHOD(device_attach,	fdc_isa_attach),
2246d6fa4fdSWarner Losh 	DEVMETHOD(device_detach,	fdc_detach),
2256d6fa4fdSWarner Losh 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2266d6fa4fdSWarner Losh 	DEVMETHOD(device_suspend,	bus_generic_suspend),
2276d6fa4fdSWarner Losh 	DEVMETHOD(device_resume,	bus_generic_resume),
2286d6fa4fdSWarner Losh 
2296d6fa4fdSWarner Losh 	/* Bus interface */
2306d6fa4fdSWarner Losh 	DEVMETHOD(bus_print_child,	fdc_print_child),
2316d6fa4fdSWarner Losh 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
232752d4735SNate Lawson 	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
2336d6fa4fdSWarner Losh 	/* Our children never use any other bus interface methods. */
2346d6fa4fdSWarner Losh 
2356d6fa4fdSWarner Losh 	{ 0, 0 }
2366d6fa4fdSWarner Losh };
2376d6fa4fdSWarner Losh 
2386d6fa4fdSWarner Losh static driver_t fdc_driver = {
2396d6fa4fdSWarner Losh 	"fdc",
2406d6fa4fdSWarner Losh 	fdc_methods,
2416d6fa4fdSWarner Losh 	sizeof(struct fdc_data)
2426d6fa4fdSWarner Losh };
2436d6fa4fdSWarner Losh 
2446d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
245