xref: /netbsd/sys/dev/pcmcia/aic_pcmcia.c (revision 6550d01e)
1 /*	$NetBSD: aic_pcmcia.c,v 1.43 2009/11/12 19:24:06 dyoung 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.43 2009/11/12 19:24:06 dyoung 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 <sys/cpu.h>
41 #include <sys/bus.h>
42 #include <sys/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 struct aic_pcmcia_softc {
55 	struct aic_softc sc_aic;		/* real "aic" softc */
56 
57 	/* PCMCIA-specific goo. */
58 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
59 	void *sc_ih;				/* interrupt handler */
60 
61 	int sc_state;
62 #define	AIC_PCMCIA_ATTACHED	3
63 };
64 
65 static int	aic_pcmcia_match(device_t, cfdata_t, void *);
66 static int	aic_pcmcia_validate_config(struct pcmcia_config_entry *);
67 static void	aic_pcmcia_attach(device_t, device_t, void *);
68 static int	aic_pcmcia_detach(device_t, int);
69 static int	aic_pcmcia_enable(device_t, int);
70 
71 CFATTACH_DECL_NEW(aic_pcmcia, sizeof(struct aic_pcmcia_softc),
72     aic_pcmcia_match, aic_pcmcia_attach, aic_pcmcia_detach, NULL);
73 
74 static const struct pcmcia_product aic_pcmcia_products[] = {
75 	{ PCMCIA_VENDOR_ADAPTEC, PCMCIA_PRODUCT_ADAPTEC_APA1460,
76 	  PCMCIA_CIS_INVALID },
77 
78 	{ PCMCIA_VENDOR_ADAPTEC, PCMCIA_PRODUCT_ADAPTEC_APA1460A,
79 	  PCMCIA_CIS_INVALID },
80 
81 	{ PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BUSTOASTER,
82 	  PCMCIA_CIS_INVALID },
83 };
84 static const size_t aic_pcmcia_nproducts = __arraycount(aic_pcmcia_products);
85 
86 int
87 aic_pcmcia_match(device_t parent, cfdata_t match, void *aux)
88 {
89 	struct pcmcia_attach_args *pa = aux;
90 
91 	if (pcmcia_product_lookup(pa, aic_pcmcia_products, aic_pcmcia_nproducts,
92 	    sizeof(aic_pcmcia_products[0]), NULL))
93 		return (1);
94 	return (0);
95 }
96 
97 int
98 aic_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
99 {
100 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
101 	    cfe->num_memspace != 0 ||
102 	    cfe->num_iospace != 1)
103 		return (EINVAL);
104 	return (0);
105 }
106 
107 void
108 aic_pcmcia_attach(device_t parent, device_t self, void *aux)
109 {
110 	struct aic_pcmcia_softc *psc = device_private(self);
111 	struct aic_softc *sc = &psc->sc_aic;
112 	struct pcmcia_attach_args *pa = aux;
113 	struct pcmcia_config_entry *cfe;
114 	struct pcmcia_function *pf = pa->pf;
115 	int error;
116 
117 	sc->sc_dev = self;
118 	psc->sc_pf = pf;
119 
120 	error = pcmcia_function_configure(pf, aic_pcmcia_validate_config);
121 	if (error) {
122 		aprint_error_dev(self, "configure failed, error=%d\n", error);
123 		return;
124 	}
125 
126 	cfe = pf->cfe;
127 	sc->sc_iot = cfe->iospace[0].handle.iot;
128 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
129 
130 	error = aic_pcmcia_enable(self, 1);
131 	if (error)
132 		goto fail;
133 
134 	if (!aic_find(sc->sc_iot, sc->sc_ioh)) {
135 		aprint_error_dev(self, "unable to detect chip!\n");
136 		goto fail2;
137 	}
138 
139 	/* We can enable and disable the controller. */
140 	sc->sc_adapter.adapt_enable = aic_pcmcia_enable;
141 	sc->sc_adapter.adapt_refcnt = 1;
142 
143 	aicattach(sc);
144 	scsipi_adapter_delref(&sc->sc_adapter);
145 	psc->sc_state = AIC_PCMCIA_ATTACHED;
146 	return;
147 
148 fail2:
149 	aic_pcmcia_enable(self, 0);
150 fail:
151 	pcmcia_function_unconfigure(pf);
152 }
153 
154 int
155 aic_pcmcia_detach(device_t self, int flags)
156 {
157 	struct aic_pcmcia_softc *psc = device_private(self);
158 	int error;
159 
160 	if (psc->sc_state != AIC_PCMCIA_ATTACHED)
161 		return (0);
162 
163 	error = aic_detach(self, flags);
164 	if (error)
165 		return (error);
166 
167 	pcmcia_function_unconfigure(psc->sc_pf);
168 
169 	return (0);
170 }
171 
172 int
173 aic_pcmcia_enable(device_t self, int onoff)
174 {
175 	struct aic_pcmcia_softc *psc = device_private(self);
176 	struct aic_softc *sc = &psc->sc_aic;
177 	int error;
178 
179 	if (onoff) {
180 		/* Establish the interrupt handler. */
181 		psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_BIO,
182 		    aicintr, sc);
183 		if (!psc->sc_ih)
184 			return (EIO);
185 
186 		error = pcmcia_function_enable(psc->sc_pf);
187 		if (error) {
188 			pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
189 			psc->sc_ih = 0;
190 			return (error);
191 		}
192 
193 		/* Initialize only chip.  */
194 		aic_init(sc, 0);
195 	} else {
196 		pcmcia_function_disable(psc->sc_pf);
197 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
198 		psc->sc_ih = 0;
199 	}
200 
201 	return (0);
202 }
203