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