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