fdc_isa.c revision 135504
1236099Sdes/*-
2236099Sdes * Copyright (c) 2004 M. Warner Losh.
3236099Sdes * All rights reserved.
4236099Sdes *
5236099Sdes * Redistribution and use in source and binary forms, with or without
6236099Sdes * modification, are permitted provided that the following conditions
7236099Sdes * are met:
8236099Sdes * 1. Redistributions of source code must retain the above copyright
9255376Sdes *    notice, this list of conditions, and the following disclaimer,
10236099Sdes *    without modification, immediately at the beginning of the file.
11236099Sdes * 2. Redistributions in binary form must reproduce the above copyright
12236099Sdes *    notice, this list of conditions and the following disclaimer in
13236099Sdes *    the documentation and/or other materials provided with the
14236099Sdes *    distribution.
15236099Sdes *
16236099Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17236099Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18236099Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19236099Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20236099Sdes * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21236099Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22236099Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23236099Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24236099Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25236099Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26236099Sdes * SUCH DAMAGE.
27236099Sdes */
28236099Sdes
29255376Sdes#include <sys/cdefs.h>
30236099Sdes__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc_isa.c 135504 2004-09-20 06:12:19Z imp $");
31236099Sdes
32236099Sdes#include <sys/param.h>
33236099Sdes#include <sys/bio.h>
34236099Sdes#include <sys/bus.h>
35236099Sdes#include <sys/kernel.h>
36236099Sdes#include <sys/lock.h>
37236099Sdes#include <sys/module.h>
38236099Sdes#include <sys/mutex.h>
39236099Sdes#include <sys/rman.h>
40236099Sdes#include <sys/systm.h>
41236099Sdes
42236099Sdes#include <machine/bus.h>
43236099Sdes
44236099Sdes#include <dev/fdc/fdcvar.h>
45236099Sdes
46236099Sdes#include <isa/isavar.h>
47236099Sdes#include <isa/isareg.h>
48236099Sdes
49236099Sdesstatic int fdc_isa_probe(device_t);
50236099Sdes
51236099Sdesstatic struct isa_pnp_id fdc_ids[] = {
52236099Sdes	{0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */
53236099Sdes	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
54236099Sdes	{0}
55236099Sdes};
56236099Sdes
57236099Sdesint
58236099Sdesfdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
59236099Sdes{
60236099Sdes	int nports = 6;
61236099Sdes
62236099Sdes	fdc->fdc_dev = dev;
63236099Sdes	fdc->rid_ioport = 0;
64236099Sdes	fdc->rid_irq = 0;
65236099Sdes	fdc->rid_drq = 0;
66236099Sdes	fdc->rid_ctl = 1;
67236099Sdes
68236099Sdes	/*
69236099Sdes	 * On standard ISA, we don't just use an 8 port range
70236099Sdes	 * (e.g. 0x3f0-0x3f7) since that covers an IDE control
71236099Sdes	 * register at 0x3f6.  So, on older hardware, we use
72236099Sdes	 * 0x3f0-0x3f5 and 0x3f7.  However, some BIOSs omit the
73236099Sdes	 * control port, while others start at 0x3f2.  Of the latter,
74236099Sdes	 * sometimes we have two resources, other times we have one.
75236099Sdes	 * We have to deal with the following cases:
76236099Sdes	 *
77236099Sdes	 * 1:	0x3f0-0x3f5			# very rare
78236099Sdes	 * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
79236099Sdes	 * 3:	0x3f0-0x3f5,0x3f7		# Most common
80236099Sdes	 * 4:	0x3f2-0x3f5,0x3f7		# Second most common
81236099Sdes	 * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
82236099Sdes	 * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
83236099Sdes	 * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
84236099Sdes	 * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
85236099Sdes	 *
86236099Sdes	 * The following code is generic for any value of 0x3fx :-)
87236099Sdes	 */
88236099Sdes
89236099Sdes	/*
90236099Sdes	 * First, allocated the main range of ports.  In the best of
91236099Sdes	 * worlds, this is 4 or 6 ports.  In others, well, that's
92236099Sdes	 * why this function is so complicated.
93236099Sdes	 */
94236099Sdesagain_ioport:
95236099Sdes	fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT,
96236099Sdes	    &fdc->rid_ioport, 0ul, ~0ul, nports, RF_ACTIVE);
97236099Sdes	if (fdc->res_ioport == 0) {
98236099Sdes		device_printf(dev, "cannot allocate I/O port (%d ports)\n",
99236099Sdes		    nports);
100236099Sdes		return (ENXIO);
101236099Sdes	}
102236099Sdes	if ((rman_get_start(fdc->res_ioport) & 0x7) == 0 &&
103236099Sdes	    rman_get_size(fdc->res_ioport) == 2) {
104236099Sdes		/* Case 8 */
105236099Sdes		bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
106236099Sdes		    fdc->res_ioport);
107236099Sdes		fdc->rid_ioport++;
108236099Sdes		goto again_ioport;
109236099Sdes	}
110236099Sdes	fdc->portt = rman_get_bustag(fdc->res_ioport);
111236099Sdes	fdc->porth = rman_get_bushandle(fdc->res_ioport);
112236099Sdes
113236099Sdes	/*
114236099Sdes	 * Handle cases 4-8 above
115236099Sdes	 */
116236099Sdes	fdc->port_off = -(fdc->porth & 0x7);
117236099Sdes
118236099Sdes	/*
119236099Sdes	 * Deal with case 6, 7, and 8: FDSTS and FDSATA are in rid 1.
120236099Sdes	 */
121236099Sdes	if (rman_get_size(fdc->res_ioport) == 2) {
122236099Sdes		fdc->rid_sts = fdc->rid_ioport + 1;
123236099Sdes		fdc->res_sts = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
124236099Sdes		    &fdc->rid_sts, RF_ACTIVE);
125236099Sdes		if (fdc->res_sts == NULL) {
126236099Sdes			device_printf(dev, "Can't alloc rid 1");
127236099Sdes			fdc_release_resources(fdc);
128236099Sdes			return (ENXIO);
129236099Sdes		}
130236099Sdes		fdc->sts_off = -4;
131236099Sdes		fdc->stst = rman_get_bustag(fdc->res_sts);
132236099Sdes		fdc->stsh = rman_get_bushandle(fdc->res_sts);
133236099Sdes	} else {
134236099Sdes		fdc->res_sts = NULL;
135236099Sdes		fdc->sts_off = fdc->port_off;
136236099Sdes		fdc->stst = fdc->portt;
137236099Sdes		fdc->stsh = fdc->porth;
138236099Sdes	}
139236099Sdes
140236099Sdes	/*
141236099Sdes	 * allocate the control port.  For cases 1, 2, 5 and 7, we
142236099Sdes	 * fake it from the ioports resource.  XXX IS THIS THE RIGHT THING
143236099Sdes	 * TO DO, OR SHOULD WE CREATE A NEW RID? (I think we need a new rid)
144236099Sdes	 */
145236099Sdes	fdc->rid_ctl = fdc->rid_sts + 1;
146236099Sdes	fdc->res_ctl = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
147236099Sdes	    &fdc->rid_ctl, RF_ACTIVE);
148236099Sdes	if (fdc->res_ctl == NULL) {
149236099Sdes		fdc->ctl_off = 7 + fdc->port_off;
150236099Sdes		fdc->res_ctl = NULL;
151236099Sdes		fdc->ctlt = fdc->portt;
152236099Sdes		fdc->ctlh = fdc->porth;
153236099Sdes	} else {
154236099Sdes		fdc->ctl_off = 0;
155236099Sdes		fdc->ctlt = rman_get_bustag(fdc->res_ctl);
156		fdc->ctlh = rman_get_bushandle(fdc->res_ctl);
157	}
158
159	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
160	    RF_ACTIVE | RF_SHAREABLE);
161	if (fdc->res_irq == 0) {
162		device_printf(dev, "cannot reserve interrupt line\n");
163		return (ENXIO);
164	}
165
166	if ((fdc->flags & FDC_NODMA) == 0) {
167		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
168		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
169		if (fdc->res_drq == 0) {
170			device_printf(dev, "cannot reserve DMA request line\n");
171			/* This is broken and doesn't work for ISA case */
172			fdc->flags |= FDC_NODMA;
173		} else
174			fdc->dmachan = rman_get_start(fdc->res_drq);
175	}
176
177	return (0);
178}
179
180static int
181fdc_isa_probe(device_t dev)
182{
183	int	error;
184	struct	fdc_data *fdc;
185
186	fdc = device_get_softc(dev);
187	fdc->fdc_dev = dev;
188
189	/* Check pnp ids */
190	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
191	if (error == ENXIO)
192		return (ENXIO);
193
194	/* Attempt to allocate our resources for the duration of the probe */
195	error = fdc_isa_alloc_resources(dev, fdc);
196	if (error == 0)
197		error = fdc_initial_reset(dev, fdc);
198
199	fdc_release_resources(fdc);
200	return (error);
201}
202
203static int
204fdc_isa_attach(device_t dev)
205{
206	struct	fdc_data *fdc;
207	int error;
208
209	fdc = device_get_softc(dev);
210	error = fdc_isa_alloc_resources(dev, fdc);
211	if (error == 0)
212		error = fdc_attach(dev);
213	if (error == 0)
214		error = fdc_hints_probe(dev);
215	if (error)
216		fdc_release_resources(fdc);
217	return (error);
218}
219
220static device_method_t fdc_methods[] = {
221	/* Device interface */
222	DEVMETHOD(device_probe,		fdc_isa_probe),
223	DEVMETHOD(device_attach,	fdc_isa_attach),
224	DEVMETHOD(device_detach,	fdc_detach),
225	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
226	DEVMETHOD(device_suspend,	bus_generic_suspend),
227	DEVMETHOD(device_resume,	bus_generic_resume),
228
229	/* Bus interface */
230	DEVMETHOD(bus_print_child,	fdc_print_child),
231	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
232	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
233	/* Our children never use any other bus interface methods. */
234
235	{ 0, 0 }
236};
237
238static driver_t fdc_driver = {
239	"fdc",
240	fdc_methods,
241	sizeof(struct fdc_data)
242};
243
244DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
245