spc_pcmcia.c revision 1.19
1/*	$NetBSD: spc_pcmcia.c,v 1.19 2008/03/31 15:20:47 tsutsui 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.19 2008/03/31 15:20:47 tsutsui 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 <sys/bus.h>
48#include <sys/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_ATTACHED	3
69};
70
71static int	spc_pcmcia_match(device_t, cfdata_t, void *);
72static int	spc_pcmcia_validate_config(struct pcmcia_config_entry *);
73static void	spc_pcmcia_attach(device_t, device_t, void *);
74static int	spc_pcmcia_detach(device_t, int);
75static int	spc_pcmcia_enable(device_t, int);
76
77CFATTACH_DECL_NEW(spc_pcmcia, sizeof(struct spc_pcmcia_softc),
78    spc_pcmcia_match, spc_pcmcia_attach, spc_pcmcia_detach, spc_activate);
79
80static const struct pcmcia_product spc_pcmcia_products[] = {
81	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_SCSI600,
82	  PCMCIA_CIS_FUJITSU_SCSI600 },
83};
84static const size_t spc_pcmcia_nproducts = __arraycount(spc_pcmcia_products);
85
86int
87spc_pcmcia_match(device_t parent, cfdata_t cf, void *aux)
88{
89	struct pcmcia_attach_args *pa = aux;
90
91	if (pcmcia_product_lookup(pa, spc_pcmcia_products, spc_pcmcia_nproducts,
92	    sizeof(spc_pcmcia_products[0]), NULL))
93		return 1;
94	return 0;
95}
96
97int
98spc_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
99{
100
101	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
102	    cfe->num_memspace != 0 ||
103	    cfe->num_iospace != 1)
104		return EINVAL;
105	return 0;
106}
107
108void
109spc_pcmcia_attach(device_t parent, device_t self, void *aux)
110{
111	struct spc_pcmcia_softc *sc = device_private(self);
112	struct spc_softc *spc = &sc->sc_spc;
113	struct pcmcia_attach_args *pa = aux;
114	struct pcmcia_config_entry *cfe;
115	struct pcmcia_function *pf = pa->pf;
116	int error;
117
118	spc->sc_dev = self;
119	sc->sc_pf = pf;
120
121	error = pcmcia_function_configure(pf, spc_pcmcia_validate_config);
122	if (error) {
123		aprint_error_dev(self, "configure failed, error=%d\n", error);
124		return;
125	}
126
127	cfe = pf->cfe;
128	spc->sc_iot = cfe->iospace[0].handle.iot;
129	spc->sc_ioh = cfe->iospace[0].handle.ioh;
130
131	error = spc_pcmcia_enable(self, 1);
132	if (error)
133		goto fail;
134
135	spc->sc_initiator = 7; /* XXX */
136	spc->sc_adapter.adapt_enable = spc_pcmcia_enable;
137	spc->sc_adapter.adapt_refcnt = 1;
138
139	/*
140	 *  Initialize nca board itself.
141	 */
142	spc_attach(spc);
143	scsipi_adapter_delref(&spc->sc_adapter);
144	sc->sc_state = SPC_PCMCIA_ATTACHED;
145	return;
146
147 fail:
148	pcmcia_function_unconfigure(pf);
149}
150
151int
152spc_pcmcia_detach(device_t self, int flags)
153{
154	struct spc_pcmcia_softc *sc = device_private(self);
155	int error;
156
157	if (sc->sc_state != SPC_PCMCIA_ATTACHED)
158		return 0;
159
160	error = spc_detach(self, flags);
161	if (error)
162		return error;
163
164	pcmcia_function_unconfigure(sc->sc_pf);
165
166	return 0;
167}
168
169int
170spc_pcmcia_enable(device_t self, int onoff)
171{
172	struct spc_pcmcia_softc *sc = device_private(self);
173	int error;
174
175	if (onoff) {
176		/* Establish the interrupt handler. */
177		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
178		    spc_intr, &sc->sc_spc);
179		if (!sc->sc_ih)
180			return EIO;
181
182		error = pcmcia_function_enable(sc->sc_pf);
183		if (error) {
184			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
185			sc->sc_ih = 0;
186			return error;
187		}
188
189		/* Initialize only chip.  */
190		spc_init(&sc->sc_spc, 0);
191	} else {
192		pcmcia_function_disable(sc->sc_pf);
193		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
194		sc->sc_ih = 0;
195	}
196
197	return 0;
198}
199