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: head/sys/dev/nsp/nsp_pccard.c 129740 2004-05-26 00:53:10Z imp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/errno.h>
44
45#include <vm/vm.h>
46
47#include <machine/bus.h>
48#include <compat/netbsd/dvcfg.h>
49
50#include <sys/device_port.h>
51
52#include "pccarddevs.h"
53#include <dev/pccard/pccardvar.h>
54
55#include <cam/scsi/scsi_low.h>
56#include <cam/scsi/scsi_low_pisa.h>
57
58#include <dev/nsp/nspreg.h>
59#include <dev/nsp/nspvar.h>
60
61#define NSP_HOSTID 7
62
63#include <sys/kernel.h>
64#include <sys/module.h>
65#if !defined(__FreeBSD__) || __FreeBSD_version < 500014
66#include <sys/select.h>
67#endif
68#include <pccard/cardinfo.h>
69#include <pccard/slot.h>
70
71#define PIO_MODE 0x100 /* pd_flags */
72
73static int nspprobe(DEVPORT_PDEVICE devi);
74static int nspattach(DEVPORT_PDEVICE devi);
75
76static void nsp_card_unload (DEVPORT_PDEVICE);
77
78const struct pccard_product nsp_products[] = {
79 PCMCIA_CARD(IODATA3, CBSC16, 0),
80 PCMCIA_CARD(PANASONIC, KME, 0),
81 PCMCIA_CARD(WORKBIT2, NINJA_SCSI3, 0),
82 PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16, 0),
83 { NULL }
84};
85
86/*
87 * Additional code for FreeBSD new-bus PCCard frontend
88 */
89
90static void
91nsp_pccard_intr(void * arg)
92{
93 nspintr(arg);
94}
95
96static void
97nsp_release_resource(DEVPORT_PDEVICE dev)
98{
99 struct nsp_softc *sc = device_get_softc(dev);
100
101 if (sc->nsp_intrhand) {
102 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
103 }
104
105 if (sc->port_res) {
106 bus_release_resource(dev, SYS_RES_IOPORT,
107 sc->port_rid, sc->port_res);
108 }
109
110 if (sc->irq_res) {
111 bus_release_resource(dev, SYS_RES_IRQ,
112 sc->irq_rid, sc->irq_res);
113 }
114
115 if (sc->mem_res) {
116 bus_release_resource(dev, SYS_RES_MEMORY,
117 sc->mem_rid, sc->mem_res);
118 }
119}
120
121static int
122nsp_alloc_resource(DEVPORT_PDEVICE dev)
123{
124 struct nsp_softc *sc = device_get_softc(dev);
125 u_long ioaddr, iosize, maddr, msize;
126 int error;
127
128 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
129 if (error || iosize < NSP_IOSIZE)
130 return(ENOMEM);
131
132 sc->port_rid = 0;
133 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
134 0, ~0, NSP_IOSIZE, RF_ACTIVE);
135 if (sc->port_res == NULL) {
136 nsp_release_resource(dev);
137 return(ENOMEM);
138 }
139
140 sc->irq_rid = 0;
141 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
142 RF_ACTIVE);
143 if (sc->irq_res == NULL) {
144 nsp_release_resource(dev);
145 return(ENOMEM);
146 }
147
148 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
149 if (error) {
150 return(0); /* XXX */
151 }
152
153 /* No need to allocate memory if not configured and it's in PIO mode */
154 if (maddr == 0 || msize == 0) {
155 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
156 printf("Memory window was not configured. Configure or use in PIO mode.");
157 nsp_release_resource(dev);
158 return(ENOMEM);
159 }
160 /* no need to allocate memory if PIO mode */
161 return(0);
162 }
163
164 sc->mem_rid = 0;
165 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
166 RF_ACTIVE);
167 if (sc->mem_res == NULL) {
168 nsp_release_resource(dev);
169 return(ENOMEM);
170 }
171
172 return(0);
173}
174
175static int nsp_pccard_match(device_t dev)
176{
177 const struct pccard_product *pp;
178
179 if ((pp = pccard_product_lookup(dev, nsp_products,
180 sizeof(nsp_products[0]), NULL)) != NULL) {
181 device_set_desc(dev, pp->pp_name);
182 return(0);
183 }
184 return(EIO);
185}
186
187static int
188nsp_pccard_probe(DEVPORT_PDEVICE dev)
189{
190 struct nsp_softc *sc = device_get_softc(dev);
191 int error;
192
193 bzero(sc, sizeof(struct nsp_softc));
194
195 error = nsp_alloc_resource(dev);
196 if (error) {
197 return(error);
198 }
199
200 if (nspprobe(dev) == 0) {
201 nsp_release_resource(dev);
202 return(ENXIO);
203 }
204
205 nsp_release_resource(dev);
206
207 return(0);
208}
209
210static int
211nsp_pccard_attach(DEVPORT_PDEVICE dev)
212{
213 struct nsp_softc *sc = device_get_softc(dev);
214 int error;
215
216 error = nsp_alloc_resource(dev);
217 if (error) {
218 return(error);
219 }
220
221 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
222 nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
223 if (error) {
224 nsp_release_resource(dev);
225 return(error);
226 }
227
228 if (nspattach(dev) == 0) {
229 nsp_release_resource(dev);
230 return(ENXIO);
231 }
232
233 return(0);
234}
235
236static void
237nsp_pccard_detach(DEVPORT_PDEVICE dev)
238{
239 nsp_card_unload(dev);
240 nsp_release_resource(dev);
241}
242
243static device_method_t nsp_pccard_methods[] = {
244 /* Device interface */
245 DEVMETHOD(device_probe, pccard_compat_probe),
246 DEVMETHOD(device_attach, pccard_compat_attach),
247 DEVMETHOD(device_detach, nsp_pccard_detach),
248
249 /* Card interface */
250 DEVMETHOD(card_compat_match, nsp_pccard_match),
251 DEVMETHOD(card_compat_probe, nsp_pccard_probe),
252 DEVMETHOD(card_compat_attach, nsp_pccard_attach),
253
254 { 0, 0 }
255};
256
257static driver_t nsp_pccard_driver = {
258 "nsp",
259 nsp_pccard_methods,
260 sizeof(struct nsp_softc),
261};
262
263static devclass_t nsp_devclass;
264
265MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
266DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
267
268static void
269nsp_card_unload(DEVPORT_PDEVICE devi)
270{
271 struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
272 intrmask_t s;
273
274 printf("%s: unload\n",sc->sc_sclow.sl_xname);
275 s = splcam();
276 scsi_low_deactivate((struct scsi_low_softc *)sc);
277 scsi_low_dettach(&sc->sc_sclow);
278 splx(s);
279}
280
281static int
282nspprobe(DEVPORT_PDEVICE devi)
283{
284 int rv;
285 struct nsp_softc *sc = device_get_softc(devi);
286
287 rv = nspprobesubr(rman_get_bustag(sc->port_res),
288 rman_get_bushandle(sc->port_res),
289 DEVPORT_PDEVFLAGS(devi));
290
291 return rv;
292}
293
294static int
295nspattach(DEVPORT_PDEVICE devi)
296{
297 struct nsp_softc *sc;
298 struct scsi_low_softc *slp;
299 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
300 u_int iobase = DEVPORT_PDEVIOBASE(devi);
301 intrmask_t s;
302 char dvname[16];
303
304 strcpy(dvname,"nsp");
305
306 if (iobase == 0)
307 {
308 printf("%s: no ioaddr is given\n", dvname);
309 return (0);
310 }
311
312 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
313 if (sc == NULL) {
314 return (0);
315 }
316
317 slp = &sc->sc_sclow;
318 slp->sl_dev = devi;
319 sc->sc_iot = rman_get_bustag(sc->port_res);
320 sc->sc_ioh = rman_get_bushandle(sc->port_res);
321
322 if(sc->mem_res == NULL){
323 printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode");
324 flags |= PIO_MODE;
325 }
326
327 if((flags & PIO_MODE) == 0) {
328 sc->sc_memt = rman_get_bustag(sc->mem_res);
329 sc->sc_memh = rman_get_bushandle(sc->mem_res);
330 } else {
331 sc->sc_memh = 0;
332 }
333 /* slp->sl_irq = devi->pd_irq; */
334 sc->sc_iclkdiv = CLKDIVR_20M;
335 sc->sc_clkdiv = CLKDIVR_40M;
336
337 slp->sl_hostid = NSP_HOSTID;
338 slp->sl_cfgflags = flags;
339
340 s = splcam();
341 nspattachsubr(sc);
342 splx(s);
343
344 return(NSP_IOSIZE);
345}
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: head/sys/dev/nsp/nsp_pccard.c 129740 2004-05-26 00:53:10Z imp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/errno.h>
44
45#include <vm/vm.h>
46
47#include <machine/bus.h>
48#include <compat/netbsd/dvcfg.h>
49
50#include <sys/device_port.h>
51
52#include "pccarddevs.h"
53#include <dev/pccard/pccardvar.h>
54
55#include <cam/scsi/scsi_low.h>
56#include <cam/scsi/scsi_low_pisa.h>
57
58#include <dev/nsp/nspreg.h>
59#include <dev/nsp/nspvar.h>
60
61#define NSP_HOSTID 7
62
63#include <sys/kernel.h>
64#include <sys/module.h>
65#if !defined(__FreeBSD__) || __FreeBSD_version < 500014
66#include <sys/select.h>
67#endif
68#include <pccard/cardinfo.h>
69#include <pccard/slot.h>
70
71#define PIO_MODE 0x100 /* pd_flags */
72
73static int nspprobe(DEVPORT_PDEVICE devi);
74static int nspattach(DEVPORT_PDEVICE devi);
75
76static void nsp_card_unload (DEVPORT_PDEVICE);
77
78const struct pccard_product nsp_products[] = {
79 PCMCIA_CARD(IODATA3, CBSC16, 0),
80 PCMCIA_CARD(PANASONIC, KME, 0),
81 PCMCIA_CARD(WORKBIT2, NINJA_SCSI3, 0),
82 PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16, 0),
83 { NULL }
84};
85
86/*
87 * Additional code for FreeBSD new-bus PCCard frontend
88 */
89
90static void
91nsp_pccard_intr(void * arg)
92{
93 nspintr(arg);
94}
95
96static void
97nsp_release_resource(DEVPORT_PDEVICE dev)
98{
99 struct nsp_softc *sc = device_get_softc(dev);
100
101 if (sc->nsp_intrhand) {
102 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
103 }
104
105 if (sc->port_res) {
106 bus_release_resource(dev, SYS_RES_IOPORT,
107 sc->port_rid, sc->port_res);
108 }
109
110 if (sc->irq_res) {
111 bus_release_resource(dev, SYS_RES_IRQ,
112 sc->irq_rid, sc->irq_res);
113 }
114
115 if (sc->mem_res) {
116 bus_release_resource(dev, SYS_RES_MEMORY,
117 sc->mem_rid, sc->mem_res);
118 }
119}
120
121static int
122nsp_alloc_resource(DEVPORT_PDEVICE dev)
123{
124 struct nsp_softc *sc = device_get_softc(dev);
125 u_long ioaddr, iosize, maddr, msize;
126 int error;
127
128 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
129 if (error || iosize < NSP_IOSIZE)
130 return(ENOMEM);
131
132 sc->port_rid = 0;
133 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
134 0, ~0, NSP_IOSIZE, RF_ACTIVE);
135 if (sc->port_res == NULL) {
136 nsp_release_resource(dev);
137 return(ENOMEM);
138 }
139
140 sc->irq_rid = 0;
141 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
142 RF_ACTIVE);
143 if (sc->irq_res == NULL) {
144 nsp_release_resource(dev);
145 return(ENOMEM);
146 }
147
148 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
149 if (error) {
150 return(0); /* XXX */
151 }
152
153 /* No need to allocate memory if not configured and it's in PIO mode */
154 if (maddr == 0 || msize == 0) {
155 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
156 printf("Memory window was not configured. Configure or use in PIO mode.");
157 nsp_release_resource(dev);
158 return(ENOMEM);
159 }
160 /* no need to allocate memory if PIO mode */
161 return(0);
162 }
163
164 sc->mem_rid = 0;
165 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
166 RF_ACTIVE);
167 if (sc->mem_res == NULL) {
168 nsp_release_resource(dev);
169 return(ENOMEM);
170 }
171
172 return(0);
173}
174
175static int nsp_pccard_match(device_t dev)
176{
177 const struct pccard_product *pp;
178
179 if ((pp = pccard_product_lookup(dev, nsp_products,
180 sizeof(nsp_products[0]), NULL)) != NULL) {
181 device_set_desc(dev, pp->pp_name);
182 return(0);
183 }
184 return(EIO);
185}
186
187static int
188nsp_pccard_probe(DEVPORT_PDEVICE dev)
189{
190 struct nsp_softc *sc = device_get_softc(dev);
191 int error;
192
193 bzero(sc, sizeof(struct nsp_softc));
194
195 error = nsp_alloc_resource(dev);
196 if (error) {
197 return(error);
198 }
199
200 if (nspprobe(dev) == 0) {
201 nsp_release_resource(dev);
202 return(ENXIO);
203 }
204
205 nsp_release_resource(dev);
206
207 return(0);
208}
209
210static int
211nsp_pccard_attach(DEVPORT_PDEVICE dev)
212{
213 struct nsp_softc *sc = device_get_softc(dev);
214 int error;
215
216 error = nsp_alloc_resource(dev);
217 if (error) {
218 return(error);
219 }
220
221 error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
222 nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
223 if (error) {
224 nsp_release_resource(dev);
225 return(error);
226 }
227
228 if (nspattach(dev) == 0) {
229 nsp_release_resource(dev);
230 return(ENXIO);
231 }
232
233 return(0);
234}
235
236static void
237nsp_pccard_detach(DEVPORT_PDEVICE dev)
238{
239 nsp_card_unload(dev);
240 nsp_release_resource(dev);
241}
242
243static device_method_t nsp_pccard_methods[] = {
244 /* Device interface */
245 DEVMETHOD(device_probe, pccard_compat_probe),
246 DEVMETHOD(device_attach, pccard_compat_attach),
247 DEVMETHOD(device_detach, nsp_pccard_detach),
248
249 /* Card interface */
250 DEVMETHOD(card_compat_match, nsp_pccard_match),
251 DEVMETHOD(card_compat_probe, nsp_pccard_probe),
252 DEVMETHOD(card_compat_attach, nsp_pccard_attach),
253
254 { 0, 0 }
255};
256
257static driver_t nsp_pccard_driver = {
258 "nsp",
259 nsp_pccard_methods,
260 sizeof(struct nsp_softc),
261};
262
263static devclass_t nsp_devclass;
264
265MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
266DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
267
268static void
269nsp_card_unload(DEVPORT_PDEVICE devi)
270{
271 struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
272 intrmask_t s;
273
274 printf("%s: unload\n",sc->sc_sclow.sl_xname);
275 s = splcam();
276 scsi_low_deactivate((struct scsi_low_softc *)sc);
277 scsi_low_dettach(&sc->sc_sclow);
278 splx(s);
279}
280
281static int
282nspprobe(DEVPORT_PDEVICE devi)
283{
284 int rv;
285 struct nsp_softc *sc = device_get_softc(devi);
286
287 rv = nspprobesubr(rman_get_bustag(sc->port_res),
288 rman_get_bushandle(sc->port_res),
289 DEVPORT_PDEVFLAGS(devi));
290
291 return rv;
292}
293
294static int
295nspattach(DEVPORT_PDEVICE devi)
296{
297 struct nsp_softc *sc;
298 struct scsi_low_softc *slp;
299 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
300 u_int iobase = DEVPORT_PDEVIOBASE(devi);
301 intrmask_t s;
302 char dvname[16];
303
304 strcpy(dvname,"nsp");
305
306 if (iobase == 0)
307 {
308 printf("%s: no ioaddr is given\n", dvname);
309 return (0);
310 }
311
312 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
313 if (sc == NULL) {
314 return (0);
315 }
316
317 slp = &sc->sc_sclow;
318 slp->sl_dev = devi;
319 sc->sc_iot = rman_get_bustag(sc->port_res);
320 sc->sc_ioh = rman_get_bushandle(sc->port_res);
321
322 if(sc->mem_res == NULL){
323 printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode");
324 flags |= PIO_MODE;
325 }
326
327 if((flags & PIO_MODE) == 0) {
328 sc->sc_memt = rman_get_bustag(sc->mem_res);
329 sc->sc_memh = rman_get_bushandle(sc->mem_res);
330 } else {
331 sc->sc_memh = 0;
332 }
333 /* slp->sl_irq = devi->pd_irq; */
334 sc->sc_iclkdiv = CLKDIVR_20M;
335 sc->sc_clkdiv = CLKDIVR_40M;
336
337 slp->sl_hostid = NSP_HOSTID;
338 slp->sl_cfgflags = flags;
339
340 s = splcam();
341 nspattachsubr(sc);
342 splx(s);
343
344 return(NSP_IOSIZE);
345}