fdc_isa.c revision 140469
1198092Srdivacky/*-
2198092Srdivacky * Copyright (c) 2004-2005 M. Warner Losh.
3353358Sdim * All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6198092Srdivacky * modification, are permitted provided that the following conditions
7198092Srdivacky * are met:
8198092Srdivacky * 1. Redistributions of source code must retain the above copyright
9198092Srdivacky *    notice, this list of conditions and the following disclaimer.
10198092Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
11198092Srdivacky *    notice, this list of conditions and the following disclaimer in the
12198092Srdivacky *    documentation and/or other materials provided with the distribution.
13212904Sdim *
14276479Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15198092Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16198092Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17198092Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18360784Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19206084Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20203955Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21203955Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22198092Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23234353Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24198092Srdivacky * SUCH DAMAGE.
25198092Srdivacky */
26198092Srdivacky
27198092Srdivacky#include <sys/cdefs.h>
28198092Srdivacky__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc_isa.c 140469 2005-01-19 07:46:38Z imp $");
29198092Srdivacky
30198092Srdivacky#include <sys/param.h>
31198092Srdivacky#include <sys/bio.h>
32198092Srdivacky#include <sys/bus.h>
33198092Srdivacky#include <sys/kernel.h>
34198092Srdivacky#include <sys/lock.h>
35198092Srdivacky#include <sys/module.h>
36198092Srdivacky#include <sys/mutex.h>
37198092Srdivacky#include <sys/rman.h>
38280031Sdim#include <sys/systm.h>
39280031Sdim
40280031Sdim#include <machine/bus.h>
41280031Sdim
42280031Sdim#include <dev/fdc/fdcvar.h>
43280031Sdim
44280031Sdim#include <isa/isavar.h>
45280031Sdim#include <isa/isareg.h>
46314564Sdim
47314564Sdimstatic int fdc_isa_probe(device_t);
48314564Sdim
49280031Sdimstatic struct isa_pnp_id fdc_ids[] = {
50280031Sdim	{0x0007d041, "PC standard floppy controller"}, /* PNP0700 */
51280031Sdim	{0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */
52314564Sdim	{0}
53314564Sdim};
54314564Sdim
55314564Sdim/*
56314564Sdim * On standard ISA, we don't just use an 8 port range
57314564Sdim * (e.g. 0x3f0-0x3f7) since that covers an IDE control register at
58314564Sdim * 0x3f6.  So, on older hardware, we use 0x3f0-0x3f5 and 0x3f7.
59314564Sdim * However, some BIOSs omit the control port, while others start at
60314564Sdim * 0x3f2.  Of the latter, sometimes we have two resources, other times
61314564Sdim * we have one.  We have to deal with the following cases:
62314564Sdim *
63314564Sdim * 1:	0x3f0-0x3f5			# very rare
64314564Sdim * 2:	0x3f0				# hints -> 0x3f0-0x3f5,0x3f7
65314564Sdim * 3:	0x3f0-0x3f5,0x3f7		# Most common
66280031Sdim * 4:	0x3f2-0x3f5,0x3f7		# Second most common
67344779Sdim * 5:	0x3f2-0x3f5			# implies 0x3f7 too.
68280031Sdim * 6:	0x3f2-0x3f3,0x3f4-0x3f5,0x3f7	# becoming common
69280031Sdim * 7:	0x3f2-0x3f3,0x3f4-0x3f5		# rare
70280031Sdim * 8:	0x3f0-0x3f1,0x3f2-0x3f3,0x3f4-0x3f5,0x3f7
71280031Sdim * 9:	0x3f0-0x3f3,0x3f4-0x3f5,0x3f7
72314564Sdim *
73314564Sdim * The following code is generic for any value of 0x3fx.  It is also
74314564Sdim * generic for all the above cases, as well as cases where things are
75314564Sdim * even weirder.
76280031Sdim */
77280031Sdimint
78280031Sdimfdc_isa_alloc_resources(device_t dev, struct fdc_data *fdc)
79341825Sdim{
80341825Sdim	struct resource *res;
81341825Sdim	int nports = 6;
82341825Sdim	int i, j, rid, newrid;
83341825Sdim
84360784Sdim	fdc->fdc_dev = dev;
85360784Sdim	rid = 0;
86341825Sdim	for (i = 0; i < FDC_MAXREG; i++)
87360784Sdim		fdc->resio[i] = NULL;
88360784Sdim
89360784Sdim	for (rid = 0; ; rid++) {
90360784Sdim		newrid = rid;
91360784Sdim		res = bus_alloc_resource(dev, SYS_RES_IOPORT,
92360784Sdim		    &newrid, 0ul, ~0ul, nports, RF_ACTIVE);
93360784Sdim		if (res == NULL)
94360784Sdim			break;
95341825Sdim		i = rman_get_start(res);
96341825Sdim		for (j = 0; j < rman_get_size(res); j++) {
97341825Sdim			fdc->resio[i + j] = res;
98341825Sdim			fdc->ridio[i + j] = newrid;
99341825Sdim			fdc->ioff[i + j] = j;
100341825Sdim			fdc->ioh[i + j] = rman_get_bushandle(res);
101341825Sdim		}
102341825Sdim	}
103341825Sdim	if (fdc->resio[2] == NULL)
104341825Sdim		return (ENXIO);
105341825Sdim	fdc->iot = rman_get_bustag(fdc->resio[2]);
106341825Sdim	if (fdc->resio[7] == NULL) {
107341825Sdim		/* XXX allocate */
108341825Sdim		fdc->resio[7] = fdc->resio[2];
109341825Sdim		fdc->ridio[7] = fdc->ridio[2];
110198092Srdivacky		fdc->ioff[7] = fdc->ioff[2] + 5;
111198092Srdivacky		fdc->ioh[7] = fdc->ioh[2];
112198092Srdivacky	}
113249423Sdim
114249423Sdim	fdc->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fdc->rid_irq,
115249423Sdim	    RF_ACTIVE | RF_SHAREABLE);
116296417Sdim	if (fdc->res_irq == NULL) {
117249423Sdim		device_printf(dev, "cannot reserve interrupt line\n");
118249423Sdim		return (ENXIO);
119249423Sdim	}
120249423Sdim
121249423Sdim	if ((fdc->flags & FDC_NODMA) == 0) {
122249423Sdim		fdc->res_drq = bus_alloc_resource_any(dev, SYS_RES_DRQ,
123249423Sdim		    &fdc->rid_drq, RF_ACTIVE | RF_SHAREABLE);
124249423Sdim		if (fdc->res_drq == NULL) {
125249423Sdim			device_printf(dev, "cannot reserve DMA request line\n");
126249423Sdim			/* This is broken and doesn't work for ISA case */
127198092Srdivacky			fdc->flags |= FDC_NODMA;
128249423Sdim		} else
129249423Sdim			fdc->dmachan = rman_get_start(fdc->res_drq);
130249423Sdim	}
131249423Sdim
132249423Sdim	return (0);
133198092Srdivacky}
134249423Sdim
135249423Sdimstatic int
136249423Sdimfdc_isa_probe(device_t dev)
137249423Sdim{
138249423Sdim	int	error;
139249423Sdim	struct	fdc_data *fdc;
140249423Sdim
141198092Srdivacky	fdc = device_get_softc(dev);
142249423Sdim	fdc->fdc_dev = dev;
143249423Sdim
144249423Sdim	/* Check pnp ids */
145249423Sdim	error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids);
146249423Sdim	if (error == ENXIO)
147249423Sdim		return (ENXIO);
148249423Sdim
149249423Sdim	/* Attempt to allocate our resources for the duration of the probe */
150249423Sdim	error = fdc_isa_alloc_resources(dev, fdc);
151249423Sdim	if (error == 0)
152249423Sdim		error = fdc_initial_reset(dev, fdc);
153249423Sdim
154249423Sdim	fdc_release_resources(fdc);
155249423Sdim	return (error);
156249423Sdim}
157249423Sdim
158309124Sdimstatic int
159309124Sdimfdc_isa_attach(device_t dev)
160309124Sdim{
161360784Sdim	struct	fdc_data *fdc;
162309124Sdim	int error;
163309124Sdim
164309124Sdim	fdc = device_get_softc(dev);
165249423Sdim	error = fdc_isa_alloc_resources(dev, fdc);
166360784Sdim	if (error == 0)
167309124Sdim		error = fdc_attach(dev);
168309124Sdim	if (error == 0)
169198092Srdivacky		error = fdc_hints_probe(dev);
170198092Srdivacky	if (error)
171198092Srdivacky		fdc_release_resources(fdc);
172198092Srdivacky	return (error);
173198092Srdivacky}
174198092Srdivacky
175198092Srdivackystatic device_method_t fdc_methods[] = {
176198092Srdivacky	/* Device interface */
177314564Sdim	DEVMETHOD(device_probe,		fdc_isa_probe),
178314564Sdim	DEVMETHOD(device_attach,	fdc_isa_attach),
179327952Sdim	DEVMETHOD(device_detach,	fdc_detach),
180314564Sdim	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
181314564Sdim	DEVMETHOD(device_suspend,	bus_generic_suspend),
182198092Srdivacky	DEVMETHOD(device_resume,	bus_generic_resume),
183198092Srdivacky
184198092Srdivacky	/* Bus interface */
185198092Srdivacky	DEVMETHOD(bus_print_child,	fdc_print_child),
186198092Srdivacky	DEVMETHOD(bus_read_ivar,	fdc_read_ivar),
187198092Srdivacky	DEVMETHOD(bus_write_ivar,       fdc_write_ivar),
188198092Srdivacky	/* Our children never use any other bus interface methods. */
189198092Srdivacky
190198092Srdivacky	{ 0, 0 }
191198092Srdivacky};
192198092Srdivacky
193198092Srdivackystatic driver_t fdc_driver = {
194198092Srdivacky	"fdc",
195198092Srdivacky	fdc_methods,
196234982Sdim	sizeof(struct fdc_data)
197234982Sdim};
198280031Sdim
199280031SdimDRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0);
200280031Sdim