xref: /netbsd/sys/dev/pcmcia/aic_pcmcia.c (revision c4a72b64)
1 /*	$NetBSD: aic_pcmcia.c,v 1.22 2002/10/02 16:52:04 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Marc Horowitz.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: aic_pcmcia.c,v 1.22 2002/10/02 16:52:04 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/select.h>
38 #include <sys/device.h>
39 
40 #include <machine/cpu.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43 
44 #include <dev/scsipi/scsipi_all.h>
45 #include <dev/scsipi/scsipiconf.h>
46 #include <dev/scsipi/scsi_all.h>
47 
48 #include <dev/ic/aic6360var.h>
49 
50 #include <dev/pcmcia/pcmciareg.h>
51 #include <dev/pcmcia/pcmciavar.h>
52 #include <dev/pcmcia/pcmciadevs.h>
53 
54 int	aic_pcmcia_match __P((struct device *, struct cfdata *, void *));
55 void	aic_pcmcia_attach __P((struct device *, struct device *, void *));
56 int	aic_pcmcia_detach __P((struct device *, int));
57 
58 struct aic_pcmcia_softc {
59 	struct aic_softc sc_aic;		/* real "aic" softc */
60 
61 	/* PCMCIA-specific goo. */
62 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
63 	int sc_io_window;			/* our i/o window */
64 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
65 	void *sc_ih;				/* interrupt handler */
66 	int sc_flags;
67 #define AIC_PCMCIA_ATTACH	0x0001		/* attach is in progress */
68 };
69 
70 CFATTACH_DECL(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
71     aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, aic_activate);
72 
73 int	aic_pcmcia_enable __P((struct device *, int));
74 
75 const struct pcmcia_product aic_pcmcia_products[] = {
76 	{ PCMCIA_STR_ADAPTEC_APA1460,		PCMCIA_VENDOR_ADAPTEC,
77 	  PCMCIA_PRODUCT_ADAPTEC_APA1460,	0 },
78 
79 	{ PCMCIA_STR_ADAPTEC_APA1460A,		PCMCIA_VENDOR_ADAPTEC,
80 	  PCMCIA_PRODUCT_ADAPTEC_APA1460A,	0 },
81 
82 	{ PCMCIA_STR_NEWMEDIA_BUSTOASTER,	PCMCIA_VENDOR_NEWMEDIA,
83 	  PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,	0 },
84 
85 	{ NULL }
86 };
87 
88 int
89 aic_pcmcia_match(parent, match, aux)
90 	struct device *parent;
91 	struct cfdata *match;
92 	void *aux;
93 {
94 	struct pcmcia_attach_args *pa = aux;
95 
96         if (pcmcia_product_lookup(pa, aic_pcmcia_products,
97             sizeof aic_pcmcia_products[0], NULL) != NULL)
98 		return (1);
99 	return (0);
100 }
101 
102 void
103 aic_pcmcia_attach(parent, self, aux)
104 	struct device *parent, *self;
105 	void *aux;
106 {
107 	struct aic_pcmcia_softc *psc = (void *)self;
108 	struct aic_softc *sc = &psc->sc_aic;
109 	struct pcmcia_attach_args *pa = aux;
110 	struct pcmcia_config_entry *cfe;
111 	struct pcmcia_function *pf = pa->pf;
112 	const struct pcmcia_product *pp;
113 
114 	psc->sc_pf = pf;
115 
116 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
117 		if (cfe->num_memspace != 0 ||
118 		    cfe->num_iospace != 1)
119 			continue;
120 
121 		/*
122 		 * The bustoaster has a default config as first
123 		 * entry, we don't want to use that.
124 		 */
125 		if (pa->manufacturer == PCMCIA_VENDOR_NEWMEDIA &&
126 		    pa->product == PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER &&
127 		    cfe->iospace[0].start == 0)
128 			continue;
129 
130 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
131 		    cfe->iospace[0].length, 0, &psc->sc_pcioh) == 0)
132 			break;
133 	}
134 
135 	if (cfe == 0) {
136 		printf(": can't alloc i/o space\n");
137 		goto no_config_entry;
138 	}
139 
140 	sc->sc_iot = psc->sc_pcioh.iot;
141 	sc->sc_ioh = psc->sc_pcioh.ioh;
142 
143 	/* Enable the card. */
144 	pcmcia_function_init(pf, cfe);
145 	if (pcmcia_function_enable(pf)) {
146 		printf(": function enable failed\n");
147 		goto enable_failed;
148 	}
149 
150 	/* Map in the io space */
151 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
152 	    &psc->sc_pcioh, &psc->sc_io_window)) {
153 		printf(": can't map i/o space\n");
154 		goto iomap_failed;
155 	}
156 
157 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
158 		printf(": unable to detect chip!\n");
159 		goto no_aic_found;
160 	}
161 
162 	pp = pcmcia_product_lookup(pa, aic_pcmcia_products,
163 	    sizeof aic_pcmcia_products[0], NULL);
164 	if (pp == NULL) {
165 		printf("\n");
166 		panic("aic_pcmcia_attach: impossible");
167 	}
168 
169 	printf(": %s\n", pp->pp_name);
170 
171 	/* We can enable and disable the controller. */
172 	sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
173 
174 	psc->sc_flags |= AIC_PCMCIA_ATTACH;
175 	aicattach(sc);
176 	psc->sc_flags &= ~AIC_PCMCIA_ATTACH;
177 	return;
178 
179  no_aic_found:
180 	/* Unmap our i/o window. */
181 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
182 
183  iomap_failed:
184 	/* Disable the device. */
185 	pcmcia_function_disable(psc->sc_pf);
186 
187  enable_failed:
188 	/* Unmap our i/o space. */
189 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
190 
191  no_config_entry:
192 	psc->sc_io_window = -1;
193 }
194 
195 int
196 aic_pcmcia_detach(self, flags)
197 	struct device *self;
198 	int flags;
199 {
200 	struct aic_pcmcia_softc *sc = (struct aic_pcmcia_softc *)self;
201 	int error;
202 
203 	if (sc->sc_io_window == -1)
204 		/* Nothing to detach. */
205 		return (0);
206 
207 	if ((error = aic_detach(self, flags)) != 0)
208 		return (error);
209 
210 	/* Unmap our i/o window and i/o space. */
211 	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
212 	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
213 
214 	return (0);
215 }
216 int
217 aic_pcmcia_enable(self, onoff)
218 	struct device *self;
219 	int onoff;
220 {
221 	struct aic_pcmcia_softc *psc = (void *)self;
222 
223 	if (onoff) {
224 		/* Establish the interrupt handler. */
225 		psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
226 		    aicintr, &psc->sc_aic);
227 		if (psc->sc_ih == NULL) {
228 			printf("%s: couldn't establish interrupt handler\n",
229 			    psc->sc_aic.sc_dev.dv_xname);
230 			return (EIO);
231 		}
232 
233 		/*
234 		 * If attach is in progress, we know that card power is
235 		 * enabled and chip will be initialized later.
236 		 * Otherwise, enable and reset now.
237 		 */
238 		if ((psc->sc_flags & AIC_PCMCIA_ATTACH) == 0) {
239 			if (pcmcia_function_enable(psc->sc_pf)) {
240 				printf("%s: couldn't enable PCMCIA function\n",
241 				    psc->sc_aic.sc_dev.dv_xname);
242 				pcmcia_intr_disestablish(psc->sc_pf,
243 				    psc->sc_ih);
244 				return (EIO);
245 			}
246 
247 			/* Initialize only chip.  */
248 			aic_init(&psc->sc_aic, 0);
249 		}
250 	} else {
251 		pcmcia_function_disable(psc->sc_pf);
252 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
253 	}
254 
255 	return (0);
256 }
257