nca_pcmcia.c revision 1.22.16.1 1 1.22.16.1 mjf /* $NetBSD: nca_pcmcia.c,v 1.22.16.1 2008/06/02 13:23:46 mjf Exp $ */
2 1.1 mycroft
3 1.1 mycroft /*-
4 1.11 mycroft * Copyright (c) 2000, 2004 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 *
19 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 mycroft * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mycroft */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.22.16.1 mjf __KERNEL_RCSID(0, "$NetBSD: nca_pcmcia.c,v 1.22.16.1 2008/06/02 13:23:46 mjf Exp $");
34 1.1 mycroft
35 1.1 mycroft #include <sys/param.h>
36 1.1 mycroft #include <sys/systm.h>
37 1.1 mycroft #include <sys/device.h>
38 1.1 mycroft #include <sys/buf.h>
39 1.1 mycroft
40 1.22 ad #include <sys/bus.h>
41 1.22 ad #include <sys/intr.h>
42 1.1 mycroft
43 1.1 mycroft #include <dev/scsipi/scsi_all.h>
44 1.1 mycroft #include <dev/scsipi/scsipi_all.h>
45 1.1 mycroft #include <dev/scsipi/scsiconf.h>
46 1.1 mycroft
47 1.1 mycroft #include <dev/pcmcia/pcmciareg.h>
48 1.1 mycroft #include <dev/pcmcia/pcmciavar.h>
49 1.1 mycroft #include <dev/pcmcia/pcmciadevs.h>
50 1.1 mycroft
51 1.1 mycroft #include <dev/ic/ncr5380reg.h>
52 1.1 mycroft #include <dev/ic/ncr5380var.h>
53 1.1 mycroft #include <dev/ic/ncr53c400reg.h>
54 1.1 mycroft
55 1.1 mycroft struct nca_pcmcia_softc {
56 1.1 mycroft struct ncr5380_softc sc_ncr5380; /* glue to MI code */
57 1.1 mycroft
58 1.1 mycroft /* PCMCIA-specific goo. */
59 1.1 mycroft struct pcmcia_function *sc_pf; /* our PCMCIA function */
60 1.1 mycroft void *sc_ih; /* interrupt handler */
61 1.11 mycroft
62 1.11 mycroft int sc_state;
63 1.11 mycroft #define NCA_PCMCIA_ATTACHED 3
64 1.1 mycroft };
65 1.1 mycroft
66 1.22.16.1 mjf int nca_pcmcia_match(device_t, cfdata_t, void *);
67 1.19 perry int nca_pcmcia_validate_config(struct pcmcia_config_entry *);
68 1.22.16.1 mjf void nca_pcmcia_attach(device_t, device_t, void *);
69 1.22.16.1 mjf int nca_pcmcia_detach(device_t, int);
70 1.22.16.1 mjf int nca_pcmcia_enable(device_t, int);
71 1.1 mycroft
72 1.22.16.1 mjf CFATTACH_DECL_NEW(nca_pcmcia, sizeof(struct nca_pcmcia_softc),
73 1.9 thorpej nca_pcmcia_match, nca_pcmcia_attach, nca_pcmcia_detach, NULL);
74 1.1 mycroft
75 1.1 mycroft #define MIN_DMA_LEN 128
76 1.1 mycroft
77 1.1 mycroft /* Options for disconnect/reselect, DMA, and interrupts. */
78 1.1 mycroft #define NCA_NO_DISCONNECT 0x00ff
79 1.1 mycroft #define NCA_NO_PARITY_CHK 0xff00
80 1.1 mycroft
81 1.1 mycroft const struct pcmcia_product nca_pcmcia_products[] = {
82 1.14 mycroft { PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
83 1.14 mycroft PCMCIA_CIS_INVALID },
84 1.1 mycroft };
85 1.14 mycroft const size_t nca_pcmcia_nproducts =
86 1.22.16.1 mjf __arraycount(nca_pcmcia_products);
87 1.1 mycroft
88 1.1 mycroft int
89 1.22.16.1 mjf nca_pcmcia_match(device_t parent, cfdata_t cf, void *aux)
90 1.1 mycroft {
91 1.1 mycroft struct pcmcia_attach_args *pa = aux;
92 1.1 mycroft
93 1.14 mycroft if (pcmcia_product_lookup(pa, nca_pcmcia_products, nca_pcmcia_nproducts,
94 1.14 mycroft sizeof(nca_pcmcia_products[0]), NULL))
95 1.22.16.1 mjf return 1;
96 1.22.16.1 mjf return 0;
97 1.1 mycroft }
98 1.1 mycroft
99 1.11 mycroft int
100 1.22.16.1 mjf nca_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
101 1.11 mycroft {
102 1.22.16.1 mjf
103 1.11 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO ||
104 1.11 mycroft cfe->num_memspace != 0 ||
105 1.11 mycroft cfe->num_iospace != 1)
106 1.22.16.1 mjf return EINVAL;
107 1.22.16.1 mjf return 0;
108 1.11 mycroft }
109 1.11 mycroft
110 1.1 mycroft void
111 1.22.16.1 mjf nca_pcmcia_attach(device_t parent, device_t self, void *aux)
112 1.1 mycroft {
113 1.22.16.1 mjf struct nca_pcmcia_softc *esc = device_private(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 int flags;
119 1.11 mycroft int error;
120 1.1 mycroft
121 1.22.16.1 mjf sc->sc_dev = self;
122 1.1 mycroft esc->sc_pf = pf;
123 1.1 mycroft
124 1.11 mycroft error = pcmcia_function_configure(pf, nca_pcmcia_validate_config);
125 1.11 mycroft if (error) {
126 1.22.16.1 mjf aprint_error_dev(self, "configure failed, error=%d\n", error);
127 1.11 mycroft return;
128 1.1 mycroft }
129 1.1 mycroft
130 1.11 mycroft cfe = pf->cfe;
131 1.11 mycroft sc->sc_regt = cfe->iospace[0].handle.iot;
132 1.11 mycroft sc->sc_regh = cfe->iospace[0].handle.ioh;
133 1.1 mycroft
134 1.1 mycroft /* Initialize 5380 compatible register offsets. */
135 1.1 mycroft sc->sci_r0 = C400_5380_REG_OFFSET + 0;
136 1.1 mycroft sc->sci_r1 = C400_5380_REG_OFFSET + 1;
137 1.1 mycroft sc->sci_r2 = C400_5380_REG_OFFSET + 2;
138 1.1 mycroft sc->sci_r3 = C400_5380_REG_OFFSET + 3;
139 1.1 mycroft sc->sci_r4 = C400_5380_REG_OFFSET + 4;
140 1.1 mycroft sc->sci_r5 = C400_5380_REG_OFFSET + 5;
141 1.1 mycroft sc->sci_r6 = C400_5380_REG_OFFSET + 6;
142 1.1 mycroft sc->sci_r7 = C400_5380_REG_OFFSET + 7;
143 1.3 tsutsui
144 1.3 tsutsui sc->sc_rev = NCR_VARIANT_NCR53C400;
145 1.1 mycroft
146 1.1 mycroft /*
147 1.1 mycroft * MD function pointers used by the MI code.
148 1.1 mycroft */
149 1.1 mycroft sc->sc_pio_out = ncr5380_pio_out;
150 1.1 mycroft sc->sc_pio_in = ncr5380_pio_in;
151 1.1 mycroft sc->sc_dma_alloc = NULL;
152 1.1 mycroft sc->sc_dma_free = NULL;
153 1.1 mycroft sc->sc_dma_setup = NULL;
154 1.1 mycroft sc->sc_dma_start = NULL;
155 1.1 mycroft sc->sc_dma_poll = NULL;
156 1.1 mycroft sc->sc_dma_eop = NULL;
157 1.1 mycroft sc->sc_dma_stop = NULL;
158 1.1 mycroft sc->sc_intr_on = NULL;
159 1.1 mycroft sc->sc_intr_off = NULL;
160 1.1 mycroft
161 1.1 mycroft /*
162 1.1 mycroft * Support the "options" (config file flags).
163 1.1 mycroft * Disconnect/reselect is a per-target mask.
164 1.1 mycroft * Interrupts and DMA are per-controller.
165 1.1 mycroft */
166 1.1 mycroft #if 0
167 1.1 mycroft flags = 0x0000; /* no options */
168 1.1 mycroft #else
169 1.1 mycroft flags = 0xffff; /* all options except force poll */
170 1.1 mycroft #endif
171 1.1 mycroft
172 1.1 mycroft sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
173 1.1 mycroft sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
174 1.1 mycroft sc->sc_min_dma_len = MIN_DMA_LEN;
175 1.1 mycroft
176 1.11 mycroft error = nca_pcmcia_enable(self, 1);
177 1.12 mycroft if (error)
178 1.11 mycroft goto fail;
179 1.11 mycroft
180 1.11 mycroft sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
181 1.18 mycroft sc->sc_adapter.adapt_refcnt = 1;
182 1.1 mycroft
183 1.1 mycroft ncr5380_attach(sc);
184 1.18 mycroft scsipi_adapter_delref(&sc->sc_adapter);
185 1.11 mycroft esc->sc_state = NCA_PCMCIA_ATTACHED;
186 1.1 mycroft return;
187 1.1 mycroft
188 1.11 mycroft fail:
189 1.11 mycroft pcmcia_function_unconfigure(pf);
190 1.1 mycroft }
191 1.1 mycroft
192 1.1 mycroft int
193 1.22.16.1 mjf nca_pcmcia_detach(device_t self, int flags)
194 1.1 mycroft {
195 1.22.16.1 mjf struct nca_pcmcia_softc *sc = device_private(self);
196 1.1 mycroft int error;
197 1.1 mycroft
198 1.11 mycroft if (sc->sc_state != NCA_PCMCIA_ATTACHED)
199 1.22.16.1 mjf return 0;
200 1.1 mycroft
201 1.11 mycroft error = ncr5380_detach(&sc->sc_ncr5380, flags);
202 1.11 mycroft if (error)
203 1.22.16.1 mjf return error;
204 1.1 mycroft
205 1.11 mycroft pcmcia_function_unconfigure(sc->sc_pf);
206 1.1 mycroft
207 1.22.16.1 mjf return 0;
208 1.1 mycroft }
209 1.1 mycroft
210 1.1 mycroft int
211 1.22.16.1 mjf nca_pcmcia_enable(device_t self, int onoff)
212 1.1 mycroft {
213 1.22.16.1 mjf struct nca_pcmcia_softc *sc = device_private(self);
214 1.12 mycroft int error;
215 1.1 mycroft
216 1.1 mycroft if (onoff) {
217 1.18 mycroft /* Establish the interrupt handler. */
218 1.18 mycroft sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
219 1.18 mycroft ncr5380_intr, &sc->sc_ncr5380);
220 1.22.16.1 mjf if (sc->sc_ih == NULL)
221 1.22.16.1 mjf return EIO;
222 1.18 mycroft
223 1.18 mycroft error = pcmcia_function_enable(sc->sc_pf);
224 1.18 mycroft if (error) {
225 1.18 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
226 1.18 mycroft sc->sc_ih = 0;
227 1.22.16.1 mjf return error;
228 1.18 mycroft }
229 1.1 mycroft
230 1.18 mycroft /* Initialize only chip. */
231 1.18 mycroft ncr5380_init(&sc->sc_ncr5380);
232 1.1 mycroft } else {
233 1.11 mycroft pcmcia_function_disable(sc->sc_pf);
234 1.11 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
235 1.11 mycroft sc->sc_ih = 0;
236 1.1 mycroft }
237 1.1 mycroft
238 1.22.16.1 mjf return 0;
239 1.1 mycroft }
240