spc_pcmcia.c revision 1.3
1/*	$NetBSD: spc_pcmcia.c,v 1.3 2004/08/09 14:07:57 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.3 2004/08/09 14:07:57 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, 0, &sc->sc_pcioh) == 0)
156			break;
157	}
158
159	if (cfe == 0) {
160		aprint_error("%s: can't alloc i/o space\n", self->dv_xname);
161		goto no_config_entry;
162	}
163
164	sc->sc_pf = pf;
165
166	/* Enable the card. */
167	pcmcia_function_init(pf, cfe);
168	if (pcmcia_function_enable(pf)) {
169		aprint_error("%s: function enable failed\n", self->dv_xname);
170		goto enable_failed;
171	}
172
173	/* Map in the I/O space */
174	if (pcmcia_io_map(pf, PCMCIA_WIDTH_AUTO, &sc->sc_pcioh,
175	    &sc->sc_io_window)) {
176		aprint_error("%s: can't map i/o space\n", self->dv_xname);
177		goto iomap_failed;
178	}
179
180	epp = spc_pcmcia_lookup(pa);
181	if (epp == NULL)
182		panic("spc_pcmcia_attach: impossible");
183
184	spc->sc_iot = sc->sc_pcioh.iot;
185	spc->sc_ioh = sc->sc_pcioh.ioh;
186	spc->sc_initiator = 7; /* XXX */
187	spc->sc_adapter.adapt_enable = spc_pcmcia_enable;
188
189	/*
190	 *  Initialize nca board itself.
191	 */
192	sc->sc_flags |= SPC_PCMCIA_ATTACH;
193	spc_attach(spc);
194	sc->sc_flags &= ~SPC_PCMCIA_ATTACH;
195	return;
196
197iomap_failed:
198	/* Disable the device. */
199	pcmcia_function_disable(pf);
200
201enable_failed:
202	/* Unmap our I/O space. */
203	pcmcia_io_free(pf, &sc->sc_pcioh);
204
205no_config_entry:
206	sc->sc_io_window = -1;
207}
208
209int
210spc_pcmcia_detach(self, flags)
211	struct device *self;
212	int flags;
213{
214	struct spc_pcmcia_softc *sc = (void *)self;
215	int error;
216
217	if (sc->sc_io_window == -1)
218                /* Nothing to detach. */
219                return (0);
220
221	error = spc_detach(self, flags);
222	if (error)
223		return (error);
224
225	/* Unmap our i/o window and i/o space. */
226	pcmcia_io_unmap(sc->sc_pf, sc->sc_io_window);
227	pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
228
229	return (0);
230}
231
232int
233spc_pcmcia_enable(arg, onoff)
234	struct device *arg;
235	int onoff;
236{
237	struct spc_pcmcia_softc *sc = (void *) arg;
238
239	if (onoff) {
240		/* Establish the interrupt handler. */
241		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
242		    spc_intr, &sc->sc_spc);
243		if (sc->sc_ih == NULL) {
244			printf("%s: couldn't establish interrupt handler\n",
245			    sc->sc_spc.sc_dev.dv_xname);
246			return (EIO);
247		}
248
249		/*
250		 * If attach is in progress, we know that card power is
251		 * enabled and chip will be initialized later.
252		 * Otherwise, enable and reset now.
253		 */
254		if ((sc->sc_flags & SPC_PCMCIA_ATTACH) == 0) {
255			if (pcmcia_function_enable(sc->sc_pf)) {
256				printf("%s: couldn't enable PCMCIA function\n",
257				    sc->sc_spc.sc_dev.dv_xname);
258				pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
259				return (EIO);
260			}
261
262			/* Initialize only chip.  */
263			spc_init(&sc->sc_spc, 0);
264		}
265	} else {
266		pcmcia_function_disable(sc->sc_pf);
267		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
268	}
269
270	return (0);
271}
272