aic_pcmcia.c revision 1.32.4.1 1 /* $NetBSD: aic_pcmcia.c,v 1.32.4.1 2006/02/03 05:19:41 riz Exp $ */
2
3 /*
4 * Copyright (c) 1997 Marc Horowitz. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Marc Horowitz.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: aic_pcmcia.c,v 1.32.4.1 2006/02/03 05:19:41 riz Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/select.h>
38 #include <sys/device.h>
39
40 #include <machine/cpu.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43
44 #include <dev/scsipi/scsipi_all.h>
45 #include <dev/scsipi/scsipiconf.h>
46 #include <dev/scsipi/scsi_all.h>
47
48 #include <dev/ic/aic6360var.h>
49
50 #include <dev/pcmcia/pcmciareg.h>
51 #include <dev/pcmcia/pcmciavar.h>
52 #include <dev/pcmcia/pcmciadevs.h>
53
54 struct aic_pcmcia_softc {
55 struct aic_softc sc_aic; /* real "aic" softc */
56
57 /* PCMCIA-specific goo. */
58 struct pcmcia_function *sc_pf; /* our PCMCIA function */
59 void *sc_ih; /* interrupt handler */
60
61 int sc_state;
62 #define AIC_PCMCIA_ATTACHED 3
63 };
64
65 int aic_pcmcia_match(struct device *, struct cfdata *, void *);
66 int aic_pcmcia_validate_config(struct pcmcia_config_entry *);
67 void aic_pcmcia_attach(struct device *, struct device *, void *);
68 int aic_pcmcia_detach(struct device *, int);
69 int aic_pcmcia_enable(struct device *, int);
70
71 CFATTACH_DECL(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
72 aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, aic_activate);
73
74 const struct pcmcia_product aic_pcmcia_products[] = {
75 { PCMCIA_VENDOR_ADAPTEC, PCMCIA_PRODUCT_ADAPTEC_APA1460,
76 PCMCIA_CIS_INVALID },
77
78 { PCMCIA_VENDOR_ADAPTEC, PCMCIA_PRODUCT_ADAPTEC_APA1460A,
79 PCMCIA_CIS_INVALID },
80
81 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,
82 PCMCIA_CIS_INVALID },
83 };
84 const size_t aic_pcmcia_nproducts =
85 sizeof(aic_pcmcia_products) / sizeof(aic_pcmcia_products[0]);
86
87 int
88 aic_pcmcia_match(parent, match, aux)
89 struct device *parent;
90 struct cfdata *match;
91 void *aux;
92 {
93 struct pcmcia_attach_args *pa = aux;
94
95 if (pcmcia_product_lookup(pa, aic_pcmcia_products, aic_pcmcia_nproducts,
96 sizeof(aic_pcmcia_products[0]), NULL))
97 return (1);
98 return (0);
99 }
100
101 int
102 aic_pcmcia_validate_config(cfe)
103 struct pcmcia_config_entry *cfe;
104 {
105 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
106 cfe->num_memspace != 0 ||
107 cfe->num_iospace != 1)
108 return (EINVAL);
109 /* XXX Below line is a hack to get around an rbus resource allocation */
110 /* XXX problem. It should be removed when the problem is fixed. */
111 cfe->iomask = 0;
112 return (0);
113 }
114
115 void
116 aic_pcmcia_attach(parent, self, aux)
117 struct device *parent, *self;
118 void *aux;
119 {
120 struct aic_pcmcia_softc *psc = (void *)self;
121 struct aic_softc *sc = &psc->sc_aic;
122 struct pcmcia_attach_args *pa = aux;
123 struct pcmcia_config_entry *cfe;
124 struct pcmcia_function *pf = pa->pf;
125 int error;
126
127 psc->sc_pf = pf;
128
129 error = pcmcia_function_configure(pf, aic_pcmcia_validate_config);
130 if (error) {
131 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
132 error);
133 return;
134 }
135
136 cfe = pf->cfe;
137 sc->sc_iot = cfe->iospace[0].handle.iot;
138 sc->sc_ioh = cfe->iospace[0].handle.ioh;
139
140 error = aic_pcmcia_enable(self, 1);
141 if (error)
142 goto fail;
143
144 if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
145 aprint_error("%s: unable to detect chip!\n", self->dv_xname);
146 goto fail2;
147 }
148
149 /* We can enable and disable the controller. */
150 sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
151 sc->sc_adapter.adapt_refcnt = 1;
152
153 aicattach(sc);
154 scsipi_adapter_delref(&sc->sc_adapter);
155 psc->sc_state = AIC_PCMCIA_ATTACHED;
156 return;
157
158 fail2:
159 aic_pcmcia_enable(self, 0);
160 fail:
161 pcmcia_function_unconfigure(pf);
162 }
163
164 int
165 aic_pcmcia_detach(self, flags)
166 struct device *self;
167 int flags;
168 {
169 struct aic_pcmcia_softc *sc = (void *)self;
170 int error;
171
172 if (sc->sc_state != AIC_PCMCIA_ATTACHED)
173 return (0);
174
175 error = aic_detach(self, flags);
176 if (error)
177 return (error);
178
179 pcmcia_function_unconfigure(sc->sc_pf);
180
181 return (0);
182 }
183
184 int
185 aic_pcmcia_enable(self, onoff)
186 struct device *self;
187 int onoff;
188 {
189 struct aic_pcmcia_softc *sc = (void *)self;
190 int error;
191
192 if (onoff) {
193 /* Establish the interrupt handler. */
194 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
195 aicintr, &sc->sc_aic);
196 if (!sc->sc_ih)
197 return (EIO);
198
199 error = pcmcia_function_enable(sc->sc_pf);
200 if (error) {
201 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
202 sc->sc_ih = 0;
203 return (error);
204 }
205
206 /* Initialize only chip. */
207 aic_init(&sc->sc_aic, 0);
208 } else {
209 pcmcia_function_disable(sc->sc_pf);
210 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
211 sc->sc_ih = 0;
212 }
213
214 return (0);
215 }
216