1 /*	$NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $	*/
2 /*	$NetBSD$	*/
3 
4 /*-
5  * [Ported for FreeBSD]
6  *  Copyright (c) 2000
7  *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
8  *      All rights reserved.
9  * [NetBSD for NEC PC-98 series]
10  *  Copyright (c) 1998
11  *	NetBSD/pc98 porting staff. All rights reserved.
12  *
13  *  Redistribution and use in source and binary forms, with or without
14  *  modification, are permitted provided that the following conditions
15  *  are met:
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. The name of the author may not be used to endorse or promote products
22  *     derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: stable/9/sys/dev/nsp/nsp_pccard.c 194023 2009-06-11 17:14:28Z avg $");
39 
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/rman.h>
50 #include <compat/netbsd/dvcfg.h>
51 
52 #include <sys/device_port.h>
53 
54 #include <dev/pccard/pccardvar.h>
55 
56 #include <cam/scsi/scsi_low.h>
57 #include <cam/scsi/scsi_low_pisa.h>
58 
59 #include <dev/nsp/nspreg.h>
60 #include <dev/nsp/nspvar.h>
61 
62 #define	NSP_HOSTID	7
63 
64 #include "pccarddevs.h"
65 
66 #define	PIO_MODE 0x100		/* pd_flags */
67 
68 static int nspprobe(DEVPORT_PDEVICE devi);
69 static int nspattach(DEVPORT_PDEVICE devi);
70 
71 static	void	nsp_card_unload	(DEVPORT_PDEVICE);
72 
73 const struct pccard_product nsp_products[] = {
74   	PCMCIA_CARD(IODATA3, CBSC16),
75   	PCMCIA_CARD(PANASONIC, KME),
76 	PCMCIA_CARD(WORKBIT2, NINJA_SCSI3),
77 	PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16),
78   	{ NULL }
79 };
80 
81 /*
82  * Additional code for FreeBSD new-bus PC Card frontend
83  */
84 
85 static void
nsp_pccard_intr(void * arg)86 nsp_pccard_intr(void * arg)
87 {
88 	nspintr(arg);
89 }
90 
91 static void
nsp_release_resource(device_t dev)92 nsp_release_resource(device_t dev)
93 {
94 	struct nsp_softc	*sc = device_get_softc(dev);
95 
96 	if (sc->nsp_intrhand)
97 		bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
98 	if (sc->port_res)
99 		bus_release_resource(dev, SYS_RES_IOPORT,
100 				     sc->port_rid, sc->port_res);
101 	if (sc->irq_res)
102 		bus_release_resource(dev, SYS_RES_IRQ,
103 				     sc->irq_rid, sc->irq_res);
104 	if (sc->mem_res)
105 		bus_release_resource(dev, SYS_RES_MEMORY,
106 				     sc->mem_rid, sc->mem_res);
107 }
108 
109 static int
nsp_alloc_resource(device_t dev)110 nsp_alloc_resource(device_t dev)
111 {
112 	struct nsp_softc	*sc = device_get_softc(dev);
113 	u_long			ioaddr, iosize, maddr, msize;
114 	int			error;
115 
116 	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
117 	if (error || iosize < NSP_IOSIZE)
118 		return(ENOMEM);
119 
120 	sc->port_rid = 0;
121 	sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
122 					  0, ~0, NSP_IOSIZE, RF_ACTIVE);
123 	if (sc->port_res == NULL) {
124 		nsp_release_resource(dev);
125 		return(ENOMEM);
126 	}
127 
128 	sc->irq_rid = 0;
129 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
130 					     RF_ACTIVE);
131 	if (sc->irq_res == NULL) {
132 		nsp_release_resource(dev);
133 		return(ENOMEM);
134 	}
135 
136 	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
137 	if (error)
138 		return(0);	/* XXX */
139 
140 	/* No need to allocate memory if not configured and it's in PIO mode */
141 	if (maddr == 0 || msize == 0) {
142 		if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
143 			printf("Memory window was not configured. Configure or use in PIO mode.");
144 			nsp_release_resource(dev);
145 			return(ENOMEM);
146 		}
147 		/* no need to allocate memory if PIO mode */
148 		return(0);
149 	}
150 
151 	sc->mem_rid = 0;
152 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
153 					     RF_ACTIVE);
154 	if (sc->mem_res == NULL) {
155 		nsp_release_resource(dev);
156 		return(ENOMEM);
157 	}
158 
159 	return(0);
160 }
161 
162 static int
nsp_pccard_probe(device_t dev)163 nsp_pccard_probe(device_t dev)
164 {
165   	const struct pccard_product *pp;
166 
167 	if ((pp = pccard_product_lookup(dev, nsp_products,
168 	    sizeof(nsp_products[0]), NULL)) != NULL) {
169 		if (pp->pp_name)
170 			device_set_desc(dev, pp->pp_name);
171 		return(0);
172 	}
173 	return(EIO);
174 }
175 
176 static int
nsp_pccard_attach(device_t dev)177 nsp_pccard_attach(device_t dev)
178 {
179 	struct nsp_softc	*sc = device_get_softc(dev);
180 	int			error;
181 
182 	error = nsp_alloc_resource(dev);
183 	if (error)
184 		return(error);
185 	if (nspprobe(dev) == 0) {
186 		nsp_release_resource(dev);
187 		return(ENXIO);
188 	}
189 	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
190 			       NULL, nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
191 	if (error) {
192 		nsp_release_resource(dev);
193 		return(error);
194 	}
195 	if (nspattach(dev) == 0) {
196 		nsp_release_resource(dev);
197 		return(ENXIO);
198 	}
199 
200 	return(0);
201 }
202 
203 static int
nsp_pccard_detach(device_t dev)204 nsp_pccard_detach(device_t dev)
205 {
206 	nsp_card_unload(dev);
207 	nsp_release_resource(dev);
208 
209 	return (0);
210 }
211 
212 static device_method_t nsp_pccard_methods[] = {
213 	/* Device interface */
214 	DEVMETHOD(device_probe,		nsp_pccard_probe),
215 	DEVMETHOD(device_attach,	nsp_pccard_attach),
216 	DEVMETHOD(device_detach,	nsp_pccard_detach),
217 	{ 0, 0 }
218 };
219 
220 static driver_t nsp_pccard_driver = {
221 	"nsp",
222 	nsp_pccard_methods,
223 	sizeof(struct nsp_softc),
224 };
225 
226 static devclass_t nsp_devclass;
227 
228 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
229 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
230 
231 static void
nsp_card_unload(DEVPORT_PDEVICE devi)232 nsp_card_unload(DEVPORT_PDEVICE devi)
233 {
234 	struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
235 	intrmask_t s;
236 
237 	s = splcam();
238 	scsi_low_deactivate((struct scsi_low_softc *)sc);
239         scsi_low_dettach(&sc->sc_sclow);
240 	splx(s);
241 }
242 
243 static	int
nspprobe(DEVPORT_PDEVICE devi)244 nspprobe(DEVPORT_PDEVICE devi)
245 {
246 	int rv;
247 	struct nsp_softc *sc = device_get_softc(devi);
248 
249 	rv = nspprobesubr(rman_get_bustag(sc->port_res),
250 			  rman_get_bushandle(sc->port_res),
251 			  DEVPORT_PDEVFLAGS(devi));
252 
253 	return rv;
254 }
255 
256 static	int
nspattach(DEVPORT_PDEVICE devi)257 nspattach(DEVPORT_PDEVICE devi)
258 {
259 	struct nsp_softc *sc;
260 	struct scsi_low_softc *slp;
261 	u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
262 	u_int	iobase = DEVPORT_PDEVIOBASE(devi);
263 	intrmask_t s;
264 	char	dvname[16];
265 
266 	strcpy(dvname,"nsp");
267 
268 	if (iobase == 0) {
269 		printf("%s: no ioaddr is given\n", dvname);
270 		return (0);
271 	}
272 
273 	sc = DEVPORT_PDEVALLOC_SOFTC(devi);
274 	if (sc == NULL)
275 		return (0);
276 
277 	slp = &sc->sc_sclow;
278 	slp->sl_dev = devi;
279 	sc->sc_iot = rman_get_bustag(sc->port_res);
280 	sc->sc_ioh = rman_get_bushandle(sc->port_res);
281 
282 	if (sc->mem_res == NULL) {
283 		printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode");
284 		flags |= PIO_MODE;
285 	}
286 
287 	if ((flags & PIO_MODE) == 0) {
288 		sc->sc_memt = rman_get_bustag(sc->mem_res);
289 		sc->sc_memh = rman_get_bushandle(sc->mem_res);
290 	} else {
291 		sc->sc_memh = 0;
292 	}
293 	/* slp->sl_irq = devi->pd_irq; */
294 	sc->sc_iclkdiv = CLKDIVR_20M;
295 	sc->sc_clkdiv = CLKDIVR_40M;
296 
297 	slp->sl_hostid = NSP_HOSTID;
298 	slp->sl_cfgflags = flags;
299 
300 	s = splcam();
301 	nspattachsubr(sc);
302 	splx(s);
303 
304 	return(NSP_IOSIZE);
305 }
306