1809b9311767c31083dd41f2aaae01d5536d7ade
[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.13 2007/07/05 12:08:53 sephe 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/buf.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/errno.h>
45 #include <sys/bus.h>
46 #include <sys/thread2.h>
47
48 #include <vm/vm.h>
49
50 #include <sys/rman.h>
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
70 #define PIO_MODE 0x100          /* pd_flags */
71
72 static int nspprobe(DEVPORT_PDEVICE devi);
73 static int nspattach(DEVPORT_PDEVICE devi);
74
75 static  void    nsp_card_unload (DEVPORT_PDEVICE);
76
77 static const struct pccard_product nsp_products[] = {
78         PCMCIA_CARD(IODATA3, CBSC16, 0),
79         PCMCIA_CARD(PANASONIC, KME, 0),
80         PCMCIA_CARD(WORKBIT2, NINJA_SCSI3, 0),
81         PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16, 0),
82         { NULL }
83 };
84
85 /*
86  * Additional code for FreeBSD new-bus PCCard frontend
87  */
88
89 static void
90 nsp_pccard_intr(void * arg)
91 {
92         nspintr(arg);
93 }
94
95 static void
96 nsp_release_resource(DEVPORT_PDEVICE dev)
97 {
98         struct nsp_softc        *sc = device_get_softc(dev);
99
100         if (sc->nsp_intrhand) {
101                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
102         }
103
104         if (sc->port_res) {
105                 bus_release_resource(dev, SYS_RES_IOPORT,
106                                      sc->port_rid, sc->port_res);
107         }
108
109         if (sc->irq_res) {
110                 bus_release_resource(dev, SYS_RES_IRQ,
111                                      sc->irq_rid, sc->irq_res);
112         }
113
114         if (sc->mem_res) {
115                 bus_release_resource(dev, SYS_RES_MEMORY,
116                                      sc->mem_rid, sc->mem_res);
117         }
118 }
119
120 static int
121 nsp_alloc_resource(DEVPORT_PDEVICE dev)
122 {
123         struct nsp_softc        *sc = device_get_softc(dev);
124         u_long                  ioaddr, iosize, maddr, msize;
125         int                     error;
126
127         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
128         if (error || iosize < NSP_IOSIZE)
129                 return(ENOMEM);
130
131         sc->port_rid = 0;
132         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
133                                           0, ~0, NSP_IOSIZE, RF_ACTIVE);
134         if (sc->port_res == NULL) {
135                 nsp_release_resource(dev);
136                 return(ENOMEM);
137         }
138
139         sc->irq_rid = 0;
140         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
141                                          0, ~0, 1, RF_ACTIVE);
142         if (sc->irq_res == NULL) {
143                 nsp_release_resource(dev);
144                 return(ENOMEM);
145         }
146
147         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
148         if (error) {
149                 return(0);      /* XXX */
150         }
151
152         /* No need to allocate memory if not configured and it's in PIO mode */
153         if (maddr == 0 || msize == 0) {
154                 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
155                         kprintf("Memory window was not configured. Configure or use in PIO mode.");
156                         nsp_release_resource(dev);
157                         return(ENOMEM);
158                 }
159                 /* no need to allocate memory if PIO mode */
160                 return(0);
161         }
162
163         sc->mem_rid = 0;
164         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
165                                          0, ~0, 1, RF_ACTIVE);
166         if (sc->mem_res == NULL) {
167                 nsp_release_resource(dev);
168                 return(ENOMEM);
169         }
170
171         return(0);
172 }
173
174 static int nsp_pccard_match(device_t dev)
175 {
176         const struct pccard_product *pp;
177
178         if ((pp = pccard_product_lookup(dev, nsp_products,
179             sizeof(nsp_products[0]), NULL)) != NULL) {
180                 device_set_desc(dev, pp->pp_name);
181                 return (0);
182         }
183         return (ENXIO);
184 }
185
186 static int
187 nsp_pccard_probe(DEVPORT_PDEVICE dev)
188 {
189         struct nsp_softc        *sc = device_get_softc(dev);
190         int                     error;
191
192         bzero(sc, sizeof(struct nsp_softc));
193
194         error = nsp_alloc_resource(dev);
195         if (error) {
196                 return(error);
197         }
198
199         if (nspprobe(dev) == 0) {
200                 nsp_release_resource(dev);
201                 return(ENXIO);
202         }
203
204         nsp_release_resource(dev);
205
206         return(0);
207 }
208
209 static int
210 nsp_pccard_attach(DEVPORT_PDEVICE dev)
211 {
212         struct nsp_softc        *sc = device_get_softc(dev);
213         int                     error;
214
215         error = nsp_alloc_resource(dev);
216         if (error) {
217                 return(error);
218         }
219
220         error = bus_setup_intr(dev, sc->irq_res, 0,
221                                nsp_pccard_intr, (void *)sc,
222                                &sc->nsp_intrhand, NULL);
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
236 static  void
237 nsp_pccard_detach(DEVPORT_PDEVICE dev)
238 {
239         nsp_card_unload(dev);
240         nsp_release_resource(dev);
241 }
242
243 static 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
257 static driver_t nsp_pccard_driver = {
258         "nsp",
259         nsp_pccard_methods,
260         sizeof(struct nsp_softc),
261 };
262
263 static devclass_t nsp_devclass;
264
265 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
266 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
267
268 static  void
269 nsp_card_unload(DEVPORT_PDEVICE devi)
270 {
271         struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
272
273         kprintf("%s: unload\n",sc->sc_sclow.sl_xname);
274         crit_enter();
275         scsi_low_deactivate((struct scsi_low_softc *)sc);
276         scsi_low_dettach(&sc->sc_sclow);
277         crit_exit();
278 }
279
280 static  int
281 nspprobe(DEVPORT_PDEVICE devi)
282 {
283         int rv;
284         struct nsp_softc *sc = device_get_softc(devi);
285
286         rv = nspprobesubr(rman_get_bustag(sc->port_res),
287                           rman_get_bushandle(sc->port_res),
288                           DEVPORT_PDEVFLAGS(devi));
289
290         return rv;
291 }
292
293 static  int
294 nspattach(DEVPORT_PDEVICE devi)
295 {
296         struct nsp_softc *sc;
297         struct scsi_low_softc *slp;
298         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
299         u_int   iobase = DEVPORT_PDEVIOBASE(devi);
300         char    dvname[16];
301
302         strcpy(dvname,"nsp");
303
304         if (iobase == 0)
305         {
306                 kprintf("%s: no ioaddr is given\n", dvname);
307                 return (0);
308         }
309
310         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
311         if (sc == NULL) {
312                 return (0);
313         }
314
315         slp = &sc->sc_sclow;
316         slp->sl_dev = devi;
317         sc->sc_iot = rman_get_bustag(sc->port_res);
318         sc->sc_ioh = rman_get_bushandle(sc->port_res);
319
320         if((flags & PIO_MODE) == 0) {
321                 sc->sc_memt = rman_get_bustag(sc->mem_res);
322                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
323         } else {
324                 sc->sc_memh = 0;
325         }
326         /* slp->sl_irq = devi->pd_irq; */
327         sc->sc_iclkdiv = CLKDIVR_20M;
328         sc->sc_clkdiv = CLKDIVR_40M;
329
330         slp->sl_hostid = NSP_HOSTID;
331         slp->sl_cfgflags = flags;
332
333         crit_enter();
334         nspattachsubr(sc);
335         crit_exit();
336
337         return(NSP_IOSIZE);
338 }