aic_pcmcia.c revision 1.24 1 1.24 mycroft /* $NetBSD: aic_pcmcia.c,v 1.24 2004/08/09 14:24:45 mycroft Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*
4 1.2 thorpej * Copyright (c) 1997 Marc Horowitz. All rights reserved.
5 1.2 thorpej *
6 1.2 thorpej * Redistribution and use in source and binary forms, with or without
7 1.2 thorpej * modification, are permitted provided that the following conditions
8 1.2 thorpej * are met:
9 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.2 thorpej * notice, this list of conditions and the following disclaimer.
11 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.2 thorpej * documentation and/or other materials provided with the distribution.
14 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
15 1.2 thorpej * must display the following acknowledgement:
16 1.2 thorpej * This product includes software developed by Marc Horowitz.
17 1.2 thorpej * 4. The name of the author may not be used to endorse or promote products
18 1.2 thorpej * derived from this software without specific prior written permission.
19 1.2 thorpej *
20 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.2 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.2 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.2 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.2 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.2 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.2 thorpej */
31 1.18 lukem
32 1.18 lukem #include <sys/cdefs.h>
33 1.24 mycroft __KERNEL_RCSID(0, "$NetBSD: aic_pcmcia.c,v 1.24 2004/08/09 14:24:45 mycroft Exp $");
34 1.2 thorpej
35 1.2 thorpej #include <sys/param.h>
36 1.2 thorpej #include <sys/systm.h>
37 1.2 thorpej #include <sys/select.h>
38 1.2 thorpej #include <sys/device.h>
39 1.2 thorpej
40 1.2 thorpej #include <machine/cpu.h>
41 1.2 thorpej #include <machine/bus.h>
42 1.2 thorpej #include <machine/intr.h>
43 1.2 thorpej
44 1.2 thorpej #include <dev/scsipi/scsipi_all.h>
45 1.2 thorpej #include <dev/scsipi/scsipiconf.h>
46 1.2 thorpej #include <dev/scsipi/scsi_all.h>
47 1.2 thorpej
48 1.2 thorpej #include <dev/ic/aic6360var.h>
49 1.2 thorpej
50 1.2 thorpej #include <dev/pcmcia/pcmciareg.h>
51 1.2 thorpej #include <dev/pcmcia/pcmciavar.h>
52 1.6 christos #include <dev/pcmcia/pcmciadevs.h>
53 1.2 thorpej
54 1.2 thorpej int aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
55 1.2 thorpej void aic_pcmcia_attach __P((struct device *, struct device *, void *));
56 1.12 enami int aic_pcmcia_detach __P((struct device *, int));
57 1.12 enami
58 1.2 thorpej struct aic_pcmcia_softc {
59 1.2 thorpej struct aic_softc sc_aic; /* real "aic" softc */
60 1.2 thorpej
61 1.2 thorpej /* PCMCIA-specific goo. */
62 1.2 thorpej struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
63 1.2 thorpej int sc_io_window; /* our i/o window */
64 1.2 thorpej struct pcmcia_function *sc_pf; /* our PCMCIA function */
65 1.2 thorpej void *sc_ih; /* interrupt handler */
66 1.13 enami int sc_flags;
67 1.14 enami #define AIC_PCMCIA_ATTACH 0x0001 /* attach is in progress */
68 1.2 thorpej };
69 1.2 thorpej
70 1.21 thorpej CFATTACH_DECL(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
71 1.22 thorpej aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, aic_activate);
72 1.2 thorpej
73 1.17 bouyer int aic_pcmcia_enable __P((struct device *, int));
74 1.8 thorpej
75 1.15 cgd const struct pcmcia_product aic_pcmcia_products[] = {
76 1.15 cgd { PCMCIA_STR_ADAPTEC_APA1460, PCMCIA_VENDOR_ADAPTEC,
77 1.15 cgd PCMCIA_PRODUCT_ADAPTEC_APA1460, 0 },
78 1.7 thorpej
79 1.15 cgd { PCMCIA_STR_ADAPTEC_APA1460A, PCMCIA_VENDOR_ADAPTEC,
80 1.15 cgd PCMCIA_PRODUCT_ADAPTEC_APA1460A, 0 },
81 1.7 thorpej
82 1.15 cgd { PCMCIA_STR_NEWMEDIA_BUSTOASTER, PCMCIA_VENDOR_NEWMEDIA,
83 1.15 cgd PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER, 0 },
84 1.7 thorpej
85 1.15 cgd { NULL }
86 1.15 cgd };
87 1.7 thorpej
88 1.2 thorpej int
89 1.2 thorpej aic_pcmcia_match(parent, match, aux)
90 1.2 thorpej struct device *parent;
91 1.3 drochner struct cfdata *match;
92 1.2 thorpej void *aux;
93 1.2 thorpej {
94 1.2 thorpej struct pcmcia_attach_args *pa = aux;
95 1.2 thorpej
96 1.15 cgd if (pcmcia_product_lookup(pa, aic_pcmcia_products,
97 1.15 cgd sizeof aic_pcmcia_products[0], NULL) != NULL)
98 1.7 thorpej return (1);
99 1.2 thorpej return (0);
100 1.2 thorpej }
101 1.2 thorpej
102 1.2 thorpej void
103 1.2 thorpej aic_pcmcia_attach(parent, self, aux)
104 1.2 thorpej struct device *parent, *self;
105 1.2 thorpej void *aux;
106 1.2 thorpej {
107 1.2 thorpej struct aic_pcmcia_softc *psc = (void *)self;
108 1.2 thorpej struct aic_softc *sc = &psc->sc_aic;
109 1.2 thorpej struct pcmcia_attach_args *pa = aux;
110 1.2 thorpej struct pcmcia_config_entry *cfe;
111 1.2 thorpej struct pcmcia_function *pf = pa->pf;
112 1.15 cgd const struct pcmcia_product *pp;
113 1.2 thorpej
114 1.2 thorpej psc->sc_pf = pf;
115 1.2 thorpej
116 1.19 lukem SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
117 1.2 thorpej if (cfe->num_memspace != 0 ||
118 1.2 thorpej cfe->num_iospace != 1)
119 1.10 mjl continue;
120 1.10 mjl
121 1.14 enami /*
122 1.14 enami * The bustoaster has a default config as first
123 1.14 enami * entry, we don't want to use that.
124 1.14 enami */
125 1.10 mjl if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
126 1.10 mjl pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
127 1.10 mjl cfe->iospace[0].start == 0)
128 1.2 thorpej continue;
129 1.2 thorpej
130 1.2 thorpej if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
131 1.24 mycroft cfe->iospace[0].length, cfe->iospace[0].length,
132 1.24 mycroft &psc->sc_pcioh) == 0)
133 1.2 thorpej break;
134 1.2 thorpej }
135 1.2 thorpej
136 1.2 thorpej if (cfe == 0) {
137 1.2 thorpej printf(": can't alloc i/o space\n");
138 1.16 enami goto no_config_entry;
139 1.2 thorpej }
140 1.2 thorpej
141 1.2 thorpej sc->sc_iot = psc->sc_pcioh.iot;
142 1.2 thorpej sc->sc_ioh = psc->sc_pcioh.ioh;
143 1.2 thorpej
144 1.2 thorpej /* Enable the card. */
145 1.2 thorpej pcmcia_function_init(pf, cfe);
146 1.2 thorpej if (pcmcia_function_enable(pf)) {
147 1.2 thorpej printf(": function enable failed\n");
148 1.16 enami goto enable_failed;
149 1.2 thorpej }
150 1.2 thorpej
151 1.2 thorpej /* Map in the io space */
152 1.23 mycroft if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, &psc->sc_pcioh,
153 1.23 mycroft &psc->sc_io_window)) {
154 1.2 thorpej printf(": can't map i/o space\n");
155 1.16 enami goto iomap_failed;
156 1.2 thorpej }
157 1.2 thorpej
158 1.7 thorpej if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
159 1.7 thorpej printf(": unable to detect chip!\n");
160 1.16 enami goto no_aic_found;
161 1.7 thorpej }
162 1.2 thorpej
163 1.15 cgd pp = pcmcia_product_lookup(pa, aic_pcmcia_products,
164 1.15 cgd sizeof aic_pcmcia_products[0], NULL);
165 1.15 cgd if (pp == NULL) {
166 1.7 thorpej printf("\n");
167 1.7 thorpej panic("aic_pcmcia_attach: impossible");
168 1.6 christos }
169 1.6 christos
170 1.15 cgd printf(": %s\n", pp->pp_name);
171 1.12 enami
172 1.8 thorpej /* We can enable and disable the controller. */
173 1.17 bouyer sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
174 1.2 thorpej
175 1.13 enami psc->sc_flags |= AIC_PCMCIA_ATTACH;
176 1.2 thorpej aicattach(sc);
177 1.13 enami psc->sc_flags &= ~AIC_PCMCIA_ATTACH;
178 1.16 enami return;
179 1.16 enami
180 1.16 enami no_aic_found:
181 1.16 enami /* Unmap our i/o window. */
182 1.16 enami pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
183 1.16 enami
184 1.16 enami iomap_failed:
185 1.16 enami /* Disable the device. */
186 1.16 enami pcmcia_function_disable(psc->sc_pf);
187 1.16 enami
188 1.16 enami enable_failed:
189 1.16 enami /* Unmap our i/o space. */
190 1.16 enami pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
191 1.16 enami
192 1.16 enami no_config_entry:
193 1.16 enami psc->sc_io_window = -1;
194 1.8 thorpej }
195 1.8 thorpej
196 1.8 thorpej int
197 1.12 enami aic_pcmcia_detach(self, flags)
198 1.12 enami struct device *self;
199 1.12 enami int flags;
200 1.12 enami {
201 1.12 enami struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
202 1.12 enami int error;
203 1.16 enami
204 1.16 enami if (sc->sc_io_window == -1)
205 1.16 enami /* Nothing to detach. */
206 1.16 enami return (0);
207 1.12 enami
208 1.12 enami if ((error = aic_detach(self, flags)) != 0)
209 1.12 enami return (error);
210 1.12 enami
211 1.12 enami /* Unmap our i/o window and i/o space. */
212 1.12 enami pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
213 1.12 enami pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
214 1.12 enami
215 1.12 enami return (0);
216 1.12 enami }
217 1.12 enami int
218 1.17 bouyer aic_pcmcia_enable(self, onoff)
219 1.17 bouyer struct device *self;
220 1.8 thorpej int onoff;
221 1.8 thorpej {
222 1.17 bouyer struct aic_pcmcia_softc *psc = (void *)self;
223 1.2 thorpej
224 1.8 thorpej if (onoff) {
225 1.8 thorpej /* Establish the interrupt handler. */
226 1.8 thorpej psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
227 1.8 thorpej aicintr, &psc->sc_aic);
228 1.8 thorpej if (psc->sc_ih == NULL) {
229 1.8 thorpej printf("%s: couldn't establish interrupt handler\n",
230 1.8 thorpej psc->sc_aic.sc_dev.dv_xname);
231 1.8 thorpej return (EIO);
232 1.8 thorpej }
233 1.8 thorpej
234 1.14 enami /*
235 1.14 enami * If attach is in progress, we know that card power is
236 1.14 enami * enabled and chip will be initialized later.
237 1.14 enami * Otherwise, enable and reset now.
238 1.14 enami */
239 1.13 enami if ((psc->sc_flags & AIC_PCMCIA_ATTACH) == 0) {
240 1.13 enami if (pcmcia_function_enable(psc->sc_pf)) {
241 1.13 enami printf("%s: couldn't enable PCMCIA function\n",
242 1.13 enami psc->sc_aic.sc_dev.dv_xname);
243 1.13 enami pcmcia_intr_disestablish(psc->sc_pf,
244 1.13 enami psc->sc_ih);
245 1.13 enami return (EIO);
246 1.13 enami }
247 1.13 enami
248 1.13 enami /* Initialize only chip. */
249 1.13 enami aic_init(&psc->sc_aic, 0);
250 1.8 thorpej }
251 1.8 thorpej } else {
252 1.8 thorpej pcmcia_function_disable(psc->sc_pf);
253 1.8 thorpej pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
254 1.8 thorpej }
255 1.8 thorpej
256 1.8 thorpej return (0);
257 1.2 thorpej }
258