2 /* $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $ */
8 * Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
10 * [NetBSD for NEC PC-98 series]
12 * NetBSD/pc98 porting staff. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/disklabel.h>
41 #if defined(__FreeBSD__) && __FreeBSD_version >= 500001
45 #include <sys/queue.h>
46 #include <sys/malloc.h>
47 #include <sys/errno.h>
51 #include <machine/bus.h>
52 #include <i386/isa/isa_device.h>
54 #include <machine/dvcfg.h>
56 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
57 static struct nsp_softc *nsp_get_softc(int);
58 extern struct nsp_softc *nspdata[];
59 #define DEVPORT_ALLOCSOFTCFUNC nsp_get_softc
60 #define DEVPORT_SOFTCARRAY nspdata
62 #include <sys/device_port.h>
64 #include <cam/scsi/scsi_low.h>
65 #include <cam/scsi/scsi_low_pisa.h>
67 #include <dev/nsp/nspreg.h>
68 #include <dev/nsp/nspvar.h>
69 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD_version < 400001)
78 #include <sys/kernel.h>
79 #include <sys/module.h>
80 #if !defined(__FreeBSD__) || __FreeBSD_version < 500014
81 #include <sys/select.h>
83 #include <pccard/cardinfo.h>
84 #include <pccard/slot.h>
86 #define PIO_MODE 0x100 /* pd_flags */
88 static int nspprobe(DEVPORT_PDEVICE devi);
89 static int nspattach(DEVPORT_PDEVICE devi);
91 static void nsp_card_unload __P((DEVPORT_PDEVICE));
92 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
93 static int nsp_card_init __P((DEVPORT_PDEVICE));
94 static int nsp_card_intr __P((DEVPORT_PDEVICE));
97 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
99 * Additional code for FreeBSD new-bus PCCard frontend
103 nsp_pccard_intr(void * arg)
109 nsp_release_resource(DEVPORT_PDEVICE dev)
111 struct nsp_softc *sc = device_get_softc(dev);
113 if (sc->nsp_intrhand) {
114 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
118 bus_release_resource(dev, SYS_RES_IOPORT,
119 sc->port_rid, sc->port_res);
123 bus_release_resource(dev, SYS_RES_IRQ,
124 sc->irq_rid, sc->irq_res);
128 bus_release_resource(dev, SYS_RES_MEMORY,
129 sc->mem_rid, sc->mem_res);
134 nsp_alloc_resource(DEVPORT_PDEVICE dev)
136 struct nsp_softc *sc = device_get_softc(dev);
137 u_long ioaddr, iosize, maddr, msize;
140 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
141 if (error || iosize < NSP_IOSIZE)
145 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
146 0, ~0, 0, RF_ACTIVE);
147 if (sc->port_res == NULL) {
148 nsp_release_resource(dev);
153 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
154 0, ~0, 1, RF_ACTIVE);
155 if (sc->irq_res == NULL) {
156 nsp_release_resource(dev);
160 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
165 /* No need to allocate memory if not configured and it's in PIO mode */
166 if (maddr == 0 || msize == 0) {
167 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
168 printf("Memory window was not configured. Configure or use in PIO mode.");
169 nsp_release_resource(dev);
172 /* no need to allocate memory if PIO mode */
177 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
178 0, ~0, msize, RF_ACTIVE);
179 if (sc->mem_res == NULL) {
180 nsp_release_resource(dev);
188 nsp_pccard_probe(DEVPORT_PDEVICE dev)
190 struct nsp_softc *sc = device_get_softc(dev);
193 bzero(sc, sizeof(struct nsp_softc));
195 error = nsp_alloc_resource(dev);
200 if (nspprobe(dev) == 0) {
201 nsp_release_resource(dev);
205 nsp_release_resource(dev);
211 nsp_pccard_attach(DEVPORT_PDEVICE dev)
213 struct nsp_softc *sc = device_get_softc(dev);
216 error = nsp_alloc_resource(dev);
221 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
222 nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
224 nsp_release_resource(dev);
228 if (nspattach(dev) == 0) {
229 nsp_release_resource(dev);
237 nsp_pccard_detach(DEVPORT_PDEVICE dev)
239 nsp_card_unload(dev);
240 nsp_release_resource(dev);
243 static device_method_t nsp_pccard_methods[] = {
244 /* Device interface */
245 DEVMETHOD(device_probe, nsp_pccard_probe),
246 DEVMETHOD(device_attach, nsp_pccard_attach),
247 DEVMETHOD(device_detach, nsp_pccard_detach),
252 static driver_t nsp_pccard_driver = {
255 sizeof(struct nsp_softc),
258 static devclass_t nsp_devclass;
260 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
264 PCCARD_MODULE(nsp, nsp_card_init,nsp_card_unload, nsp_card_intr,0, cam_imask);
268 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
269 static struct nsp_softc *
270 nsp_get_softc(int unit)
272 struct nsp_softc *sc;
278 if (nspdata[unit] == NULL) {
279 sc = malloc(sizeof(struct nsp_softc), M_TEMP,M_NOWAIT);
281 printf("nsp_get_softc: cannot malloc!\n");
293 nsp_card_init(DEVPORT_PDEVICE devi)
295 int unit = DEVPORT_PDEVUNIT(devi);
300 if (nspprobe(devi) == 0)
303 if (nspattach(devi) == 0)
310 nsp_card_intr(DEVPORT_PDEVICE devi)
312 nspintr(DEVPORT_PDEVGET_SOFTC(devi));
318 nsp_card_unload(DEVPORT_PDEVICE devi)
320 struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
322 printf("%s: unload\n",sc->sc_sclow.sl_xname);
323 scsi_low_deactivate((struct scsi_low_softc *)sc);
324 scsi_low_dettach(&sc->sc_sclow);
328 nspprobe(DEVPORT_PDEVICE devi)
331 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
332 struct nsp_softc *sc = device_get_softc(devi);
334 rv = nspprobesubr(rman_get_bustag(sc->port_res),
335 rman_get_bushandle(sc->port_res),
336 DEVPORT_PDEVFLAGS(devi));
338 rv = nspprobesubr(I386_BUS_SPACE_IO,
339 DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi));
346 nspattach(DEVPORT_PDEVICE devi)
348 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
349 int unit = DEVPORT_PDEVUNIT(devi);
351 struct nsp_softc *sc;
352 struct scsi_low_softc *slp;
353 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
354 u_int iobase = DEVPORT_PDEVIOBASE(devi);
357 strcpy(dvname,"nsp");
359 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
362 printf("%s: unit number too high\n",dvname);
369 printf("%s: no ioaddr is given\n", dvname);
373 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
379 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
381 sc->sc_iot = rman_get_bustag(sc->port_res);
382 sc->sc_ioh = rman_get_bushandle(sc->port_res);
384 bzero(sc, sizeof(struct nsp_softc));
385 strcpy(slp->sl_dev.dv_xname, dvname);
386 slp->sl_dev.dv_unit = unit;
387 sc->sc_iot = I386_BUS_SPACE_IO;
391 if((flags & PIO_MODE) == 0) {
392 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
393 sc->sc_memt = rman_get_bustag(sc->mem_res);
394 sc->sc_memh = rman_get_bushandle(sc->mem_res);
396 sc->sc_memt = I386_BUS_SPACE_MEM;
397 sc->sc_memh = (bus_space_handle_t)DEVPORT_PDEVMADDR(devi);
402 /* slp->sl_irq = devi->pd_irq; */
403 sc->sc_iclkdiv = CLKDIVR_20M;
404 sc->sc_clkdiv = CLKDIVR_40M;
406 slp->sl_hostid = NSP_HOSTID;
407 slp->sl_cfgflags = flags;