xref: /netbsd/sys/dev/pcmcia/nca_pcmcia.c (revision bf9ec67e)
1 /*	$NetBSD: nca_pcmcia.c,v 1.5 2001/11/13 07:26:34 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 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.5 2001/11/13 07:26:34 lukem 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_io_handle sc_pcioh;	/* PCMCIA i/o space info */
67 	int sc_io_window;			/* our i/o window */
68 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
69 	void *sc_ih;				/* interrupt handler */
70 	int sc_flags;
71 #define	NCA_PCMCIA_ATTACHED	1		/* attach completed */
72 #define NCA_PCMCIA_ATTACHING	2		/* attach in progress */
73 };
74 
75 int	nca_pcmcia_match __P((struct device *, struct cfdata *, void *));
76 void	nca_pcmcia_attach __P((struct device *, struct device *, void *));
77 int	nca_pcmcia_detach __P((struct device *, int));
78 int	nca_pcmcia_enable __P((struct device *, int));
79 
80 struct cfattach nca_pcmcia_ca = {
81 	sizeof(struct nca_pcmcia_softc), nca_pcmcia_match, nca_pcmcia_attach,
82 	nca_pcmcia_detach
83 };
84 
85 #define MIN_DMA_LEN 128
86 
87 /* Options for disconnect/reselect, DMA, and interrupts. */
88 #define NCA_NO_DISCONNECT	0x00ff
89 #define NCA_NO_PARITY_CHK	0xff00
90 
91 const struct pcmcia_product nca_pcmcia_products[] = {
92 
93 	{ NULL }
94 };
95 
96 int
97 nca_pcmcia_match(parent, match, aux)
98 	struct device *parent;
99 	struct cfdata *match;
100 	void *aux;
101 {
102 	struct pcmcia_attach_args *pa = aux;
103 
104 	if (pcmcia_product_lookup(pa, nca_pcmcia_products,
105 	    sizeof nca_pcmcia_products[0], NULL) != NULL)
106 		return (1);
107 	return (0);
108 }
109 
110 void
111 nca_pcmcia_attach(parent, self, aux)
112 	struct device *parent, *self;
113 	void *aux;
114 {
115 	struct nca_pcmcia_softc *esc = (void *)self;
116 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
117 	struct pcmcia_attach_args *pa = aux;
118 	struct pcmcia_config_entry *cfe;
119 	struct pcmcia_function *pf = pa->pf;
120 	const struct pcmcia_product *pp;
121 	int flags;
122 
123 	esc->sc_pf = pf;
124 
125 	for (cfe = SIMPLEQ_FIRST(&pf->cfe_head); cfe != NULL;
126 	    cfe = SIMPLEQ_NEXT(cfe, cfe_list)) {
127 		if (cfe->num_memspace != 0 ||
128 		    cfe->num_iospace != 1)
129 			continue;
130 
131 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
132 		    cfe->iospace[0].length, 0, &esc->sc_pcioh) == 0)
133 			break;
134 	}
135 
136 	if (cfe == 0) {
137 		printf(": can't alloc i/o space\n");
138 		goto no_config_entry;
139 	}
140 
141 	/* Enable the card. */
142 	pcmcia_function_init(pf, cfe);
143 	if (pcmcia_function_enable(pf)) {
144 		printf(": function enable failed\n");
145 		goto enable_failed;
146 	}
147 
148 	/* Map in the I/O space */
149 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, esc->sc_pcioh.size,
150 	    &esc->sc_pcioh, &esc->sc_io_window)) {
151 		printf(": can't map i/o space\n");
152 		goto iomap_failed;
153 	}
154 
155 	pp = pcmcia_product_lookup(pa, nca_pcmcia_products,
156 	    sizeof nca_pcmcia_products[0], NULL);
157 	if (pp == NULL) {
158 		printf("\n");
159 		panic("nca_pcmcia_attach: impossible");
160 	}
161 
162 	printf(": %s\n", pp->pp_name);
163 
164 	/* We can enable and disable the controller. */
165 	sc->sc_adapter.adapt_enable = nca_pcmcia_enable;
166 
167 	/* Reset into 5380-compat. mode */
168 	bus_space_write_1(esc->sc_pcioh.iot, esc->sc_pcioh.ioh, C400_CSR,
169 	    C400_CSR_5380_ENABLE);
170 
171 	/* Initialize 5380 compatible register offsets. */
172 	sc->sci_r0 = C400_5380_REG_OFFSET + 0;
173 	sc->sci_r1 = C400_5380_REG_OFFSET + 1;
174 	sc->sci_r2 = C400_5380_REG_OFFSET + 2;
175 	sc->sci_r3 = C400_5380_REG_OFFSET + 3;
176 	sc->sci_r4 = C400_5380_REG_OFFSET + 4;
177 	sc->sci_r5 = C400_5380_REG_OFFSET + 5;
178 	sc->sci_r6 = C400_5380_REG_OFFSET + 6;
179 	sc->sci_r7 = C400_5380_REG_OFFSET + 7;
180 
181 	sc->sc_rev = NCR_VARIANT_NCR53C400;
182 
183 	/*
184 	 * MD function pointers used by the MI code.
185 	 */
186 	sc->sc_pio_out = ncr5380_pio_out;
187 	sc->sc_pio_in =  ncr5380_pio_in;
188 	sc->sc_dma_alloc = NULL;
189 	sc->sc_dma_free  = NULL;
190 	sc->sc_dma_setup = NULL;
191 	sc->sc_dma_start = NULL;
192 	sc->sc_dma_poll  = NULL;
193 	sc->sc_dma_eop   = NULL;
194 	sc->sc_dma_stop  = NULL;
195 	sc->sc_intr_on   = NULL;
196 	sc->sc_intr_off  = NULL;
197 
198 
199 	/*
200 	 * Support the "options" (config file flags).
201 	 * Disconnect/reselect is a per-target mask.
202 	 * Interrupts and DMA are per-controller.
203 	 */
204 #if 0
205 	flags = 0x0000;	/* no options */
206 #else
207 	flags = 0xffff;	/* all options except force poll */
208 #endif
209 
210 	sc->sc_no_disconnect = (flags & NCA_NO_DISCONNECT);
211 	sc->sc_parity_disable = (flags & NCA_NO_PARITY_CHK) >> 8;
212 	sc->sc_min_dma_len = MIN_DMA_LEN;
213 
214 	/*
215 	 * Initialize fields used by the MI code
216 	 */
217 	sc->sc_regt = esc->sc_pcioh.iot;
218 	sc->sc_regh = esc->sc_pcioh.ioh;
219 
220 	/*
221 	 *  Initialize nca board itself.
222 	 */
223 	esc->sc_flags |= NCA_PCMCIA_ATTACHING;
224 	ncr5380_attach(sc);
225 	esc->sc_flags &= ~NCA_PCMCIA_ATTACHING;
226 	esc->sc_flags |= NCA_PCMCIA_ATTACHED;
227 	return;
228 
229 iomap_failed:
230 	/* Disable the device. */
231 	pcmcia_function_disable(esc->sc_pf);
232 
233 enable_failed:
234 	/* Unmap our I/O space. */
235 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
236 
237 no_config_entry:
238 	return;
239 }
240 
241 int
242 nca_pcmcia_detach(self, flags)
243 	struct device *self;
244 	int flags;
245 {
246 	struct nca_pcmcia_softc *esc = (void *)self;
247 	int error;
248 
249 	if ((esc->sc_flags & NCA_PCMCIA_ATTACHED) == 0) {
250 		/* Nothing to detach. */
251 		return (0);
252 	}
253 
254 	if ((error = ncr5380_detach(&esc->sc_ncr5380, flags)) != 0)
255 		return (error);
256 
257 	/* Unmap our i/o window and i/o space. */
258 	pcmcia_io_unmap(esc->sc_pf, esc->sc_io_window);
259 	pcmcia_io_free(esc->sc_pf, &esc->sc_pcioh);
260 
261 	return (0);
262 }
263 
264 int
265 nca_pcmcia_enable(arg, onoff)
266 	struct device *arg;
267 	int onoff;
268 {
269 	struct nca_pcmcia_softc *esc = (struct nca_pcmcia_softc*)arg;
270 
271 	if (onoff) {
272 		/* Establish the interrupt handler. */
273 		esc->sc_ih = pcmcia_intr_establish(esc->sc_pf, IPL_BIO,
274 		    ncr5380_intr, &esc->sc_ncr5380);
275 		if (esc->sc_ih == NULL) {
276 			printf("%s: couldn't establish interrupt handler\n",
277 			    esc->sc_ncr5380.sc_dev.dv_xname);
278 			return (EIO);
279 		}
280 
281 		/*
282 		 * If attach is in progress, we know that card power is
283 		 * enabled and chip will be initialized later.
284 		 * Otherwise, enable and reset now.
285 		 */
286 		if ((esc->sc_flags & NCA_PCMCIA_ATTACHING) == 0) {
287 			if (pcmcia_function_enable(esc->sc_pf)) {
288 				printf("%s: couldn't enable PCMCIA function\n",
289 				    esc->sc_ncr5380.sc_dev.dv_xname);
290 				pcmcia_intr_disestablish(esc->sc_pf,
291 				    esc->sc_ih);
292 				return (EIO);
293 			}
294 
295 			/* Initialize only chip.  */
296 			ncr5380_init(&esc->sc_ncr5380);
297 		}
298 	} else {
299 		pcmcia_function_disable(esc->sc_pf);
300 		pcmcia_intr_disestablish(esc->sc_pf, esc->sc_ih);
301 	}
302 
303 	return (0);
304 }
305