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