spc_pcmcia.c revision 1.9
1/*	$NetBSD: spc_pcmcia.c,v 1.9 2004/08/10 16:04:16 mycroft 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: spc_pcmcia.c,v 1.9 2004/08/10 16:04:16 mycroft 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/mb89352var.h>
59
60struct spc_pcmcia_softc {
61	struct spc_softc	sc_spc;		/* glue to MI code */
62
63	/* PCMCIA-specific goo. */
64	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
65	void *sc_ih;				/* interrupt handler */
66
67	int sc_state;
68#define SPC_PCMCIA_ATTACH1	1
69#define	SPC_PCMCIA_ATTACH2	2
70#define	SPC_PCMCIA_ATTACHED	3
71};
72
73int	spc_pcmcia_match __P((struct device *, struct cfdata *, void *));
74int	spc_pcmcia_validate_config __P((struct pcmcia_config_entry *));
75void	spc_pcmcia_attach __P((struct device *, struct device *, void *));
76int	spc_pcmcia_detach __P((struct device *, int));
77int	spc_pcmcia_enable __P((struct device *, int));
78
79CFATTACH_DECL(spc_pcmcia, sizeof(struct spc_pcmcia_softc),
80    spc_pcmcia_match, spc_pcmcia_attach, spc_pcmcia_detach, spc_activate);
81
82static const struct spc_pcmcia_product *
83    spc_pcmcia_lookup __P((struct pcmcia_attach_args *));
84
85const struct spc_pcmcia_product {
86	u_int32_t	epp_vendor;		/* vendor ID */
87	u_int32_t	epp_product;		/* product ID */
88	const char	*epp_cisinfo[4];	/* CIS information */
89} spc_pcmcia_products[] = {
90	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_SCSI600,
91	  PCMCIA_CIS_FUJITSU_SCSI600 },
92};
93
94static const struct spc_pcmcia_product *
95spc_pcmcia_lookup(pa)
96	struct pcmcia_attach_args *pa;
97{
98	const struct spc_pcmcia_product *epp;
99	int n;
100
101	for (epp = spc_pcmcia_products,
102	    n = sizeof(spc_pcmcia_products) / sizeof(spc_pcmcia_products[0]);
103	    n; epp++, n--) {
104		/* match by CIS information */
105		if (pa->card->cis1_info[0] != NULL &&
106		    epp->epp_cisinfo[0] != NULL &&
107		    strcmp(pa->card->cis1_info[0], epp->epp_cisinfo[0]) == 0 &&
108		    pa->card->cis1_info[1] != NULL &&
109		    epp->epp_cisinfo[1] != NULL &&
110		    strcmp(pa->card->cis1_info[1], epp->epp_cisinfo[1]) == 0)
111			return (epp);
112
113		/* match by vendor/product id */
114		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
115		    pa->manufacturer == epp->epp_vendor &&
116		    pa->product != PCMCIA_PRODUCT_INVALID &&
117		    pa->product == epp->epp_product)
118			return (epp);
119	}
120
121	return (NULL);
122}
123
124int
125spc_pcmcia_match(parent, match, aux)
126	struct device *parent;
127	struct cfdata *match;
128	void *aux;
129{
130	struct pcmcia_attach_args *pa = aux;
131
132	if (spc_pcmcia_lookup(pa) != NULL)
133		return (1);
134	return (0);
135}
136
137int
138spc_pcmcia_validate_config(cfe)
139	struct pcmcia_config_entry *cfe;
140{
141	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
142	    cfe->num_memspace != 0 ||
143	    cfe->num_iospace != 1)
144		return (EINVAL);
145	return (0);
146}
147
148void
149spc_pcmcia_attach(parent, self, aux)
150	struct device *parent, *self;
151	void *aux;
152{
153	struct spc_pcmcia_softc *sc = (void *)self;
154	struct spc_softc *spc = (void *)self;
155	struct pcmcia_attach_args *pa = aux;
156	struct pcmcia_config_entry *cfe;
157	struct pcmcia_function *pf = pa->pf;
158	int error;
159
160	aprint_normal("\n");
161	sc->sc_pf = pf;
162
163	error = pcmcia_function_configure(pf, spc_pcmcia_validate_config);
164	if (error) {
165		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
166		    error);
167		return;
168	}
169
170	cfe = pf->cfe;
171	spc->sc_iot = cfe->iospace[0].handle.iot;
172	spc->sc_ioh = cfe->iospace[0].handle.ioh;
173
174	error = spc_pcmcia_enable(self, 1);
175	if (error)
176		goto fail;
177
178	spc->sc_initiator = 7; /* XXX */
179	spc->sc_adapter.adapt_enable = spc_pcmcia_enable;
180
181	/*
182	 *  Initialize nca board itself.
183	 */
184	sc->sc_state = SPC_PCMCIA_ATTACH1;
185	spc_attach(spc);
186	if (sc->sc_state == SPC_PCMCIA_ATTACH1)
187		spc_pcmcia_enable(self, 0);
188	sc->sc_state = SPC_PCMCIA_ATTACHED;
189	return;
190
191fail:
192	pcmcia_function_unconfigure(pf);
193}
194
195int
196spc_pcmcia_detach(self, flags)
197	struct device *self;
198	int flags;
199{
200	struct spc_pcmcia_softc *sc = (void *)self;
201	int error;
202
203	if (sc->sc_state != SPC_PCMCIA_ATTACHED)
204		return (0);
205
206	error = spc_detach(self, flags);
207	if (error)
208		return (error);
209
210	pcmcia_function_unconfigure(sc->sc_pf);
211
212	return (0);
213}
214
215int
216spc_pcmcia_enable(arg, onoff)
217	struct device *arg;
218	int onoff;
219{
220	struct spc_pcmcia_softc *sc = (void *)arg;
221	int error;
222
223	if (onoff) {
224		/*
225		 * If attach is in progress, we already have the device
226		 * powered up.
227		 */
228		if (sc->sc_state == SPC_PCMCIA_ATTACH1) {
229			sc->sc_state = SPC_PCMCIA_ATTACH2;
230		} else {
231			/* Establish the interrupt handler. */
232			sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
233			    spc_intr, &sc->sc_spc);
234			if (!sc->sc_ih)
235				return (EIO);
236
237			error = pcmcia_function_enable(sc->sc_pf);
238			if (error) {
239				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
240				sc->sc_ih = 0;
241				return (error);
242			}
243
244			/* Initialize only chip.  */
245			spc_init(&sc->sc_spc, 0);
246		}
247	} else {
248		pcmcia_function_disable(sc->sc_pf);
249		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
250		sc->sc_ih = 0;
251	}
252
253	return (0);
254}
255