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