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