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