nca_pcmcia.c revision 1.22 1 /* $NetBSD: nca_pcmcia.c,v 1.22 2007/10/19 12:01:06 ad 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.22 2007/10/19 12:01:06 ad 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 <sys/bus.h>
48 #include <sys/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_ATTACHED 3
71 };
72
73 int nca_pcmcia_match(struct device *, struct cfdata *, void *);
74 int nca_pcmcia_validate_config(struct pcmcia_config_entry *);
75 void nca_pcmcia_attach(struct device *, struct device *, void *);
76 int nca_pcmcia_detach(struct device *, int);
77 int nca_pcmcia_enable(struct device *, int);
78
79 CFATTACH_DECL(nca_pcmcia, sizeof(struct nca_pcmcia_softc),
80 nca_pcmcia_match, nca_pcmcia_attach, nca_pcmcia_detach, NULL);
81
82 #define MIN_DMA_LEN 128
83
84 /* Options for disconnect/reselect, DMA, and interrupts. */
85 #define NCA_NO_DISCONNECT 0x00ff
86 #define NCA_NO_PARITY_CHK 0xff00
87
88 const struct pcmcia_product nca_pcmcia_products[] = {
89 { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
90 PCMCIA_CIS_INVALID },
91 };
92 const size_t nca_pcmcia_nproducts =
93 sizeof(nca_pcmcia_products) / sizeof(nca_pcmcia_products[0]);
94
95 int
96 nca_pcmcia_match(parent, match, aux)
97 struct device *parent;
98 struct cfdata *match;
99 void *aux;
100 {
101 struct pcmcia_attach_args *pa = aux;
102
103 if (pcmcia_product_lookup(pa, nca_pcmcia_products, nca_pcmcia_nproducts,
104 sizeof(nca_pcmcia_products[0]), NULL))
105 return (1);
106 return (0);
107 }
108
109 int
110 nca_pcmcia_validate_config(cfe)
111 struct pcmcia_config_entry *cfe;
112 {
113 if (cfe->iftype != PCMCIA_IFTYPE_IO ||
114 cfe->num_memspace != 0 ||
115 cfe->num_iospace != 1)
116 return (EINVAL);
117 return (0);
118 }
119
120 void
121 nca_pcmcia_attach(parent, self, aux)
122 struct device *parent, *self;
123 void *aux;
124 {
125 struct nca_pcmcia_softc *esc = (void *)self;
126 struct ncr5380_softc *sc = &esc->sc_ncr5380;
127 struct pcmcia_attach_args *pa = aux;
128 struct pcmcia_config_entry *cfe;
129 struct pcmcia_function *pf = pa->pf;
130 int flags;
131 int error;
132
133 esc->sc_pf = pf;
134
135 error = pcmcia_function_configure(pf, nca_pcmcia_validate_config);
136 if (error) {
137 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
138 error);
139 return;
140 }
141
142 cfe = pf->cfe;
143 sc->sc_regt = cfe->iospace[0].handle.iot;
144 sc->sc_regh = cfe->iospace[0].handle.ioh;
145
146 /* Initialize 5380 compatible register offsets. */
147 sc->sci_r0 = C400_5380_REG_OFFSET + 0;
148 sc->sci_r1 = C400_5380_REG_OFFSET + 1;
149 sc->sci_r2 = C400_5380_REG_OFFSET + 2;
150 sc->sci_r3 = C400_5380_REG_OFFSET + 3;
151 sc->sci_r4 = C400_5380_REG_OFFSET + 4;
152 sc->sci_r5 = C400_5380_REG_OFFSET + 5;
153 sc->sci_r6 = C400_5380_REG_OFFSET + 6;
154 sc->sci_r7 = C400_5380_REG_OFFSET + 7;
155
156 sc->sc_rev = NCR_VARIANT_NCR53C400;
157
158 /*
159 * MD function pointers used by the MI code.
160 */
161 sc->sc_pio_out = ncr5380_pio_out;
162 sc->sc_pio_in = ncr5380_pio_in;
163 sc->sc_dma_alloc = NULL;
164 sc->sc_dma_free = NULL;
165 sc->sc_dma_setup = NULL;
166 sc->sc_dma_start = NULL;
167 sc->sc_dma_poll = NULL;
168 sc->sc_dma_eop = NULL;
169 sc->sc_dma_stop = NULL;
170 sc->sc_intr_on = NULL;
171 sc->sc_intr_off = NULL;
172
173 /*
174 * Support the "options" (config file flags).
175 * Disconnect/reselect is a per-target mask.
176 * Interrupts and DMA are per-controller.
177 */
178 #if 0
179 flags = 0x0000; /* no options */
180 #else
181 flags = 0xffff; /* all options except force poll */
182 #endif
183
184 sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
185 sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
186 sc->sc_min_dma_len = MIN_DMA_LEN;
187
188 error = nca_pcmcia_enable(self, 1);
189 if (error)
190 goto fail;
191
192 sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
193 sc->sc_adapter.adapt_refcnt = 1;
194
195 ncr5380_attach(sc);
196 scsipi_adapter_delref(&sc->sc_adapter);
197 esc->sc_state = NCA_PCMCIA_ATTACHED;
198 return;
199
200 fail:
201 pcmcia_function_unconfigure(pf);
202 }
203
204 int
205 nca_pcmcia_detach(self, flags)
206 struct device *self;
207 int flags;
208 {
209 struct nca_pcmcia_softc *sc = (void *)self;
210 int error;
211
212 if (sc->sc_state != NCA_PCMCIA_ATTACHED)
213 return (0);
214
215 error = ncr5380_detach(&sc->sc_ncr5380, flags);
216 if (error)
217 return (error);
218
219 pcmcia_function_unconfigure(sc->sc_pf);
220
221 return (0);
222 }
223
224 int
225 nca_pcmcia_enable(arg, onoff)
226 struct device *arg;
227 int onoff;
228 {
229 struct nca_pcmcia_softc *sc = (struct nca_pcmcia_softc*)arg;
230 int error;
231
232 if (onoff) {
233 /* Establish the interrupt handler. */
234 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
235 ncr5380_intr, &sc->sc_ncr5380);
236 if (!sc->sc_ih)
237 return (EIO);
238
239 error = pcmcia_function_enable(sc->sc_pf);
240 if (error) {
241 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
242 sc->sc_ih = 0;
243 return (error);
244 }
245
246 /* Initialize only chip. */
247 ncr5380_init(&sc->sc_ncr5380);
248 } else {
249 pcmcia_function_disable(sc->sc_pf);
250 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
251 sc->sc_ih = 0;
252 }
253
254 return (0);
255 }
256