nca_pcmcia.c revision 1.14 1 /* $NetBSD: nca_pcmcia.c,v 1.14 2004/08/10 18:39:08 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: nca_pcmcia.c,v 1.14 2004/08/10 18:39:08 mycroft Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/buf.h>
46
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49
50 #include <dev/scsipi/scsi_all.h>
51 #include <dev/scsipi/scsipi_all.h>
52 #include <dev/scsipi/scsiconf.h>
53
54 #include <dev/pcmcia/pcmciareg.h>
55 #include <dev/pcmcia/pcmciavar.h>
56 #include <dev/pcmcia/pcmciadevs.h>
57
58 #include <dev/ic/ncr5380reg.h>
59 #include <dev/ic/ncr5380var.h>
60 #include <dev/ic/ncr53c400reg.h>
61
62 struct nca_pcmcia_softc {
63 struct ncr5380_softc sc_ncr5380; /* glue to MI code */
64
65 /* PCMCIA-specific goo. */
66 struct pcmcia_function *sc_pf; /* our PCMCIA function */
67 void *sc_ih; /* interrupt handler */
68
69 int sc_state;
70 #define NCA_PCMCIA_ATTACH1 1
71 #define NCA_PCMCIA_ATTACH2 2
72 #define NCA_PCMCIA_ATTACHED 3
73 };
74
75 int nca_pcmcia_match __P((struct device *, struct cfdata *, void *));
76 int nca_pcmcia_validate_config __P((struct pcmcia_config_entry *));
77 void nca_pcmcia_attach __P((struct device *, struct device *, void *));
78 int nca_pcmcia_detach __P((struct device *, int));
79 int nca_pcmcia_enable __P((struct device *, int));
80
81 CFATTACH_DECL(nca_pcmcia, sizeof(struct nca_pcmcia_softc),
82 nca_pcmcia_match, nca_pcmcia_attach, nca_pcmcia_detach, NULL);
83
84 #define MIN_DMA_LEN 128
85
86 /* Options for disconnect/reselect, DMA, and interrupts. */
87 #define NCA_NO_DISCONNECT 0x00ff
88 #define NCA_NO_PARITY_CHK 0xff00
89
90 const struct pcmcia_product nca_pcmcia_products[] = {
91 { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
92 PCMCIA_CIS_INVALID },
93 };
94 const size_t nca_pcmcia_nproducts =
95 sizeof(nca_pcmcia_products) / sizeof(nca_pcmcia_products[0]);
96
97 int
98 nca_pcmcia_match(parent, match, aux)
99 struct device *parent;
100 struct cfdata *match;
101 void *aux;
102 {
103 struct pcmcia_attach_args *pa = aux;
104
105 if (pcmcia_product_lookup(pa, nca_pcmcia_products, nca_pcmcia_nproducts,
106 sizeof(nca_pcmcia_products[0]), NULL))
107 return (1);
108 return (0);
109 }
110
111 int
112 nca_pcmcia_validate_config(cfe)
113 struct pcmcia_config_entry *cfe;
114 {
115 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
116 cfe->num_memspace != 0 ||
117 cfe->num_iospace != 1)
118 return (EINVAL);
119 return (0);
120 }
121
122 void
123 nca_pcmcia_attach(parent, self, aux)
124 struct device *parent, *self;
125 void *aux;
126 {
127 struct nca_pcmcia_softc *esc = (void *)self;
128 struct ncr5380_softc *sc = &esc->sc_ncr5380;
129 struct pcmcia_attach_args *pa = aux;
130 struct pcmcia_config_entry *cfe;
131 struct pcmcia_function *pf = pa->pf;
132 int flags;
133 int error;
134
135 aprint_normal("\n");
136 esc->sc_pf = pf;
137
138 error = pcmcia_function_configure(pf, nca_pcmcia_validate_config);
139 if (error) {
140 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
141 error);
142 return;
143 }
144
145 cfe = pf->cfe;
146 sc->sc_regt = cfe->iospace[0].handle.iot;
147 sc->sc_regh = cfe->iospace[0].handle.ioh;
148
149 /* Initialize 5380 compatible register offsets. */
150 sc->sci_r0 = C400_5380_REG_OFFSET + 0;
151 sc->sci_r1 = C400_5380_REG_OFFSET + 1;
152 sc->sci_r2 = C400_5380_REG_OFFSET + 2;
153 sc->sci_r3 = C400_5380_REG_OFFSET + 3;
154 sc->sci_r4 = C400_5380_REG_OFFSET + 4;
155 sc->sci_r5 = C400_5380_REG_OFFSET + 5;
156 sc->sci_r6 = C400_5380_REG_OFFSET + 6;
157 sc->sci_r7 = C400_5380_REG_OFFSET + 7;
158
159 sc->sc_rev = NCR_VARIANT_NCR53C400;
160
161 /*
162 * MD function pointers used by the MI code.
163 */
164 sc->sc_pio_out = ncr5380_pio_out;
165 sc->sc_pio_in = ncr5380_pio_in;
166 sc->sc_dma_alloc = NULL;
167 sc->sc_dma_free = NULL;
168 sc->sc_dma_setup = NULL;
169 sc->sc_dma_start = NULL;
170 sc->sc_dma_poll = NULL;
171 sc->sc_dma_eop = NULL;
172 sc->sc_dma_stop = NULL;
173 sc->sc_intr_on = NULL;
174 sc->sc_intr_off = NULL;
175
176 /*
177 * Support the "options" (config file flags).
178 * Disconnect/reselect is a per-target mask.
179 * Interrupts and DMA are per-controller.
180 */
181 #if 0
182 flags = 0x0000; /* no options */
183 #else
184 flags = 0xffff; /* all options except force poll */
185 #endif
186
187 sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
188 sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
189 sc->sc_min_dma_len = MIN_DMA_LEN;
190
191 error = nca_pcmcia_enable(self, 1);
192 if (error)
193 goto fail;
194
195 sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
196
197 esc->sc_state = NCA_PCMCIA_ATTACH1;
198 ncr5380_attach(sc);
199 if (esc->sc_state == NCA_PCMCIA_ATTACH1)
200 pcmcia_function_disable(esc->sc_pf);
201 esc->sc_state = NCA_PCMCIA_ATTACHED;
202 return;
203
204 fail:
205 pcmcia_function_unconfigure(pf);
206 }
207
208 int
209 nca_pcmcia_detach(self, flags)
210 struct device *self;
211 int flags;
212 {
213 struct nca_pcmcia_softc *sc = (void *)self;
214 int error;
215
216 if (sc->sc_state != NCA_PCMCIA_ATTACHED)
217 return (0);
218
219 error = ncr5380_detach(&sc->sc_ncr5380, flags);
220 if (error)
221 return (error);
222
223 pcmcia_function_unconfigure(sc->sc_pf);
224
225 return (0);
226 }
227
228 int
229 nca_pcmcia_enable(arg, onoff)
230 struct device *arg;
231 int onoff;
232 {
233 struct nca_pcmcia_softc *sc = (struct nca_pcmcia_softc*)arg;
234 int error;
235
236 if (onoff) {
237 /*
238 * If attach is in progress, we already have the device
239 * powered up.
240 */
241 if (sc->sc_state == NCA_PCMCIA_ATTACH1) {
242 sc->sc_state = NCA_PCMCIA_ATTACH2;
243 } else {
244 /* Establish the interrupt handler. */
245 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
246 ncr5380_intr, &sc->sc_ncr5380);
247 if (!sc->sc_ih)
248 return (EIO);
249
250 error = pcmcia_function_enable(sc->sc_pf);
251 if (error) {
252 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
253 sc->sc_ih = 0;
254 return (error);
255 }
256
257 /* Initialize only chip. */
258 ncr5380_init(&sc->sc_ncr5380);
259 }
260 } else {
261 pcmcia_function_disable(sc->sc_pf);
262 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
263 sc->sc_ih = 0;
264 }
265
266 return (0);
267 }
268