xref: /freebsd/sys/dev/fdc/fdc_isa.c (revision c47476d7e6801deffc8b3c057d0fbf7d2335a0c2)
10bdf1a55SNate Lawson /*-
2973bfe6cSWarner Losh  * Copyright (c) 2004-2005 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
9ac673f9aSWarner Losh  *    notice, this list of conditions and the following disclaimer.
106d6fa4fdSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ac673f9aSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ac673f9aSWarner Losh  *    documentation and/or other materials provided with the distribution.
136d6fa4fdSWarner Losh  *
146d6fa4fdSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
156d6fa4fdSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166d6fa4fdSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ac673f9aSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ac673f9aSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196d6fa4fdSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
206d6fa4fdSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
216d6fa4fdSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
226d6fa4fdSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
236d6fa4fdSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
246d6fa4fdSWarner Losh  * SUCH DAMAGE.
256d6fa4fdSWarner Losh  */
266d6fa4fdSWarner Losh 
276d6fa4fdSWarner Losh #include <sys/cdefs.h>
286d6fa4fdSWarner Losh __FBSDID("$FreeBSD$");
296d6fa4fdSWarner Losh 
306d6fa4fdSWarner Losh #include <sys/param.h>
316d6fa4fdSWarner Losh #include <sys/bio.h>
326d6fa4fdSWarner Losh #include <sys/bus.h>
336d6fa4fdSWarner Losh #include <sys/kernel.h>
341b67be7bSPoul-Henning Kamp #include <sys/lock.h>
356d6fa4fdSWarner Losh #include <sys/module.h>
361b67be7bSPoul-Henning Kamp #include <sys/mutex.h>
376d6fa4fdSWarner Losh #include <sys/rman.h>
386d6fa4fdSWarner Losh #include <sys/systm.h>
396d6fa4fdSWarner Losh 
40c3ae4c40SWarner Losh #include <machine/bus.h>
41c3ae4c40SWarner Losh 
426d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h>
436d6fa4fdSWarner Losh 
446d6fa4fdSWarner Losh #include <isa/isavar.h>
456d6fa4fdSWarner Losh #include <isa/isareg.h>
466d6fa4fdSWarner Losh 
476d6fa4fdSWarner Losh static int fdc_isa_probe(device_t);
486d6fa4fdSWarner Losh 
496d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = {
50973bfe6cSWarner Losh 	{0x0007d041, "PC standard floppy controller"}, /* PNP0700 */
516d6fa4fdSWarner Losh 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
526d6fa4fdSWarner Losh 	{0}
536d6fa4fdSWarner Losh };
546d6fa4fdSWarner Losh 
55aad64165SWarner Losh /*
56aad64165SWarner Losh  * On standard ISA, we don't just use an 8 port range
57973bfe6cSWarner Losh  * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at
58973bfe6cSWarner Losh  * 0x3f6.  So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7.
59973bfe6cSWarner Losh  * However, some BIOSs omit the control port, while others start at
60973bfe6cSWarner Losh  * 0x3f2.  Of the latter, sometimes we have two resources, other times
61973bfe6cSWarner Losh  * we have one.  We have to deal with the following cases:
62aad64165SWarner Losh  *
63b4046cd7SWarner Losh  * 1:	0x3f0-0x3f5			# very rare
64b4046cd7SWarner Losh  * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
65b4046cd7SWarner Losh  * 3:	0x3f0-0x3f5,0x3f7		# Most common
66b4046cd7SWarner Losh  * 4:	0x3f2-0x3f5,0x3f7		# Second most common
67b4046cd7SWarner Losh  * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
68b4046cd7SWarner Losh  * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
69b4046cd7SWarner Losh  * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
70083ba097SWarner Losh  * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
71534e7194SWarner Losh  * 9:	0x3f0-0x3f3,0x3f4-0x3f5,0x3f7
72b4046cd7SWarner Losh  *
73973bfe6cSWarner Losh  * The following code is generic for any value of 0x3fx.  It is also
74973bfe6cSWarner Losh  * generic for all the above cases, as well as cases where things are
75973bfe6cSWarner Losh  * even weirder.
76aad64165SWarner Losh  */
77973bfe6cSWarner Losh int
78973bfe6cSWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
79973bfe6cSWarner Losh {
80973bfe6cSWarner Losh 	struct resource *res;
81530a6924SWarner Losh 	int i, j, rid, newrid, nport;
82530a6924SWarner Losh 	u_long port;
83aad64165SWarner Losh 
84973bfe6cSWarner Losh 	fdc->fdc_dev = dev;
85973bfe6cSWarner Losh 	rid = 0;
86973bfe6cSWarner Losh 	for (i = 0; i < FDC_MAXREG; i++)
87973bfe6cSWarner Losh 		fdc->resio[i] = NULL;
88973bfe6cSWarner Losh 
89530a6924SWarner Losh 	nport = isa_get_logicalid(dev) ? 1 : 6;
90973bfe6cSWarner Losh 	for (rid = 0; ; rid++) {
91973bfe6cSWarner Losh 		newrid = rid;
92*c47476d7SJustin Hibbits 		res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &newrid,
93*c47476d7SJustin Hibbits 		    rid == 0 ? nport : 1, RF_ACTIVE);
94973bfe6cSWarner Losh 		if (res == NULL)
95973bfe6cSWarner Losh 			break;
96787a196bSWarner Losh 		/*
97787a196bSWarner Losh 		 * Mask off the upper bits of the register, and sanity
98787a196bSWarner Losh 		 * check resource ranges.
99787a196bSWarner Losh 		 */
100787a196bSWarner Losh 		i = rman_get_start(res) & 0x7;
101356eadcdSWarner Losh 		if (i + rman_get_size(res) - 1 > FDC_MAXREG) {
102356eadcdSWarner Losh 			bus_release_resource(dev, SYS_RES_IOPORT, newrid, res);
103787a196bSWarner Losh 			return (ENXIO);
104356eadcdSWarner Losh 		}
105973bfe6cSWarner Losh 		for (j = 0; j < rman_get_size(res); j++) {
106973bfe6cSWarner Losh 			fdc->resio[i + j] = res;
107973bfe6cSWarner Losh 			fdc->ridio[i + j] = newrid;
108973bfe6cSWarner Losh 			fdc->ioff[i + j] = j;
109973bfe6cSWarner Losh 			fdc->ioh[i + j] = rman_get_bushandle(res);
110973bfe6cSWarner Losh 		}
111973bfe6cSWarner Losh 	}
112530a6924SWarner Losh 	if (fdc->resio[2] == NULL) {
113530a6924SWarner Losh 		device_printf(dev, "No FDOUT register!\n");
114b4046cd7SWarner Losh 		return (ENXIO);
115530a6924SWarner Losh 	}
116973bfe6cSWarner Losh 	fdc->iot = rman_get_bustag(fdc->resio[2]);
117973bfe6cSWarner Losh 	if (fdc->resio[7] == NULL) {
118530a6924SWarner Losh 		port = (rman_get_start(fdc->resio[2]) & ~0x7) + 7;
119530a6924SWarner Losh 		newrid = rid;
120530a6924SWarner Losh 		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, port,
121530a6924SWarner Losh 		    port, 1, RF_ACTIVE);
122530a6924SWarner Losh 		if (res == NULL) {
123530a6924SWarner Losh 			device_printf(dev, "Faking up FDCTL\n");
124973bfe6cSWarner Losh 			fdc->resio[7] = fdc->resio[2];
125973bfe6cSWarner Losh 			fdc->ridio[7] = fdc->ridio[2];
126973bfe6cSWarner Losh 			fdc->ioff[7] = fdc->ioff[2] + 5;
127973bfe6cSWarner Losh 			fdc->ioh[7] = fdc->ioh[2];
128530a6924SWarner Losh 		} else {
129530a6924SWarner Losh 			fdc->resio[7] = res;
130530a6924SWarner Losh 			fdc->ridio[7] = newrid;
131530a6924SWarner Losh 			fdc->ioff[7] = rman_get_start(res) & 7;
132530a6924SWarner Losh 			fdc->ioh[7] = rman_get_bushandle(res);
133530a6924SWarner Losh 		}
134b4046cd7SWarner Losh 	}
135aad64165SWarner Losh 
136aad64165SWarner Losh 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
137aad64165SWarner Losh 	    RF_ACTIVE | RF_SHAREABLE);
138973bfe6cSWarner Losh 	if (fdc->res_irq == NULL) {
139aad64165SWarner Losh 		device_printf(dev, "cannot reserve interrupt line\n");
140b4046cd7SWarner Losh 		return (ENXIO);
141aad64165SWarner Losh 	}
142aad64165SWarner Losh 
143aad64165SWarner Losh 	if ((fdc->flags & FDC_NODMA) == 0) {
144aad64165SWarner Losh 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
145aad64165SWarner Losh 		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
146973bfe6cSWarner Losh 		if (fdc->res_drq == NULL) {
147aad64165SWarner Losh 			device_printf(dev, "cannot reserve DMA request line\n");
148b4046cd7SWarner Losh 			/* This is broken and doesn't work for ISA case */
149aad64165SWarner Losh 			fdc->flags |= FDC_NODMA;
150aad64165SWarner Losh 		} else
151aad64165SWarner Losh 			fdc->dmachan = rman_get_start(fdc->res_drq);
152aad64165SWarner Losh 	}
153aad64165SWarner Losh 
154b4046cd7SWarner Losh 	return (0);
155aad64165SWarner Losh }
156aad64165SWarner Losh 
157aad64165SWarner Losh static int
1586d6fa4fdSWarner Losh fdc_isa_probe(device_t dev)
1596d6fa4fdSWarner Losh {
1601b67be7bSPoul-Henning Kamp 	int	error;
1616d6fa4fdSWarner Losh 	struct	fdc_data *fdc;
1626d6fa4fdSWarner Losh 
1636d6fa4fdSWarner Losh 	fdc = device_get_softc(dev);
1646d6fa4fdSWarner Losh 	fdc->fdc_dev = dev;
1656d6fa4fdSWarner Losh 
1666d6fa4fdSWarner Losh 	/* Check pnp ids */
1676d6fa4fdSWarner Losh 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
1686d6fa4fdSWarner Losh 	if (error == ENXIO)
169aad64165SWarner Losh 		return (ENXIO);
1706d6fa4fdSWarner Losh 
1716d6fa4fdSWarner Losh 	/* Attempt to allocate our resources for the duration of the probe */
172aad64165SWarner Losh 	error = fdc_isa_alloc_resources(dev, fdc);
1731b67be7bSPoul-Henning Kamp 	if (error == 0)
1741b67be7bSPoul-Henning Kamp 		error = fdc_initial_reset(dev, fdc);
1756d6fa4fdSWarner Losh 
1766d6fa4fdSWarner Losh 	fdc_release_resources(fdc);
1776d6fa4fdSWarner Losh 	return (error);
1786d6fa4fdSWarner Losh }
1796d6fa4fdSWarner Losh 
180aad64165SWarner Losh static int
181aad64165SWarner Losh fdc_isa_attach(device_t dev)
182aad64165SWarner Losh {
183aad64165SWarner Losh 	struct	fdc_data *fdc;
184aad64165SWarner Losh 	int error;
185aad64165SWarner Losh 
186aad64165SWarner Losh 	fdc = device_get_softc(dev);
187787a196bSWarner Losh 	fdc->fdc_dev = dev;
1880bdf1a55SNate Lawson 	error = fdc_isa_alloc_resources(dev, fdc);
1891b67be7bSPoul-Henning Kamp 	if (error == 0)
1900bdf1a55SNate Lawson 		error = fdc_attach(dev);
1911b67be7bSPoul-Henning Kamp 	if (error == 0)
1920bdf1a55SNate Lawson 		error = fdc_hints_probe(dev);
1930bdf1a55SNate Lawson 	if (error)
1940bdf1a55SNate Lawson 		fdc_release_resources(fdc);
1950bdf1a55SNate Lawson 	return (error);
196aad64165SWarner Losh }
197aad64165SWarner Losh 
1986d6fa4fdSWarner Losh static device_method_t fdc_methods[] = {
1996d6fa4fdSWarner Losh 	/* Device interface */
2006d6fa4fdSWarner Losh 	DEVMETHOD(device_probe,		fdc_isa_probe),
201aad64165SWarner Losh 	DEVMETHOD(device_attach,	fdc_isa_attach),
2026d6fa4fdSWarner Losh 	DEVMETHOD(device_detach,	fdc_detach),
2036d6fa4fdSWarner Losh 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2046d6fa4fdSWarner Losh 	DEVMETHOD(device_suspend,	bus_generic_suspend),
2056d6fa4fdSWarner Losh 	DEVMETHOD(device_resume,	bus_generic_resume),
2066d6fa4fdSWarner Losh 
2076d6fa4fdSWarner Losh 	/* Bus interface */
2086d6fa4fdSWarner Losh 	DEVMETHOD(bus_print_child,	fdc_print_child),
2096d6fa4fdSWarner Losh 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
210752d4735SNate Lawson 	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
2116d6fa4fdSWarner Losh 	/* Our children never use any other bus interface methods. */
2126d6fa4fdSWarner Losh 
2136d6fa4fdSWarner Losh 	{ 0, 0 }
2146d6fa4fdSWarner Losh };
2156d6fa4fdSWarner Losh 
2166d6fa4fdSWarner Losh static driver_t fdc_driver = {
2176d6fa4fdSWarner Losh 	"fdc",
2186d6fa4fdSWarner Losh 	fdc_methods,
2196d6fa4fdSWarner Losh 	sizeof(struct fdc_data)
2206d6fa4fdSWarner Losh };
2216d6fa4fdSWarner Losh 
2226d6fa4fdSWarner Losh DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
223