nsp_pccard.c revision 331722
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/11/sys/dev/nsp/nsp_pccard.c 331722 2018-03-29 02:50:57Z eadler $");
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/module.h>
46#include <sys/systm.h>
47
48#include <machine/bus.h>
49#include <machine/resource.h>
50#include <sys/rman.h>
51
52#include <sys/bus.h>
53
54#include <dev/pccard/pccardvar.h>
55
56#include <cam/scsi/scsi_low.h>
57
58#include <dev/nsp/nspreg.h>
59#include <dev/nsp/nspvar.h>
60
61#define	NSP_HOSTID	7
62
63#include "pccarddevs.h"
64
65#define	PIO_MODE 0x100		/* pd_flags */
66
67static int nspprobe(device_t devi);
68static int nspattach(device_t devi);
69
70static	void	nsp_card_unload	(device_t);
71
72const struct pccard_product nsp_products[] = {
73  	PCMCIA_CARD(IODATA3, CBSC16),
74  	PCMCIA_CARD(PANASONIC, KME),
75	PCMCIA_CARD(WORKBIT2, NINJA_SCSI3),
76	PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16),
77  	{ NULL }
78};
79
80/*
81 * Additional code for FreeBSD new-bus PC Card frontend
82 */
83
84static void
85nsp_pccard_intr(void * arg)
86{
87	struct nsp_softc *sc;
88
89	sc = arg;
90	SCSI_LOW_LOCK(&sc->sc_sclow);
91	nspintr(sc);
92	SCSI_LOW_UNLOCK(&sc->sc_sclow);
93}
94
95static void
96nsp_release_resource(device_t dev)
97{
98	struct nsp_softc	*sc = device_get_softc(dev);
99
100	if (sc->nsp_intrhand)
101		bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
102	if (sc->port_res)
103		bus_release_resource(dev, SYS_RES_IOPORT,
104				     sc->port_rid, sc->port_res);
105	if (sc->irq_res)
106		bus_release_resource(dev, SYS_RES_IRQ,
107				     sc->irq_rid, sc->irq_res);
108	if (sc->mem_res)
109		bus_release_resource(dev, SYS_RES_MEMORY,
110				     sc->mem_rid, sc->mem_res);
111	mtx_destroy(&sc->sc_sclow.sl_lock);
112}
113
114static int
115nsp_alloc_resource(device_t dev)
116{
117	struct nsp_softc	*sc = device_get_softc(dev);
118	rman_res_t		ioaddr, iosize, maddr, msize;
119	int			error;
120
121	error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
122	if (error || iosize < NSP_IOSIZE)
123		return(ENOMEM);
124
125	mtx_init(&sc->sc_sclow.sl_lock, "nsp", NULL, MTX_DEF);
126	sc->port_rid = 0;
127	sc->port_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
128						   &sc->port_rid, NSP_IOSIZE,
129						   RF_ACTIVE);
130	if (sc->port_res == NULL) {
131		nsp_release_resource(dev);
132		return(ENOMEM);
133	}
134
135	sc->irq_rid = 0;
136	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
137					     RF_ACTIVE);
138	if (sc->irq_res == NULL) {
139		nsp_release_resource(dev);
140		return(ENOMEM);
141	}
142
143	error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
144	if (error)
145		return(0);	/* XXX */
146
147	/* No need to allocate memory if not configured and it's in PIO mode */
148	if (maddr == 0 || msize == 0) {
149		if ((device_get_flags(dev) & PIO_MODE) == 0) {
150			printf("Memory window was not configured. Configure or use in PIO mode.");
151			nsp_release_resource(dev);
152			return(ENOMEM);
153		}
154		/* no need to allocate memory if PIO mode */
155		return(0);
156	}
157
158	sc->mem_rid = 0;
159	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
160					     RF_ACTIVE);
161	if (sc->mem_res == NULL) {
162		nsp_release_resource(dev);
163		return(ENOMEM);
164	}
165
166	return(0);
167}
168
169static int
170nsp_pccard_probe(device_t dev)
171{
172  	const struct pccard_product *pp;
173
174	if ((pp = pccard_product_lookup(dev, nsp_products,
175	    sizeof(nsp_products[0]), NULL)) != NULL) {
176		if (pp->pp_name)
177			device_set_desc(dev, pp->pp_name);
178		return (BUS_PROBE_DEFAULT);
179	}
180	return(EIO);
181}
182
183static int
184nsp_pccard_attach(device_t dev)
185{
186	struct nsp_softc	*sc = device_get_softc(dev);
187	int			error;
188
189	error = nsp_alloc_resource(dev);
190	if (error)
191		return(error);
192	if (nspprobe(dev) == 0) {
193		nsp_release_resource(dev);
194		return(ENXIO);
195	}
196	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY |
197	    INTR_MPSAFE, NULL, nsp_pccard_intr, sc, &sc->nsp_intrhand);
198	if (error) {
199		nsp_release_resource(dev);
200		return(error);
201	}
202	if (nspattach(dev) == 0) {
203		nsp_release_resource(dev);
204		return(ENXIO);
205	}
206
207	return(0);
208}
209
210static int
211nsp_pccard_detach(device_t dev)
212{
213	nsp_card_unload(dev);
214	nsp_release_resource(dev);
215
216	return (0);
217}
218
219static device_method_t nsp_pccard_methods[] = {
220	/* Device interface */
221	DEVMETHOD(device_probe,		nsp_pccard_probe),
222	DEVMETHOD(device_attach,	nsp_pccard_attach),
223	DEVMETHOD(device_detach,	nsp_pccard_detach),
224	{ 0, 0 }
225};
226
227static driver_t nsp_pccard_driver = {
228	"nsp",
229	nsp_pccard_methods,
230	sizeof(struct nsp_softc),
231};
232
233static devclass_t nsp_devclass;
234
235MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
236DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
237PCCARD_PNP_INFO(nsp_products);
238
239static void
240nsp_card_unload(device_t devi)
241{
242	struct nsp_softc *sc = device_get_softc(devi);
243
244	scsi_low_deactivate(&sc->sc_sclow);
245        scsi_low_detach(&sc->sc_sclow);
246}
247
248static	int
249nspprobe(device_t devi)
250{
251	int rv;
252	struct nsp_softc *sc = device_get_softc(devi);
253
254	rv = nspprobesubr(sc->port_res,
255			  device_get_flags(devi));
256
257	return rv;
258}
259
260static	int
261nspattach(device_t devi)
262{
263	struct nsp_softc *sc;
264	struct scsi_low_softc *slp;
265	u_int32_t flags = device_get_flags(devi);
266	u_int	iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
267
268	if (iobase == 0) {
269		device_printf(devi, "no ioaddr is given\n");
270		return (ENXIO);
271	}
272
273	sc = device_get_softc(devi);
274	slp = &sc->sc_sclow;
275	slp->sl_dev = devi;
276
277	if (sc->mem_res == NULL) {
278		device_printf(devi,
279		    "WARNING: CANNOT GET Memory RESOURCE going PIO mode\n");
280		flags |= PIO_MODE;
281	}
282
283	/* slp->sl_irq = devi->pd_irq; */
284	sc->sc_iclkdiv = CLKDIVR_20M;
285	sc->sc_clkdiv = CLKDIVR_40M;
286
287	slp->sl_hostid = NSP_HOSTID;
288	slp->sl_cfgflags = flags;
289
290	nspattachsubr(sc);
291
292	return(NSP_IOSIZE);
293}
294