xref: /freebsd/sys/dev/fdc/fdc_isa.c (revision 7c146c0cbca3b08c21f0e59409672e8d647bcd44)
10bdf1a55SNate Lawson /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4f86e6000SWarner Losh  * Copyright (c) 2004-2005 M. Warner Losh <imp@FreeBSD.org>
56d6fa4fdSWarner Losh  *
66d6fa4fdSWarner Losh  * Redistribution and use in source and binary forms, with or without
76d6fa4fdSWarner Losh  * modification, are permitted provided that the following conditions
86d6fa4fdSWarner Losh  * are met:
96d6fa4fdSWarner Losh  * 1. Redistributions of source code must retain the above copyright
10ac673f9aSWarner Losh  *    notice, this list of conditions and the following disclaimer.
116d6fa4fdSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12ac673f9aSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13ac673f9aSWarner Losh  *    documentation and/or other materials provided with the distribution.
146d6fa4fdSWarner Losh  *
156d6fa4fdSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
166d6fa4fdSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176d6fa4fdSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ac673f9aSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19ac673f9aSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206d6fa4fdSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
216d6fa4fdSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226d6fa4fdSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236d6fa4fdSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246d6fa4fdSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256d6fa4fdSWarner Losh  * SUCH DAMAGE.
266d6fa4fdSWarner Losh  */
276d6fa4fdSWarner Losh 
286d6fa4fdSWarner Losh #include <sys/cdefs.h>
296d6fa4fdSWarner Losh __FBSDID("$FreeBSD$");
306d6fa4fdSWarner Losh 
316d6fa4fdSWarner Losh #include <sys/param.h>
326d6fa4fdSWarner Losh #include <sys/bio.h>
336d6fa4fdSWarner Losh #include <sys/bus.h>
346d6fa4fdSWarner Losh #include <sys/kernel.h>
351b67be7bSPoul-Henning Kamp #include <sys/lock.h>
366d6fa4fdSWarner Losh #include <sys/module.h>
371b67be7bSPoul-Henning Kamp #include <sys/mutex.h>
386d6fa4fdSWarner Losh #include <sys/rman.h>
396d6fa4fdSWarner Losh #include <sys/systm.h>
406d6fa4fdSWarner Losh 
41c3ae4c40SWarner Losh #include <machine/bus.h>
42c3ae4c40SWarner Losh 
436d6fa4fdSWarner Losh #include <dev/fdc/fdcvar.h>
446d6fa4fdSWarner Losh 
456d6fa4fdSWarner Losh #include <isa/isavar.h>
466d6fa4fdSWarner Losh #include <isa/isareg.h>
476d6fa4fdSWarner Losh 
486d6fa4fdSWarner Losh static int fdc_isa_probe(device_t);
496d6fa4fdSWarner Losh 
506d6fa4fdSWarner Losh static struct isa_pnp_id fdc_ids[] = {
51973bfe6cSWarner Losh 	{0x0007d041, "PC standard floppy controller"}, /* PNP0700 */
526d6fa4fdSWarner Losh 	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
536d6fa4fdSWarner Losh 	{0}
546d6fa4fdSWarner Losh };
556d6fa4fdSWarner Losh 
56aad64165SWarner Losh /*
57aad64165SWarner Losh  * On standard ISA, we don't just use an 8 port range
58973bfe6cSWarner Losh  * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at
59973bfe6cSWarner Losh  * 0x3f6.  So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7.
60973bfe6cSWarner Losh  * However, some BIOSs omit the control port, while others start at
61973bfe6cSWarner Losh  * 0x3f2.  Of the latter, sometimes we have two resources, other times
62973bfe6cSWarner Losh  * we have one.  We have to deal with the following cases:
63aad64165SWarner Losh  *
64b4046cd7SWarner Losh  * 1:	0x3f0-0x3f5			# very rare
65b4046cd7SWarner Losh  * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
66b4046cd7SWarner Losh  * 3:	0x3f0-0x3f5,0x3f7		# Most common
67b4046cd7SWarner Losh  * 4:	0x3f2-0x3f5,0x3f7		# Second most common
68b4046cd7SWarner Losh  * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
69b4046cd7SWarner Losh  * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
70b4046cd7SWarner Losh  * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
71083ba097SWarner Losh  * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
72534e7194SWarner Losh  * 9:	0x3f0-0x3f3,0x3f4-0x3f5,0x3f7
73b4046cd7SWarner Losh  *
74973bfe6cSWarner Losh  * The following code is generic for any value of 0x3fx.  It is also
75973bfe6cSWarner Losh  * generic for all the above cases, as well as cases where things are
76973bfe6cSWarner Losh  * even weirder.
77aad64165SWarner Losh  */
78973bfe6cSWarner Losh int
79973bfe6cSWarner Losh fdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
80973bfe6cSWarner Losh {
81973bfe6cSWarner Losh 	struct resource *res;
82530a6924SWarner Losh 	int i, j, rid, newrid, nport;
83530a6924SWarner Losh 	u_long port;
84aad64165SWarner Losh 
85973bfe6cSWarner Losh 	fdc->fdc_dev = dev;
86973bfe6cSWarner Losh 	rid = 0;
87973bfe6cSWarner Losh 	for (i = 0; i < FDC_MAXREG; i++)
88973bfe6cSWarner Losh 		fdc->resio[i] = NULL;
89973bfe6cSWarner Losh 
90530a6924SWarner Losh 	nport = isa_get_logicalid(dev) ? 1 : 6;
91973bfe6cSWarner Losh 	for (rid = 0; ; rid++) {
92973bfe6cSWarner Losh 		newrid = rid;
93c47476d7SJustin Hibbits 		res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &newrid,
94c47476d7SJustin Hibbits 		    rid == 0 ? nport : 1, RF_ACTIVE);
95973bfe6cSWarner Losh 		if (res == NULL)
96973bfe6cSWarner Losh 			break;
97787a196bSWarner Losh 		/*
98787a196bSWarner Losh 		 * Mask off the upper bits of the register, and sanity
99787a196bSWarner Losh 		 * check resource ranges.
100787a196bSWarner Losh 		 */
101787a196bSWarner Losh 		i = rman_get_start(res) & 0x7;
102356eadcdSWarner Losh 		if (i + rman_get_size(res) - 1 > FDC_MAXREG) {
103356eadcdSWarner Losh 			bus_release_resource(dev, SYS_RES_IOPORT, newrid, res);
104787a196bSWarner Losh 			return (ENXIO);
105356eadcdSWarner Losh 		}
106973bfe6cSWarner Losh 		for (j = 0; j < rman_get_size(res); j++) {
107973bfe6cSWarner Losh 			fdc->resio[i + j] = res;
108973bfe6cSWarner Losh 			fdc->ridio[i + j] = newrid;
109973bfe6cSWarner Losh 			fdc->ioff[i + j] = j;
110973bfe6cSWarner Losh 			fdc->ioh[i + j] = rman_get_bushandle(res);
111973bfe6cSWarner Losh 		}
112973bfe6cSWarner Losh 	}
113530a6924SWarner Losh 	if (fdc->resio[2] == NULL) {
114530a6924SWarner Losh 		device_printf(dev, "No FDOUT register!\n");
115b4046cd7SWarner Losh 		return (ENXIO);
116530a6924SWarner Losh 	}
117973bfe6cSWarner Losh 	fdc->iot = rman_get_bustag(fdc->resio[2]);
118973bfe6cSWarner Losh 	if (fdc->resio[7] == NULL) {
119530a6924SWarner Losh 		port = (rman_get_start(fdc->resio[2]) & ~0x7) + 7;
120530a6924SWarner Losh 		newrid = rid;
121530a6924SWarner Losh 		res = bus_alloc_resource(dev, SYS_RES_IOPORT, &newrid, port,
122530a6924SWarner Losh 		    port, 1, RF_ACTIVE);
123530a6924SWarner Losh 		if (res == NULL) {
124530a6924SWarner Losh 			device_printf(dev, "Faking up FDCTL\n");
125973bfe6cSWarner Losh 			fdc->resio[7] = fdc->resio[2];
126973bfe6cSWarner Losh 			fdc->ridio[7] = fdc->ridio[2];
127973bfe6cSWarner Losh 			fdc->ioff[7] = fdc->ioff[2] + 5;
128973bfe6cSWarner Losh 			fdc->ioh[7] = fdc->ioh[2];
129530a6924SWarner Losh 		} else {
130530a6924SWarner Losh 			fdc->resio[7] = res;
131530a6924SWarner Losh 			fdc->ridio[7] = newrid;
132530a6924SWarner Losh 			fdc->ioff[7] = rman_get_start(res) & 7;
133530a6924SWarner Losh 			fdc->ioh[7] = rman_get_bushandle(res);
134530a6924SWarner Losh 		}
135b4046cd7SWarner Losh 	}
136aad64165SWarner Losh 
137aad64165SWarner Losh 	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
138aad64165SWarner Losh 	    RF_ACTIVE | RF_SHAREABLE);
139973bfe6cSWarner Losh 	if (fdc->res_irq == NULL) {
140aad64165SWarner Losh 		device_printf(dev, "cannot reserve interrupt line\n");
141b4046cd7SWarner Losh 		return (ENXIO);
142aad64165SWarner Losh 	}
143aad64165SWarner Losh 
144aad64165SWarner Losh 	if ((fdc->flags & FDC_NODMA) == 0) {
145aad64165SWarner Losh 		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
146aad64165SWarner Losh 		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
147973bfe6cSWarner Losh 		if (fdc->res_drq == NULL) {
148aad64165SWarner Losh 			device_printf(dev, "cannot reserve DMA request line\n");
149b4046cd7SWarner Losh 			/* This is broken and doesn't work for ISA case */
150aad64165SWarner Losh 			fdc->flags |= FDC_NODMA;
151aad64165SWarner Losh 		} else
152aad64165SWarner Losh 			fdc->dmachan = rman_get_start(fdc->res_drq);
153aad64165SWarner Losh 	}
154aad64165SWarner Losh 
155b4046cd7SWarner Losh 	return (0);
156aad64165SWarner Losh }
157aad64165SWarner Losh 
158aad64165SWarner Losh static int
1596d6fa4fdSWarner Losh fdc_isa_probe(device_t dev)
1606d6fa4fdSWarner Losh {
1611b67be7bSPoul-Henning Kamp 	int	error;
1626d6fa4fdSWarner Losh 	struct	fdc_data *fdc;
1636d6fa4fdSWarner Losh 
1646d6fa4fdSWarner Losh 	fdc = device_get_softc(dev);
1656d6fa4fdSWarner Losh 	fdc->fdc_dev = dev;
1666d6fa4fdSWarner Losh 
1676d6fa4fdSWarner Losh 	/* Check pnp ids */
1686d6fa4fdSWarner Losh 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
1696d6fa4fdSWarner Losh 	if (error == ENXIO)
170aad64165SWarner Losh 		return (ENXIO);
1716d6fa4fdSWarner Losh 
1726d6fa4fdSWarner Losh 	/* Attempt to allocate our resources for the duration of the probe */
173aad64165SWarner Losh 	error = fdc_isa_alloc_resources(dev, fdc);
1741b67be7bSPoul-Henning Kamp 	if (error == 0)
1751b67be7bSPoul-Henning Kamp 		error = fdc_initial_reset(dev, fdc);
1766d6fa4fdSWarner Losh 
1776d6fa4fdSWarner Losh 	fdc_release_resources(fdc);
1786d6fa4fdSWarner Losh 	return (error);
1796d6fa4fdSWarner Losh }
1806d6fa4fdSWarner Losh 
181aad64165SWarner Losh static int
182aad64165SWarner Losh fdc_isa_attach(device_t dev)
183aad64165SWarner Losh {
184aad64165SWarner Losh 	struct	fdc_data *fdc;
185aad64165SWarner Losh 	int error;
186aad64165SWarner Losh 
187aad64165SWarner Losh 	fdc = device_get_softc(dev);
188787a196bSWarner Losh 	fdc->fdc_dev = dev;
1890bdf1a55SNate Lawson 	error = fdc_isa_alloc_resources(dev, fdc);
1901b67be7bSPoul-Henning Kamp 	if (error == 0)
1910bdf1a55SNate Lawson 		error = fdc_attach(dev);
1921b67be7bSPoul-Henning Kamp 	if (error == 0)
1930bdf1a55SNate Lawson 		error = fdc_hints_probe(dev);
194fed2c48aSJohn Baldwin 	if (error == 0)
195fed2c48aSJohn Baldwin 		fdc_start_worker(dev);
196fed2c48aSJohn Baldwin 	else
1970bdf1a55SNate Lawson 		fdc_release_resources(fdc);
1980bdf1a55SNate Lawson 	return (error);
199aad64165SWarner Losh }
200aad64165SWarner Losh 
2016d6fa4fdSWarner Losh static device_method_t fdc_methods[] = {
2026d6fa4fdSWarner Losh 	/* Device interface */
2036d6fa4fdSWarner Losh 	DEVMETHOD(device_probe,		fdc_isa_probe),
204aad64165SWarner Losh 	DEVMETHOD(device_attach,	fdc_isa_attach),
2056d6fa4fdSWarner Losh 	DEVMETHOD(device_detach,	fdc_detach),
2066d6fa4fdSWarner Losh 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2076d6fa4fdSWarner Losh 	DEVMETHOD(device_suspend,	bus_generic_suspend),
2086d6fa4fdSWarner Losh 	DEVMETHOD(device_resume,	bus_generic_resume),
2096d6fa4fdSWarner Losh 
2106d6fa4fdSWarner Losh 	/* Bus interface */
2116d6fa4fdSWarner Losh 	DEVMETHOD(bus_print_child,	fdc_print_child),
2126d6fa4fdSWarner Losh 	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
213752d4735SNate Lawson 	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
2146d6fa4fdSWarner Losh 	/* Our children never use any other bus interface methods. */
2156d6fa4fdSWarner Losh 
2166d6fa4fdSWarner Losh 	{ 0, 0 }
2176d6fa4fdSWarner Losh };
2186d6fa4fdSWarner Losh 
2196d6fa4fdSWarner Losh static driver_t fdc_driver = {
2206d6fa4fdSWarner Losh 	"fdc",
2216d6fa4fdSWarner Losh 	fdc_methods,
2226d6fa4fdSWarner Losh 	sizeof(struct fdc_data)
2236d6fa4fdSWarner Losh };
2246d6fa4fdSWarner Losh 
225*7c146c0cSJohn Baldwin DRIVER_MODULE(fdc, isa, fdc_driver, 0, 0);
2265fe4723cSWarner Losh ISA_PNP_INFO(fdc_ids);
227