aic_pcmcia.c revision 1.24 1 /* $NetBSD: aic_pcmcia.c,v 1.24 2004/08/09 14:24:45 mycroft 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.24 2004/08/09 14:24:45 mycroft 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 int aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
55 void aic_pcmcia_attach __P((struct device *, struct device *, void *));
56 int aic_pcmcia_detach __P((struct device *, int));
57
58 struct aic_pcmcia_softc {
59 struct aic_softc sc_aic; /* real "aic" softc */
60
61 /* PCMCIA-specific goo. */
62 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
63 int sc_io_window; /* our i/o window */
64 struct pcmcia_function *sc_pf; /* our PCMCIA function */
65 void *sc_ih; /* interrupt handler */
66 int sc_flags;
67 #define AIC_PCMCIA_ATTACH 0x0001 /* attach is in progress */
68 };
69
70 CFATTACH_DECL(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
71 aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, aic_activate);
72
73 int aic_pcmcia_enable __P((struct device *, int));
74
75 const struct pcmcia_product aic_pcmcia_products[] = {
76 { PCMCIA_STR_ADAPTEC_APA1460, PCMCIA_VENDOR_ADAPTEC,
77 PCMCIA_PRODUCT_ADAPTEC_APA1460, 0 },
78
79 { PCMCIA_STR_ADAPTEC_APA1460A, PCMCIA_VENDOR_ADAPTEC,
80 PCMCIA_PRODUCT_ADAPTEC_APA1460A, 0 },
81
82 { PCMCIA_STR_NEWMEDIA_BUSTOASTER, PCMCIA_VENDOR_NEWMEDIA,
83 PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER, 0 },
84
85 { NULL }
86 };
87
88 int
89 aic_pcmcia_match(parent, match, aux)
90 struct device *parent;
91 struct cfdata *match;
92 void *aux;
93 {
94 struct pcmcia_attach_args *pa = aux;
95
96 if (pcmcia_product_lookup(pa, aic_pcmcia_products,
97 sizeof aic_pcmcia_products[0], NULL) != NULL)
98 return (1);
99 return (0);
100 }
101
102 void
103 aic_pcmcia_attach(parent, self, aux)
104 struct device *parent, *self;
105 void *aux;
106 {
107 struct aic_pcmcia_softc *psc = (void *)self;
108 struct aic_softc *sc = &psc->sc_aic;
109 struct pcmcia_attach_args *pa = aux;
110 struct pcmcia_config_entry *cfe;
111 struct pcmcia_function *pf = pa->pf;
112 const struct pcmcia_product *pp;
113
114 psc->sc_pf = pf;
115
116 SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
117 if (cfe->num_memspace != 0 ||
118 cfe->num_iospace != 1)
119 continue;
120
121 /*
122 * The bustoaster has a default config as first
123 * entry, we don't want to use that.
124 */
125 if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
126 pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
127 cfe->iospace[0].start == 0)
128 continue;
129
130 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
131 cfe->iospace[0].length, cfe->iospace[0].length,
132 &psc->sc_pcioh) == 0)
133 break;
134 }
135
136 if (cfe == 0) {
137 printf(": can't alloc i/o space\n");
138 goto no_config_entry;
139 }
140
141 sc->sc_iot = psc->sc_pcioh.iot;
142 sc->sc_ioh = psc->sc_pcioh.ioh;
143
144 /* Enable the card. */
145 pcmcia_function_init(pf, cfe);
146 if (pcmcia_function_enable(pf)) {
147 printf(": function enable failed\n");
148 goto enable_failed;
149 }
150
151 /* Map in the io space */
152 if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
153 &psc->sc_io_window)) {
154 printf(": can't map i/o space\n");
155 goto iomap_failed;
156 }
157
158 if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
159 printf(": unable to detect chip!\n");
160 goto no_aic_found;
161 }
162
163 pp = pcmcia_product_lookup(pa, aic_pcmcia_products,
164 sizeof aic_pcmcia_products[0], NULL);
165 if (pp == NULL) {
166 printf("\n");
167 panic("aic_pcmcia_attach: impossible");
168 }
169
170 printf(": %s\n", pp->pp_name);
171
172 /* We can enable and disable the controller. */
173 sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
174
175 psc->sc_flags |= AIC_PCMCIA_ATTACH;
176 aicattach(sc);
177 psc->sc_flags &= ~AIC_PCMCIA_ATTACH;
178 return;
179
180 no_aic_found:
181 /* Unmap our i/o window. */
182 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
183
184 iomap_failed:
185 /* Disable the device. */
186 pcmcia_function_disable(psc->sc_pf);
187
188 enable_failed:
189 /* Unmap our i/o space. */
190 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
191
192 no_config_entry:
193 psc->sc_io_window = -1;
194 }
195
196 int
197 aic_pcmcia_detach(self, flags)
198 struct device *self;
199 int flags;
200 {
201 struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
202 int error;
203
204 if (sc->sc_io_window == -1)
205 /* Nothing to detach. */
206 return (0);
207
208 if ((error = aic_detach(self, flags)) != 0)
209 return (error);
210
211 /* Unmap our i/o window and i/o space. */
212 pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
213 pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
214
215 return (0);
216 }
217 int
218 aic_pcmcia_enable(self, onoff)
219 struct device *self;
220 int onoff;
221 {
222 struct aic_pcmcia_softc *psc = (void *)self;
223
224 if (onoff) {
225 /* Establish the interrupt handler. */
226 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
227 aicintr, &psc->sc_aic);
228 if (psc->sc_ih == NULL) {
229 printf("%s: couldn't establish interrupt handler\n",
230 psc->sc_aic.sc_dev.dv_xname);
231 return (EIO);
232 }
233
234 /*
235 * If attach is in progress, we know that card power is
236 * enabled and chip will be initialized later.
237 * Otherwise, enable and reset now.
238 */
239 if ((psc->sc_flags & AIC_PCMCIA_ATTACH) == 0) {
240 if (pcmcia_function_enable(psc->sc_pf)) {
241 printf("%s: couldn't enable PCMCIA function\n",
242 psc->sc_aic.sc_dev.dv_xname);
243 pcmcia_intr_disestablish(psc->sc_pf,
244 psc->sc_ih);
245 return (EIO);
246 }
247
248 /* Initialize only chip. */
249 aic_init(&psc->sc_aic, 0);
250 }
251 } else {
252 pcmcia_function_disable(psc->sc_pf);
253 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
254 }
255
256 return (0);
257 }
258