1 /* $FreeBSD: src/sys/dev/nsp/nsp_pccard.c,v 1.2.2.6 2001/12/17 13:30:19 non Exp $ */
2 /* $DragonFly: src/sys/dev/disk/nsp/nsp_pccard.c,v 1.9 2006/10/25 20:55:54 dillon Exp $ */
3 /* $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $ */
9 * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
10 * All rights reserved.
11 * [NetBSD for NEC PC-98 series]
13 * NetBSD/pc98 porting staff. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/disklabel.h>
43 #include <sys/queue.h>
44 #include <sys/malloc.h>
45 #include <sys/errno.h>
47 #include <sys/thread2.h>
51 #include <machine/dvcfg.h>
53 #include <sys/device_port.h>
55 #include <bus/pccard/pccarddevs.h>
56 #include <bus/pccard/pccardvar.h>
58 #include <bus/cam/scsi/scsi_low.h>
59 #include <bus/cam/scsi/scsi_low_pisa.h>
66 #include <sys/kernel.h>
67 #include <sys/module.h>
68 #if !defined(__FreeBSD__) || __FreeBSD_version < 500014
69 #include <sys/select.h>
71 #include <bus/pccard/cardinfo.h>
72 #include <bus/pccard/slot.h>
74 #define PIO_MODE 0x100 /* pd_flags */
76 static int nspprobe(DEVPORT_PDEVICE devi);
77 static int nspattach(DEVPORT_PDEVICE devi);
79 static void nsp_card_unload (DEVPORT_PDEVICE);
81 static const struct pccard_product nsp_products[] = {
82 PCMCIA_CARD(IODATA3, CBSC16, 0),
83 PCMCIA_CARD(PANASONIC, KME, 0),
84 PCMCIA_CARD(WORKBIT2, NINJA_SCSI3, 0),
85 PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16, 0),
90 * Additional code for FreeBSD new-bus PCCard frontend
94 nsp_pccard_intr(void * arg)
100 nsp_release_resource(DEVPORT_PDEVICE dev)
102 struct nsp_softc *sc = device_get_softc(dev);
104 if (sc->nsp_intrhand) {
105 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
109 bus_release_resource(dev, SYS_RES_IOPORT,
110 sc->port_rid, sc->port_res);
114 bus_release_resource(dev, SYS_RES_IRQ,
115 sc->irq_rid, sc->irq_res);
119 bus_release_resource(dev, SYS_RES_MEMORY,
120 sc->mem_rid, sc->mem_res);
125 nsp_alloc_resource(DEVPORT_PDEVICE dev)
127 struct nsp_softc *sc = device_get_softc(dev);
128 u_long ioaddr, iosize, maddr, msize;
131 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
132 if (error || iosize < NSP_IOSIZE)
136 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
137 0, ~0, NSP_IOSIZE, RF_ACTIVE);
138 if (sc->port_res == NULL) {
139 nsp_release_resource(dev);
144 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
145 0, ~0, 1, RF_ACTIVE);
146 if (sc->irq_res == NULL) {
147 nsp_release_resource(dev);
151 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
156 /* No need to allocate memory if not configured and it's in PIO mode */
157 if (maddr == 0 || msize == 0) {
158 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
159 printf("Memory window was not configured. Configure or use in PIO mode.");
160 nsp_release_resource(dev);
163 /* no need to allocate memory if PIO mode */
168 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
169 0, ~0, 1, RF_ACTIVE);
170 if (sc->mem_res == NULL) {
171 nsp_release_resource(dev);
178 static int nsp_pccard_match(device_t dev)
180 const struct pccard_product *pp;
182 if ((pp = pccard_product_lookup(dev, nsp_products,
183 sizeof(nsp_products[0]), NULL)) != NULL) {
184 device_set_desc(dev, pp->pp_name);
191 nsp_pccard_probe(DEVPORT_PDEVICE dev)
193 struct nsp_softc *sc = device_get_softc(dev);
196 bzero(sc, sizeof(struct nsp_softc));
198 error = nsp_alloc_resource(dev);
203 if (nspprobe(dev) == 0) {
204 nsp_release_resource(dev);
208 nsp_release_resource(dev);
214 nsp_pccard_attach(DEVPORT_PDEVICE dev)
216 struct nsp_softc *sc = device_get_softc(dev);
219 error = nsp_alloc_resource(dev);
224 error = bus_setup_intr(dev, sc->irq_res, 0,
225 nsp_pccard_intr, (void *)sc,
226 &sc->nsp_intrhand, NULL);
228 nsp_release_resource(dev);
232 if (nspattach(dev) == 0) {
233 nsp_release_resource(dev);
241 nsp_pccard_detach(DEVPORT_PDEVICE dev)
243 nsp_card_unload(dev);
244 nsp_release_resource(dev);
247 static device_method_t nsp_pccard_methods[] = {
248 /* Device interface */
249 DEVMETHOD(device_probe, pccard_compat_probe),
250 DEVMETHOD(device_attach, pccard_compat_attach),
251 DEVMETHOD(device_detach, nsp_pccard_detach),
254 DEVMETHOD(card_compat_match, nsp_pccard_match),
255 DEVMETHOD(card_compat_probe, nsp_pccard_probe),
256 DEVMETHOD(card_compat_attach, nsp_pccard_attach),
261 static driver_t nsp_pccard_driver = {
264 sizeof(struct nsp_softc),
267 static devclass_t nsp_devclass;
269 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
270 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
273 nsp_card_unload(DEVPORT_PDEVICE devi)
275 struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
277 printf("%s: unload\n",sc->sc_sclow.sl_xname);
279 scsi_low_deactivate((struct scsi_low_softc *)sc);
280 scsi_low_dettach(&sc->sc_sclow);
285 nspprobe(DEVPORT_PDEVICE devi)
288 struct nsp_softc *sc = device_get_softc(devi);
290 rv = nspprobesubr(rman_get_bustag(sc->port_res),
291 rman_get_bushandle(sc->port_res),
292 DEVPORT_PDEVFLAGS(devi));
298 nspattach(DEVPORT_PDEVICE devi)
300 struct nsp_softc *sc;
301 struct scsi_low_softc *slp;
302 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
303 u_int iobase = DEVPORT_PDEVIOBASE(devi);
306 strcpy(dvname,"nsp");
310 printf("%s: no ioaddr is given\n", dvname);
314 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
321 sc->sc_iot = rman_get_bustag(sc->port_res);
322 sc->sc_ioh = rman_get_bushandle(sc->port_res);
324 if((flags & PIO_MODE) == 0) {
325 sc->sc_memt = rman_get_bustag(sc->mem_res);
326 sc->sc_memh = rman_get_bushandle(sc->mem_res);
330 /* slp->sl_irq = devi->pd_irq; */
331 sc->sc_iclkdiv = CLKDIVR_20M;
332 sc->sc_clkdiv = CLKDIVR_40M;
334 slp->sl_hostid = NSP_HOSTID;
335 slp->sl_cfgflags = flags;