xref: /netbsd-src/sys/dev/pcmcia/spc_pcmcia.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: spc_pcmcia.c,v 1.20 2008/04/28 20:23:56 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: spc_pcmcia.c,v 1.20 2008/04/28 20:23:56 martin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/buf.h>
39 
40 #include <sys/bus.h>
41 #include <sys/intr.h>
42 
43 #include <dev/scsipi/scsi_all.h>
44 #include <dev/scsipi/scsipi_all.h>
45 #include <dev/scsipi/scsiconf.h>
46 
47 #include <dev/pcmcia/pcmciareg.h>
48 #include <dev/pcmcia/pcmciavar.h>
49 #include <dev/pcmcia/pcmciadevs.h>
50 
51 #include <dev/ic/mb89352var.h>
52 
53 struct spc_pcmcia_softc {
54 	struct spc_softc	sc_spc;		/* glue to MI code */
55 
56 	/* PCMCIA-specific goo. */
57 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
58 	void *sc_ih;				/* interrupt handler */
59 
60 	int sc_state;
61 #define	SPC_PCMCIA_ATTACHED	3
62 };
63 
64 static int	spc_pcmcia_match(device_t, cfdata_t, void *);
65 static int	spc_pcmcia_validate_config(struct pcmcia_config_entry *);
66 static void	spc_pcmcia_attach(device_t, device_t, void *);
67 static int	spc_pcmcia_detach(device_t, int);
68 static int	spc_pcmcia_enable(device_t, int);
69 
70 CFATTACH_DECL_NEW(spc_pcmcia, sizeof(struct spc_pcmcia_softc),
71     spc_pcmcia_match, spc_pcmcia_attach, spc_pcmcia_detach, spc_activate);
72 
73 static const struct pcmcia_product spc_pcmcia_products[] = {
74 	{ PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_SCSI600,
75 	  PCMCIA_CIS_FUJITSU_SCSI600 },
76 };
77 static const size_t spc_pcmcia_nproducts = __arraycount(spc_pcmcia_products);
78 
79 int
80 spc_pcmcia_match(device_t parent, cfdata_t cf, void *aux)
81 {
82 	struct pcmcia_attach_args *pa = aux;
83 
84 	if (pcmcia_product_lookup(pa, spc_pcmcia_products, spc_pcmcia_nproducts,
85 	    sizeof(spc_pcmcia_products[0]), NULL))
86 		return 1;
87 	return 0;
88 }
89 
90 int
91 spc_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
92 {
93 
94 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
95 	    cfe->num_memspace != 0 ||
96 	    cfe->num_iospace != 1)
97 		return EINVAL;
98 	return 0;
99 }
100 
101 void
102 spc_pcmcia_attach(device_t parent, device_t self, void *aux)
103 {
104 	struct spc_pcmcia_softc *sc = device_private(self);
105 	struct spc_softc *spc = &sc->sc_spc;
106 	struct pcmcia_attach_args *pa = aux;
107 	struct pcmcia_config_entry *cfe;
108 	struct pcmcia_function *pf = pa->pf;
109 	int error;
110 
111 	spc->sc_dev = self;
112 	sc->sc_pf = pf;
113 
114 	error = pcmcia_function_configure(pf, spc_pcmcia_validate_config);
115 	if (error) {
116 		aprint_error_dev(self, "configure failed, error=%d\n", error);
117 		return;
118 	}
119 
120 	cfe = pf->cfe;
121 	spc->sc_iot = cfe->iospace[0].handle.iot;
122 	spc->sc_ioh = cfe->iospace[0].handle.ioh;
123 
124 	error = spc_pcmcia_enable(self, 1);
125 	if (error)
126 		goto fail;
127 
128 	spc->sc_initiator = 7; /* XXX */
129 	spc->sc_adapter.adapt_enable = spc_pcmcia_enable;
130 	spc->sc_adapter.adapt_refcnt = 1;
131 
132 	/*
133 	 *  Initialize nca board itself.
134 	 */
135 	spc_attach(spc);
136 	scsipi_adapter_delref(&spc->sc_adapter);
137 	sc->sc_state = SPC_PCMCIA_ATTACHED;
138 	return;
139 
140  fail:
141 	pcmcia_function_unconfigure(pf);
142 }
143 
144 int
145 spc_pcmcia_detach(device_t self, int flags)
146 {
147 	struct spc_pcmcia_softc *sc = device_private(self);
148 	int error;
149 
150 	if (sc->sc_state != SPC_PCMCIA_ATTACHED)
151 		return 0;
152 
153 	error = spc_detach(self, flags);
154 	if (error)
155 		return error;
156 
157 	pcmcia_function_unconfigure(sc->sc_pf);
158 
159 	return 0;
160 }
161 
162 int
163 spc_pcmcia_enable(device_t self, int onoff)
164 {
165 	struct spc_pcmcia_softc *sc = device_private(self);
166 	int error;
167 
168 	if (onoff) {
169 		/* Establish the interrupt handler. */
170 		sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_BIO,
171 		    spc_intr, &sc->sc_spc);
172 		if (!sc->sc_ih)
173 			return EIO;
174 
175 		error = pcmcia_function_enable(sc->sc_pf);
176 		if (error) {
177 			pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
178 			sc->sc_ih = 0;
179 			return error;
180 		}
181 
182 		/* Initialize only chip.  */
183 		spc_init(&sc->sc_spc, 0);
184 	} else {
185 		pcmcia_function_disable(sc->sc_pf);
186 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
187 		sc->sc_ih = 0;
188 	}
189 
190 	return 0;
191 }
192