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