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