aic_pccard.c revision 73280
1/*-
2 * Copyright (c) 1999 Luoqi Chen.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/aic/aic_pccard.c 73280 2001-03-01 17:09:09Z markm $
27 */
28
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/module.h>
32#include <sys/bus.h>
33
34#include <machine/bus_pio.h>
35#include <machine/bus.h>
36#include <machine/resource.h>
37#include <sys/rman.h>
38
39#include <dev/aic/aicvar.h>
40#include <dev/pccard/pccardvar.h>
41#include <dev/pccard/pccarddevs.h>
42
43#include "card_if.h"
44
45struct aic_pccard_softc {
46	struct	aic_softc sc_aic;
47	struct	resource *sc_port;
48	struct	resource *sc_irq;
49	void	*sc_ih;
50};
51
52static int aic_pccard_alloc_resources(device_t);
53static void aic_pccard_release_resources(device_t);
54static int aic_pccard_match(device_t);
55static int aic_pccard_probe(device_t);
56static int aic_pccard_attach(device_t);
57
58const struct pccard_product aic_pccard_products[] = {
59	{ PCCARD_STR_ADAPTEC_APA1460,		PCCARD_VENDOR_ADAPTEC,
60	  PCCARD_PRODUCT_ADAPTEC_APA1460,	0,
61	  PCCARD_CIS_ADAPTEC_APA1460 },
62	{ PCCARD_STR_ADAPTEC_APA1460A,		PCCARD_VENDOR_ADAPTEC,
63	  PCCARD_PRODUCT_ADAPTEC_APA1460A,	0,
64	  PCCARD_CIS_ADAPTEC_APA1460A },
65	{ PCCARD_STR_NEWMEDIA_BUSTOASTER,	PCCARD_VENDOR_NEWMEDIA,
66	  PCCARD_PRODUCT_NEWMEDIA_BUSTOASTER,	0,
67	  PCCARD_CIS_NEWMEDIA_BUSTOASTER },
68	{ NULL }
69};
70
71#define	AIC_PCCARD_PORTSIZE 0x20
72
73static int
74aic_pccard_alloc_resources(device_t dev)
75{
76	struct aic_pccard_softc *sc = device_get_softc(dev);
77	int rid;
78
79	sc->sc_port = sc->sc_irq = 0;
80
81	rid = 0;
82	sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
83	    0ul, ~0ul, AIC_PCCARD_PORTSIZE, RF_ACTIVE);
84	if (!sc->sc_port)
85		return (ENOMEM);
86
87	rid = 0;
88	sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
89	    0ul, ~0ul, 1, RF_ACTIVE);
90	if (!sc->sc_irq) {
91		aic_pccard_release_resources(dev);
92		return (ENOMEM);
93	}
94
95	sc->sc_aic.unit = device_get_unit(dev);
96	sc->sc_aic.tag = rman_get_bustag(sc->sc_port);
97	sc->sc_aic.bsh = rman_get_bushandle(sc->sc_port);
98	return (0);
99}
100
101static void
102aic_pccard_release_resources(device_t dev)
103{
104	struct aic_pccard_softc *sc = device_get_softc(dev);
105
106	if (sc->sc_port)
107		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->sc_port);
108	if (sc->sc_irq)
109		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
110	sc->sc_port = sc->sc_irq = 0;
111}
112
113static int
114aic_pccard_match(device_t dev)
115{
116	const struct pccard_product *pp;
117
118	if ((pp = pccard_product_lookup(dev, aic_pccard_products,
119	    sizeof(aic_pccard_products[0]), NULL)) != NULL) {
120		device_set_desc(dev, pp->pp_name);
121		return 0;
122	}
123	return EIO;
124}
125
126static int
127aic_pccard_probe(device_t dev)
128{
129	struct aic_pccard_softc *sc = device_get_softc(dev);
130	struct aic_softc *aic = &sc->sc_aic;
131
132	if (aic_pccard_alloc_resources(dev))
133		return (ENXIO);
134	if (aic_probe(aic)) {
135		aic_pccard_release_resources(dev);
136		return (ENXIO);
137	}
138	aic_pccard_release_resources(dev);
139
140	device_set_desc(dev, "Adaptec 6260/6360 SCSI controller");
141	return (0);
142}
143
144static int
145aic_pccard_attach(device_t dev)
146{
147	struct aic_pccard_softc *sc = device_get_softc(dev);
148	struct aic_softc *aic = &sc->sc_aic;
149	int error;
150
151	error = aic_pccard_alloc_resources(dev);
152	if (error) {
153		device_printf(dev, "resource allocation failed\n");
154		return (error);
155	}
156
157	error = aic_attach(aic);
158	if (error) {
159		device_printf(dev, "attach failed\n");
160		aic_pccard_release_resources(dev);
161		return (error);
162	}
163
164	error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_CAM|INTR_ENTROPY,
165				aic_intr, aic, &sc->sc_ih);
166	if (error) {
167		device_printf(dev, "failed to register interrupt handler\n");
168		aic_pccard_release_resources(dev);
169		return (error);
170	}
171	return (0);
172}
173
174static int
175aic_pccard_detach(device_t dev)
176{
177	struct aic_pccard_softc *sc = device_get_softc(dev);
178	struct aic_softc *aic = &sc->sc_aic;
179	int error;
180
181	error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
182	if (error) {
183		device_printf(dev, "failed to unregister interrupt handler\n");
184	}
185
186	error = aic_detach(aic);
187	if (error) {
188		device_printf(dev, "detach failed\n");
189		return (error);
190	}
191
192	aic_pccard_release_resources(dev);
193	return (0);
194}
195
196static device_method_t aic_pccard_methods[] = {
197	/* Device interface */
198	DEVMETHOD(device_probe,		pccard_compat_probe),
199	DEVMETHOD(device_attach,	pccard_compat_attach),
200	DEVMETHOD(device_detach,	aic_pccard_detach),
201
202	/* Card interface */
203	DEVMETHOD(card_compat_match,	aic_pccard_match),
204	DEVMETHOD(card_compat_probe,	aic_pccard_probe),
205	DEVMETHOD(card_compat_attach,	aic_pccard_attach),
206
207	{ 0, 0 }
208};
209
210static driver_t aic_pccard_driver = {
211	"aic",
212	aic_pccard_methods, sizeof(struct aic_pccard_softc),
213};
214
215extern devclass_t aic_devclass;
216
217MODULE_DEPEND(aic, cam, 1,1,1);
218DRIVER_MODULE(aic, pccard, aic_pccard_driver, aic_devclass, 0, 0);
219