]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nsp/nsp_pccard.c
This commit was generated by cvs2svn to compensate for changes in r85756,
[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/disklabel.h>
41 #if defined(__FreeBSD__) && __FreeBSD_version >= 500001
42 #include <sys/bio.h>
43 #endif
44 #include <sys/buf.h>
45 #include <sys/queue.h>
46 #include <sys/malloc.h>
47 #include <sys/errno.h>
48
49 #include <vm/vm.h>
50
51 #include <machine/bus.h>
52 #include <i386/isa/isa_device.h>
53
54 #include <machine/dvcfg.h>
55
56 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
57 static struct nsp_softc *nsp_get_softc(int);
58 extern struct nsp_softc *nspdata[];
59 #define DEVPORT_ALLOCSOFTCFUNC  nsp_get_softc
60 #define DEVPORT_SOFTCARRAY      nspdata
61 #endif
62 #include <sys/device_port.h>
63
64 #include <cam/scsi/scsi_low.h>
65 #include <cam/scsi/scsi_low_pisa.h>
66
67 #include <dev/nsp/nspreg.h>
68 #include <dev/nsp/nspvar.h>
69 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD_version < 400001)
70 #include "nsp.h"
71 #endif
72
73 #define NSP_HOSTID      7
74
75 /* pccard support */
76 #include        "card.h"
77 #if NCARD > 0
78 #include        <sys/kernel.h>
79 #include        <sys/module.h>
80 #if !defined(__FreeBSD__) || __FreeBSD_version < 500014
81 #include        <sys/select.h>
82 #endif
83 #include        <pccard/cardinfo.h>
84 #include        <pccard/slot.h>
85
86 #define PIO_MODE 0x100          /* pd_flags */
87
88 static int nspprobe(DEVPORT_PDEVICE devi);
89 static int nspattach(DEVPORT_PDEVICE devi);
90
91 static  void    nsp_card_unload __P((DEVPORT_PDEVICE));
92 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
93 static  int     nsp_card_init   __P((DEVPORT_PDEVICE));
94 static  int     nsp_card_intr   __P((DEVPORT_PDEVICE));
95 #endif
96
97 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
98 /*
99  * Additional code for FreeBSD new-bus PCCard frontend
100  */
101
102 static void
103 nsp_pccard_intr(void * arg)
104 {
105         nspintr(arg);
106 }
107
108 static void
109 nsp_release_resource(DEVPORT_PDEVICE dev)
110 {
111         struct nsp_softc        *sc = device_get_softc(dev);
112
113         if (sc->nsp_intrhand) {
114                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
115         }
116
117         if (sc->port_res) {
118                 bus_release_resource(dev, SYS_RES_IOPORT,
119                                      sc->port_rid, sc->port_res);
120         }
121
122         if (sc->irq_res) {
123                 bus_release_resource(dev, SYS_RES_IRQ,
124                                      sc->irq_rid, sc->irq_res);
125         }
126
127         if (sc->mem_res) {
128                 bus_release_resource(dev, SYS_RES_MEMORY,
129                                      sc->mem_rid, sc->mem_res);
130         }
131 }
132
133 static int
134 nsp_alloc_resource(DEVPORT_PDEVICE dev)
135 {
136         struct nsp_softc        *sc = device_get_softc(dev);
137         u_long                  ioaddr, iosize, maddr, msize;
138         int                     error;
139
140         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
141         if (error || iosize < NSP_IOSIZE)
142                 return(ENOMEM);
143
144         sc->port_rid = 0;
145         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
146                                           0, ~0, NSP_IOSIZE, RF_ACTIVE);
147         if (sc->port_res == NULL) {
148                 nsp_release_resource(dev);
149                 return(ENOMEM);
150         }
151
152         sc->irq_rid = 0;
153         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
154                                          0, ~0, 1, RF_ACTIVE);
155         if (sc->irq_res == NULL) {
156                 nsp_release_resource(dev);
157                 return(ENOMEM);
158         }
159
160         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
161         if (error) {
162                 return(0);      /* XXX */
163         }
164
165         /* No need to allocate memory if not configured and it's in PIO mode */
166         if (maddr == 0 || msize == 0) {
167                 if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) == 0) {
168                         printf("Memory window was not configured. Configure or use in PIO mode.");
169                         nsp_release_resource(dev);
170                         return(ENOMEM);
171                 }
172                 /* no need to allocate memory if PIO mode */
173                 return(0);
174         }
175
176         sc->mem_rid = 0;
177         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
178                                          0, ~0, 1, RF_ACTIVE);
179         if (sc->mem_res == NULL) {
180                 nsp_release_resource(dev);
181                 return(ENOMEM);
182         }
183
184         return(0);
185 }
186
187 static int
188 nsp_pccard_probe(DEVPORT_PDEVICE dev)
189 {
190         struct nsp_softc        *sc = device_get_softc(dev);
191         int                     error;
192
193         bzero(sc, sizeof(struct nsp_softc));
194
195         error = nsp_alloc_resource(dev);
196         if (error) {
197                 return(error);
198         }
199
200         if (nspprobe(dev) == 0) {
201                 nsp_release_resource(dev);
202                 return(ENXIO);
203         }
204
205         nsp_release_resource(dev);
206
207         return(0);
208 }
209
210 static int
211 nsp_pccard_attach(DEVPORT_PDEVICE dev)
212 {
213         struct nsp_softc        *sc = device_get_softc(dev);
214         int                     error;
215
216         error = nsp_alloc_resource(dev);
217         if (error) {
218                 return(error);
219         }
220
221         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
222                                nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
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,         nsp_pccard_probe),
246         DEVMETHOD(device_attach,        nsp_pccard_attach),
247         DEVMETHOD(device_detach,        nsp_pccard_detach),
248
249         { 0, 0 }
250 };
251
252 static driver_t nsp_pccard_driver = {
253         "nsp",
254         nsp_pccard_methods,
255         sizeof(struct nsp_softc),
256 };
257
258 static devclass_t nsp_devclass;
259
260 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
261
262 #else
263
264 PCCARD_MODULE(nsp, nsp_card_init,nsp_card_unload, nsp_card_intr,0, cam_imask);
265
266 #endif
267
268 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
269 static struct nsp_softc *
270 nsp_get_softc(int unit)
271 {
272         struct nsp_softc *sc;
273
274         if (unit >= NNSP) {
275                 return(NULL);
276         }
277
278         if (nspdata[unit] == NULL) {
279                 sc = malloc(sizeof(struct nsp_softc), M_TEMP,M_NOWAIT);
280                 if (sc == NULL) {
281                         printf("nsp_get_softc: cannot malloc!\n");
282                         return(NULL);
283                 }
284                 nspdata[unit] = sc;
285         } else {
286                 sc = nspdata[unit];
287         }
288
289         return(sc);
290 }
291
292 static  int
293 nsp_card_init(DEVPORT_PDEVICE devi)
294 {
295         int unit = DEVPORT_PDEVUNIT(devi);
296
297         if (NNSP <= unit)
298                 return (ENODEV);
299
300         if (nspprobe(devi) == 0)
301                 return (ENXIO);
302
303         if (nspattach(devi) == 0)
304                 return (ENXIO);
305         
306         return (0);
307 }
308
309 static  int
310 nsp_card_intr(DEVPORT_PDEVICE devi)
311 {
312         nspintr(DEVPORT_PDEVGET_SOFTC(devi));
313         return 1;
314 }
315 #endif
316
317 static  void
318 nsp_card_unload(DEVPORT_PDEVICE devi)
319 {
320         struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
321         intrmask_t s;
322
323         printf("%s: unload\n",sc->sc_sclow.sl_xname);
324         s = splcam();
325         scsi_low_deactivate((struct scsi_low_softc *)sc);
326         scsi_low_dettach(&sc->sc_sclow);
327         splx(s);
328 }
329
330 static  int
331 nspprobe(DEVPORT_PDEVICE devi)
332 {
333         int rv;
334 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
335         struct nsp_softc *sc = device_get_softc(devi);
336
337         rv = nspprobesubr(rman_get_bustag(sc->port_res),
338                           rman_get_bushandle(sc->port_res),
339                           DEVPORT_PDEVFLAGS(devi));
340 #else
341         rv = nspprobesubr(I386_BUS_SPACE_IO,
342                           DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi));
343 #endif
344
345         return rv;
346 }
347
348 static  int
349 nspattach(DEVPORT_PDEVICE devi)
350 {
351 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
352         int unit = DEVPORT_PDEVUNIT(devi);
353 #endif
354         struct nsp_softc *sc;
355         struct scsi_low_softc *slp;
356         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
357         u_int   iobase = DEVPORT_PDEVIOBASE(devi);
358         intrmask_t s;
359         char    dvname[16];
360
361         strcpy(dvname,"nsp");
362
363 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
364         if (unit >= NNSP)
365         {
366                 printf("%s: unit number too high\n",dvname);
367                 return(0);
368         }
369 #endif
370
371         if (iobase == 0)
372         {
373                 printf("%s: no ioaddr is given\n", dvname);
374                 return (0);
375         }
376
377         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
378         if (sc == NULL) {
379                 return (0);
380         }
381
382         slp = &sc->sc_sclow;
383 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
384         slp->sl_dev = devi;
385         sc->sc_iot = rman_get_bustag(sc->port_res);
386         sc->sc_ioh = rman_get_bushandle(sc->port_res);
387 #else
388         bzero(sc, sizeof(struct nsp_softc));
389         strcpy(slp->sl_dev.dv_xname, dvname);
390         slp->sl_dev.dv_unit = unit;
391         sc->sc_iot = I386_BUS_SPACE_IO;
392         sc->sc_ioh = iobase;
393 #endif
394
395         if((flags & PIO_MODE) == 0) {
396 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
397                 sc->sc_memt = rman_get_bustag(sc->mem_res);
398                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
399 #else
400                 sc->sc_memt = I386_BUS_SPACE_MEM;
401                 sc->sc_memh = (bus_space_handle_t)DEVPORT_PDEVMADDR(devi);
402 #endif
403         } else {
404                 sc->sc_memh = 0;
405         }
406         /* slp->sl_irq = devi->pd_irq; */
407         sc->sc_iclkdiv = CLKDIVR_20M;
408         sc->sc_clkdiv = CLKDIVR_40M;
409
410         slp->sl_hostid = NSP_HOSTID;
411         slp->sl_cfgflags = flags;
412
413         s = splcam();
414         nspattachsubr(sc);
415         splx(s);
416
417         return(NSP_IOSIZE);
418 }
419 #endif /* NCARD>0 */