spc_pcmcia.c revision 1.4
1/*	$NetBSD: spc_pcmcia.c,v 1.4 2004/08/09 14:24:10 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.4 2004/08/09 14:24:10 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_io_handle sc_pcioh;	/* PCMCIA i/o space info */
65	int sc_io_window;			/* our i/o window */
66	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
67	void *sc_ih;				/* interrupt handler */
68	int sc_flags;
69#define SPC_PCMCIA_ATTACH	2		/* attach in progress */
70};
71
72int	spc_pcmcia_match __P((struct device *, struct cfdata *, void *));
73void	spc_pcmcia_attach __P((struct device *, struct device *, void *));
74int	spc_pcmcia_detach __P((struct device *, int));
75int	spc_pcmcia_enable __P((struct device *, int));
76
77CFATTACH_DECL(spc_pcmcia, sizeof(struct spc_pcmcia_softc),
78    spc_pcmcia_match, spc_pcmcia_attach, spc_pcmcia_detach, spc_activate);
79
80static const struct spc_pcmcia_product *
81    spc_pcmcia_lookup __P((struct pcmcia_attach_args *));
82
83const struct spc_pcmcia_product {
84	u_int32_t	epp_vendor;		/* vendor ID */
85	u_int32_t	epp_product;		/* product ID */
86	const char	*epp_cisinfo[4];	/* CIS information */
87} spc_pcmcia_products[] = {
88	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_SCSI600,
89	  PCMCIA_CIS_FUJITSU_SCSI600 },
90};
91
92static const struct spc_pcmcia_product *
93spc_pcmcia_lookup(pa)
94	struct pcmcia_attach_args *pa;
95{
96	const struct spc_pcmcia_product *epp;
97	int n;
98
99	for (epp = spc_pcmcia_products,
100	    n = sizeof(spc_pcmcia_products) / sizeof(spc_pcmcia_products[0]);
101	    n; epp++, n--) {
102		/* match by CIS information */
103		if (pa->card->cis1_info[0] != NULL &&
104		    epp->epp_cisinfo[0] != NULL &&
105		    strcmp(pa->card->cis1_info[0], epp->epp_cisinfo[0]) == 0 &&
106		    pa->card->cis1_info[1] != NULL &&
107		    epp->epp_cisinfo[1] != NULL &&
108		    strcmp(pa->card->cis1_info[1], epp->epp_cisinfo[1]) == 0)
109			return (epp);
110
111		/* match by vendor/product id */
112		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
113		    pa->manufacturer == epp->epp_vendor &&
114		    pa->product != PCMCIA_PRODUCT_INVALID &&
115		    pa->product == epp->epp_product)
116			return (epp);
117	}
118
119	return (NULL);
120}
121
122int
123spc_pcmcia_match(parent, match, aux)
124	struct device *parent;
125	struct cfdata *match;
126	void *aux;
127{
128	struct pcmcia_attach_args *pa = aux;
129
130	if (spc_pcmcia_lookup(pa) != NULL)
131		return (1);
132	return (0);
133}
134
135void
136spc_pcmcia_attach(parent, self, aux)
137	struct device *parent, *self;
138	void *aux;
139{
140	struct spc_pcmcia_softc *sc = (void *)self;
141	struct spc_softc *spc = (void *)self;
142	struct pcmcia_attach_args *pa = aux;
143	struct pcmcia_config_entry *cfe;
144	struct pcmcia_function *pf = pa->pf;
145	const struct spc_pcmcia_product *epp;
146
147	aprint_normal("\n");
148
149	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
150		if (cfe->num_memspace != 0 ||
151		    cfe->num_iospace != 1)
152			continue;
153
154		if (pcmcia_io_alloc(pf, cfe->iospace[0].start,
155		    cfe->iospace[0].length, cfe->iospace[0].length,
156		    &sc->sc_pcioh) == 0)
157			break;
158	}
159
160	if (cfe == 0) {
161		aprint_error("%s: can't alloc i/o space\n", self->dv_xname);
162		goto no_config_entry;
163	}
164
165	sc->sc_pf = pf;
166
167	/* Enable the card. */
168	pcmcia_function_init(pf, cfe);
169	if (pcmcia_function_enable(pf)) {
170		aprint_error("%s: function enable failed\n", self->dv_xname);
171		goto enable_failed;
172	}
173
174	/* Map in the I/O space */
175	if (pcmcia_io_map(pf, PCMCIA_WIDTH_AUTO, &sc->sc_pcioh,
176	    &sc->sc_io_window)) {
177		aprint_error("%s: can't map i/o space\n", self->dv_xname);
178		goto iomap_failed;
179	}
180
181	epp = spc_pcmcia_lookup(pa);
182	if (epp == NULL)
183		panic("spc_pcmcia_attach: impossible");
184
185	spc->sc_iot = sc->sc_pcioh.iot;
186	spc->sc_ioh = sc->sc_pcioh.ioh;
187	spc->sc_initiator = 7; /* XXX */
188	spc->sc_adapter.adapt_enable = spc_pcmcia_enable;
189
190	/*
191	 *  Initialize nca board itself.
192	 */
193	sc->sc_flags |= SPC_PCMCIA_ATTACH;
194	spc_attach(spc);
195	sc->sc_flags &= ~SPC_PCMCIA_ATTACH;
196	return;
197
198iomap_failed:
199	/* Disable the device. */
200	pcmcia_function_disable(pf);
201
202enable_failed:
203	/* Unmap our I/O space. */
204	pcmcia_io_free(pf, &sc->sc_pcioh);
205
206no_config_entry:
207	sc->sc_io_window = -1;
208}
209
210int
211spc_pcmcia_detach(self, flags)
212	struct device *self;
213	int flags;
214{
215	struct spc_pcmcia_softc *sc = (void *)self;
216	int error;
217
218	if (sc->sc_io_window == -1)
219                /* Nothing to detach. */
220                return (0);
221
222	error = spc_detach(self, flags);
223	if (error)
224		return (error);
225
226	/* Unmap our i/o window and i/o space. */
227	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
228	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
229
230	return (0);
231}
232
233int
234spc_pcmcia_enable(arg, onoff)
235	struct device *arg;
236	int onoff;
237{
238	struct spc_pcmcia_softc *sc = (void *) arg;
239
240	if (onoff) {
241		/* Establish the interrupt handler. */
242		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
243		    spc_intr, &sc->sc_spc);
244		if (sc->sc_ih == NULL) {
245			printf("%s: couldn't establish interrupt handler\n",
246			    sc->sc_spc.sc_dev.dv_xname);
247			return (EIO);
248		}
249
250		/*
251		 * If attach is in progress, we know that card power is
252		 * enabled and chip will be initialized later.
253		 * Otherwise, enable and reset now.
254		 */
255		if ((sc->sc_flags & SPC_PCMCIA_ATTACH) == 0) {
256			if (pcmcia_function_enable(sc->sc_pf)) {
257				printf("%s: couldn't enable PCMCIA function\n",
258				    sc->sc_spc.sc_dev.dv_xname);
259				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
260				return (EIO);
261			}
262
263			/* Initialize only chip.  */
264			spc_init(&sc->sc_spc, 0);
265		}
266	} else {
267		pcmcia_function_disable(sc->sc_pf);
268		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
269	}
270
271	return (0);
272}
273