aic_pcmcia.c revision 1.25 1 /* $NetBSD: aic_pcmcia.c,v 1.25 2004/08/10 06:08:58 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.25 2004/08/10 06:08:58 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 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_ATTACH1 1
63 #define AIC_PCMCIA_ATTACH2 2
64 #define AIC_PCMCIA_ATTACHED 3
65 };
66
67 int aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
68 int aic_pcmcia_validate_config __P((struct pcmcia_config_entry *));
69 void aic_pcmcia_attach __P((struct device *, struct device *, void *));
70 int aic_pcmcia_detach __P((struct device *, int));
71 int aic_pcmcia_enable __P((struct device *, int));
72
73 CFATTACH_DECL(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
74 aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, aic_activate);
75
76 const struct pcmcia_product aic_pcmcia_products[] = {
77 { "", PCMCIA_VENDOR_ADAPTEC,
78 PCMCIA_PRODUCT_ADAPTEC_APA1460, 0 },
79
80 { "", PCMCIA_VENDOR_ADAPTEC,
81 PCMCIA_PRODUCT_ADAPTEC_APA1460A, 0 },
82
83 { "", PCMCIA_VENDOR_NEWMEDIA,
84 PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER, 0 },
85
86 { NULL }
87 };
88
89 int
90 aic_pcmcia_match(parent, match, aux)
91 struct device *parent;
92 struct cfdata *match;
93 void *aux;
94 {
95 struct pcmcia_attach_args *pa = aux;
96
97 if (pcmcia_product_lookup(pa, aic_pcmcia_products,
98 sizeof aic_pcmcia_products[0], NULL) != NULL)
99 return (1);
100 return (0);
101 }
102
103 int
104 aic_pcmcia_validate_config(cfe)
105 struct pcmcia_config_entry *cfe;
106 {
107 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
108 cfe->num_memspace != 0 ||
109 cfe->num_iospace != 1)
110 return (EINVAL);
111 return (0);
112 }
113
114 void
115 aic_pcmcia_attach(parent, self, aux)
116 struct device *parent, *self;
117 void *aux;
118 {
119 struct aic_pcmcia_softc *psc = (void *)self;
120 struct aic_softc *sc = &psc->sc_aic;
121 struct pcmcia_attach_args *pa = aux;
122 struct pcmcia_config_entry *cfe;
123 struct pcmcia_function *pf = pa->pf;
124 int error;
125
126 aprint_normal("\n");
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 if (aic_pcmcia_enable(self, 1)) {
141 aprint_error("%s: enable failed, error=%d\n", self->dv_xname,
142 error);
143 goto fail;
144 }
145
146 if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
147 aprint_error("%s: unable to detect chip!\n", self->dv_xname);
148 goto fail2;
149 }
150
151 /* We can enable and disable the controller. */
152 sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
153
154 psc->sc_state = AIC_PCMCIA_ATTACH1;
155 aicattach(sc);
156 if (psc->sc_state == AIC_PCMCIA_ATTACH1)
157 aic_pcmcia_enable(self, 0);
158 psc->sc_state = AIC_PCMCIA_ATTACHED;
159 return;
160
161 fail2:
162 aic_pcmcia_enable(self, 0);
163 fail:
164 pcmcia_function_unconfigure(pf);
165 }
166
167 int
168 aic_pcmcia_detach(self, flags)
169 struct device *self;
170 int flags;
171 {
172 struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
173 int error;
174
175 if (sc->sc_state != AIC_PCMCIA_ATTACHED)
176 return (0);
177
178 error = aic_detach(self, flags);
179 if (error)
180 return (error);
181
182 pcmcia_function_unconfigure(sc->sc_pf);
183
184 return (0);
185 }
186
187 int
188 aic_pcmcia_enable(self, onoff)
189 struct device *self;
190 int onoff;
191 {
192 struct aic_pcmcia_softc *sc = (void *)self;
193
194 if (onoff) {
195 /*
196 * If attach is in progress, we already have the device
197 * powered up.
198 */
199 if (sc->sc_state == AIC_PCMCIA_ATTACH1) {
200 sc->sc_state = AIC_PCMCIA_ATTACH2;
201 } else {
202 /* Establish the interrupt handler. */
203 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
204 aicintr, &sc->sc_aic);
205 if (sc->sc_ih == NULL) {
206 printf("%s: couldn't establish interrupt handler\n",
207 sc->sc_aic.sc_dev.dv_xname);
208 return (EIO);
209 }
210
211 if (pcmcia_function_enable(sc->sc_pf)) {
212 printf("%s: couldn't enable PCMCIA function\n",
213 sc->sc_aic.sc_dev.dv_xname);
214 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
215 return (EIO);
216 }
217
218 /* Initialize only chip. */
219 aic_init(&sc->sc_aic, 0);
220 }
221 } else {
222 pcmcia_function_disable(sc->sc_pf);
223 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
224 sc->sc_ih = 0;
225 }
226
227 return (0);
228 }
229