kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / disk / nsp / nsp_pccard.c
1 /*      $FreeBSD: src/sys/dev/nsp/nsp_pccard.c,v 1.2.2.6 2001/12/17 13:30:19 non Exp $  */
2 /*      $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $    */
3 /*      $NetBSD$        */
4
5 /*
6  * [Ported for FreeBSD]
7  *  Copyright (c) 2000
8  *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
9  *      All rights reserved.
10  * [NetBSD for NEC PC-98 series]
11  *  Copyright (c) 1998
12  *      NetBSD/pc98 porting staff. All rights reserved.
13  * 
14  *  Redistribution and use in source and binary forms, with or without
15  *  modification, are permitted provided that the following conditions
16  *  are met:
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.
24  * 
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.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/buf.h>
41 #include <sys/queue.h>
42 #include <sys/malloc.h>
43 #include <sys/errno.h>
44 #include <sys/bus.h>
45 #include <sys/thread2.h>
46
47 #include <vm/vm.h>
48
49 #include <sys/rman.h>
50 #include <sys/device_port.h>
51
52 #include <bus/pccard/pccarddevs.h>
53 #include <bus/pccard/pccardvar.h>
54
55 #include <bus/cam/scsi/scsi_low.h>
56 #include <bus/cam/scsi/scsi_low_pisa.h>
57
58 #include "nspreg.h"
59 #include "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
69 #define PIO_MODE 0x100          /* pd_flags */
70
71 static int nspprobe(DEVPORT_PDEVICE devi);
72 static int nspattach(DEVPORT_PDEVICE devi);
73
74 static  void    nsp_card_unload (DEVPORT_PDEVICE);
75
76 static const struct pccard_product nsp_products[] = {
77         PCMCIA_CARD(IODATA3, CBSC16, 0),
78         PCMCIA_CARD(PANASONIC, KME, 0),
79         PCMCIA_CARD(WORKBIT2, NINJA_SCSI3, 0),
80         PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16, 0),
81         { NULL }
82 };
83
84 /*
85  * Additional code for FreeBSD new-bus PCCard frontend
86  */
87
88 static void
89 nsp_pccard_intr(void * arg)
90 {
91         nspintr(arg);
92 }
93
94 static void
95 nsp_release_resource(DEVPORT_PDEVICE dev)
96 {
97         struct nsp_softc        *sc = device_get_softc(dev);
98
99         if (sc->nsp_intrhand) {
100                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
101         }
102
103         if (sc->port_res) {
104                 bus_release_resource(dev, SYS_RES_IOPORT,
105                                      sc->port_rid, sc->port_res);
106         }
107
108         if (sc->irq_res) {
109                 bus_release_resource(dev, SYS_RES_IRQ,
110                                      sc->irq_rid, sc->irq_res);
111         }
112
113         if (sc->mem_res) {
114                 bus_release_resource(dev, SYS_RES_MEMORY,
115                                      sc->mem_rid, sc->mem_res);
116         }
117 }
118
119 static int
120 nsp_alloc_resource(DEVPORT_PDEVICE dev)
121 {
122         struct nsp_softc        *sc = device_get_softc(dev);
123         u_long                  ioaddr, iosize, maddr, msize;
124         int                     error;
125
126         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
127         if (error || iosize < NSP_IOSIZE)
128                 return(ENOMEM);
129
130         sc->port_rid = 0;
131         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
132                                           0, ~0, NSP_IOSIZE, RF_ACTIVE);
133         if (sc->port_res == NULL) {
134                 nsp_release_resource(dev);
135                 return(ENOMEM);
136         }
137
138         sc->irq_rid = 0;
139         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
140                                          0, ~0, 1, RF_ACTIVE);
141         if (sc->irq_res == NULL) {
142                 nsp_release_resource(dev);
143                 return(ENOMEM);
144         }
145
146         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
147         if (error) {
148                 return(0);      /* XXX */
149         }
150
151         /* No need to allocate memory if not configured and it's in PIO mode */
152         if (maddr == 0 || msize == 0) {
153                 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
154                         kprintf("Memory window was not configured. Configure or use in PIO mode.");
155                         nsp_release_resource(dev);
156                         return(ENOMEM);
157                 }
158                 /* no need to allocate memory if PIO mode */
159                 return(0);
160         }
161
162         sc->mem_rid = 0;
163         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
164                                          0, ~0, 1, RF_ACTIVE);
165         if (sc->mem_res == NULL) {
166                 nsp_release_resource(dev);
167                 return(ENOMEM);
168         }
169
170         return(0);
171 }
172
173 static int nsp_pccard_match(device_t dev)
174 {
175         const struct pccard_product *pp;
176
177         if ((pp = pccard_product_lookup(dev, nsp_products,
178             sizeof(nsp_products[0]), NULL)) != NULL) {
179                 device_set_desc(dev, pp->pp_name);
180                 return (0);
181         }
182         return (ENXIO);
183 }
184
185 static int
186 nsp_pccard_probe(DEVPORT_PDEVICE dev)
187 {
188         struct nsp_softc        *sc = device_get_softc(dev);
189         int                     error;
190
191         bzero(sc, sizeof(struct nsp_softc));
192
193         error = nsp_alloc_resource(dev);
194         if (error) {
195                 return(error);
196         }
197
198         if (nspprobe(dev) == 0) {
199                 nsp_release_resource(dev);
200                 return(ENXIO);
201         }
202
203         nsp_release_resource(dev);
204
205         return(0);
206 }
207
208 static int
209 nsp_pccard_attach(DEVPORT_PDEVICE dev)
210 {
211         struct nsp_softc        *sc = device_get_softc(dev);
212         int                     error;
213
214         error = nsp_alloc_resource(dev);
215         if (error) {
216                 return(error);
217         }
218
219         error = bus_setup_intr(dev, sc->irq_res, 0,
220                                nsp_pccard_intr, (void *)sc,
221                                &sc->nsp_intrhand, NULL);
222         if (error) {
223                 nsp_release_resource(dev);
224                 return(error);
225         }
226
227         if (nspattach(dev) == 0) {
228                 nsp_release_resource(dev);
229                 return(ENXIO);
230         }
231
232         return(0);
233 }
234
235 static  void
236 nsp_pccard_detach(DEVPORT_PDEVICE dev)
237 {
238         nsp_card_unload(dev);
239         nsp_release_resource(dev);
240 }
241
242 static device_method_t nsp_pccard_methods[] = {
243         /* Device interface */
244         DEVMETHOD(device_probe,         pccard_compat_probe),
245         DEVMETHOD(device_attach,        pccard_compat_attach),
246         DEVMETHOD(device_detach,        nsp_pccard_detach),
247
248         /* Card interface */
249         DEVMETHOD(card_compat_match,    nsp_pccard_match),
250         DEVMETHOD(card_compat_probe,    nsp_pccard_probe),
251         DEVMETHOD(card_compat_attach,   nsp_pccard_attach),
252
253         { 0, 0 }
254 };
255
256 static driver_t nsp_pccard_driver = {
257         "nsp",
258         nsp_pccard_methods,
259         sizeof(struct nsp_softc),
260 };
261
262 static devclass_t nsp_devclass;
263
264 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
265 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, NULL, NULL);
266
267 static  void
268 nsp_card_unload(DEVPORT_PDEVICE devi)
269 {
270         struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
271
272         kprintf("%s: unload\n",sc->sc_sclow.sl_xname);
273         crit_enter();
274         scsi_low_deactivate((struct scsi_low_softc *)sc);
275         scsi_low_dettach(&sc->sc_sclow);
276         crit_exit();
277 }
278
279 static  int
280 nspprobe(DEVPORT_PDEVICE devi)
281 {
282         int rv;
283         struct nsp_softc *sc = device_get_softc(devi);
284
285         rv = nspprobesubr(rman_get_bustag(sc->port_res),
286                           rman_get_bushandle(sc->port_res),
287                           DEVPORT_PDEVFLAGS(devi));
288
289         return rv;
290 }
291
292 static  int
293 nspattach(DEVPORT_PDEVICE devi)
294 {
295         struct nsp_softc *sc;
296         struct scsi_low_softc *slp;
297         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
298         u_int   iobase = DEVPORT_PDEVIOBASE(devi);
299         char    dvname[16];
300
301         strcpy(dvname,"nsp");
302
303         if (iobase == 0)
304         {
305                 kprintf("%s: no ioaddr is given\n", dvname);
306                 return (0);
307         }
308
309         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
310         if (sc == NULL) {
311                 return (0);
312         }
313
314         slp = &sc->sc_sclow;
315         slp->sl_dev = devi;
316         sc->sc_iot = rman_get_bustag(sc->port_res);
317         sc->sc_ioh = rman_get_bushandle(sc->port_res);
318
319         if((flags & PIO_MODE) == 0) {
320                 sc->sc_memt = rman_get_bustag(sc->mem_res);
321                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
322         } else {
323                 sc->sc_memh = 0;
324         }
325         /* slp->sl_irq = devi->pd_irq; */
326         sc->sc_iclkdiv = CLKDIVR_20M;
327         sc->sc_clkdiv = CLKDIVR_40M;
328
329         slp->sl_hostid = NSP_HOSTID;
330         slp->sl_cfgflags = flags;
331
332         crit_enter();
333         nspattachsubr(sc);
334         crit_exit();
335
336         return(NSP_IOSIZE);
337 }