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