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