]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nsp/nsp_pccard.c
This commit was generated by cvs2svn to compensate for changes in r69830,
[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
77 #include "apm.h"
78 #if NAPM > 0
79 #include        <machine/apm_bios.h>
80 #endif
81
82 #include        "card.h"
83 #if NCARD > 0
84 #include        <sys/kernel.h>
85 #include        <sys/module.h>
86 #include        <sys/select.h>
87 #include        <pccard/cardinfo.h>
88 #include        <pccard/slot.h>
89
90 #define PIO_MODE 0x100          /* pd_flags */
91
92 static int nspprobe(DEVPORT_PDEVICE devi);
93 static int nspattach(DEVPORT_PDEVICE devi);
94
95 static  int     nsp_card_intr   __P((DEVPORT_PDEVICE));
96 static  void    nsp_card_unload __P((DEVPORT_PDEVICE));
97 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
98 static  int     nsp_card_init   __P((DEVPORT_PDEVICE));
99 #endif
100
101 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
102 /*
103  * Additional code for FreeBSD new-bus PCCard frontend
104  */
105
106 static void
107 nsp_pccard_intr(void * arg)
108 {
109         nspintr(arg);
110 }
111
112 static void
113 nsp_release_resource(DEVPORT_PDEVICE dev)
114 {
115         struct nsp_softc        *sc = device_get_softc(dev);
116
117         if (sc->nsp_intrhand) {
118                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
119         }
120
121         if (sc->port_res) {
122                 bus_release_resource(dev, SYS_RES_IOPORT,
123                                      sc->port_rid, sc->port_res);
124         }
125
126         if (sc->irq_res) {
127                 bus_release_resource(dev, SYS_RES_IRQ,
128                                      sc->irq_rid, sc->irq_res);
129         }
130
131         if (sc->mem_res) {
132                 bus_release_resource(dev, SYS_RES_MEMORY,
133                                      sc->mem_rid, sc->mem_res);
134         }
135 }
136
137 static int
138 nsp_alloc_resource(DEVPORT_PDEVICE dev)
139 {
140         struct nsp_softc        *sc = device_get_softc(dev);
141         u_long                  maddr, msize;
142         int                     error;
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         /* no need to allocate memory if PIO mode */
161         if ((DEVPORT_PDEVFLAGS(dev) & PIO_MODE) != 0) {
162                 return(0);
163         }
164
165         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
166         if (error) {
167                 return(0);      /* XXX */
168         }
169
170         /* no need to allocate memory if not configured */
171         if (maddr == 0 || msize == 0) {
172                 return(0);
173         }
174
175         sc->mem_rid = 0;
176         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
177                                          0, ~0, msize, RF_ACTIVE);
178         if (sc->mem_res == NULL) {
179                 nsp_release_resource(dev);
180                 return(ENOMEM);
181         }
182
183         return(0);
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, INTR_TYPE_CAM,
221                                nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
222         if (error) {
223                 nsp_release_resource(dev);
224                 return(error);
225         }
226
227         if (nspattach(dev) == 0) {
228                 nsp_release_resource(dev);
229                 return(ENXIO);
230         }
231
232         return(0);
233 }
234
235 static  void
236 nsp_pccard_detach(DEVPORT_PDEVICE dev)
237 {
238         nsp_card_unload(dev);
239         nsp_release_resource(dev);
240 }
241
242 static device_method_t nsp_pccard_methods[] = {
243         /* Device interface */
244         DEVMETHOD(device_probe,         nsp_pccard_probe),
245         DEVMETHOD(device_attach,        nsp_pccard_attach),
246         DEVMETHOD(device_detach,        nsp_pccard_detach),
247
248         { 0, 0 }
249 };
250
251 static driver_t nsp_pccard_driver = {
252         "nsp",
253         nsp_pccard_methods,
254         sizeof(struct nsp_softc),
255 };
256
257 static devclass_t nsp_devclass;
258
259 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
260
261 #else
262
263 PCCARD_MODULE(nsp, nsp_card_init,nsp_card_unload, nsp_card_intr,0, cam_imask);
264
265 #endif
266
267 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
268 static struct nsp_softc *
269 nsp_get_softc(int unit)
270 {
271         struct nsp_softc *sc;
272
273         if (unit >= NNSP) {
274                 return(NULL);
275         }
276
277         if (nspdata[unit] == NULL) {
278                 sc = malloc(sizeof(struct nsp_softc), M_TEMP,M_NOWAIT);
279                 if (sc == NULL) {
280                         printf("nsp_get_softc: cannot malloc!\n");
281                         return(NULL);
282                 }
283                 nspdata[unit] = sc;
284         } else {
285                 sc = nspdata[unit];
286         }
287
288         return(sc);
289 }
290
291 static  int
292 nsp_card_init(DEVPORT_PDEVICE devi)
293 {
294         int unit = DEVPORT_PDEVUNIT(devi);
295
296         if (NNSP <= unit)
297                 return (ENODEV);
298
299         if (nspprobe(devi) == 0)
300                 return (ENXIO);
301
302         if (nspattach(devi) == 0)
303                 return (ENXIO);
304         
305         return (0);
306 }
307 #endif
308
309 static  void
310 nsp_card_unload(DEVPORT_PDEVICE devi)
311 {
312         struct nsp_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
313
314         printf("%s: unload\n",sc->sc_sclow.sl_xname);
315         scsi_low_deactivate((struct scsi_low_softc *)sc);
316         scsi_low_dettach(&sc->sc_sclow);
317 }
318
319 static  int
320 nsp_card_intr(DEVPORT_PDEVICE devi)
321 {
322         nspintr(DEVPORT_PDEVGET_SOFTC(devi));
323         return 1;
324 }
325
326 static  int
327 nspprobe(DEVPORT_PDEVICE devi)
328 {
329         int rv;
330 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
331         struct nsp_softc *sc = device_get_softc(devi);
332
333         rv = nspprobesubr(rman_get_bustag(sc->port_res),
334                           rman_get_bushandle(sc->port_res),
335                           DEVPORT_PDEVFLAGS(devi));
336 #else
337         rv = nspprobesubr(I386_BUS_SPACE_IO,
338                           DEVPORT_PDEVIOBASE(devi), DEVPORT_PDEVFLAGS(devi));
339 #endif
340
341         return rv;
342 }
343
344 static  int
345 nspattach(DEVPORT_PDEVICE devi)
346 {
347 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
348         int unit = DEVPORT_PDEVUNIT(devi);
349 #endif
350         struct nsp_softc *sc;
351         struct scsi_low_softc *slp;
352         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
353         u_int   iobase = DEVPORT_PDEVIOBASE(devi);
354         char    dvname[16];
355
356         strcpy(dvname,"nsp");
357
358 #if defined(__FreeBSD__) && __FreeBSD_version < 400001
359         if (unit >= NNSP)
360         {
361                 printf("%s: unit number too high\n",dvname);
362                 return(0);
363         }
364 #endif
365
366         if (iobase == 0)
367         {
368                 printf("%s: no ioaddr is given\n", dvname);
369                 return (0);
370         }
371
372         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
373         if (sc == NULL) {
374                 return (0);
375         }
376
377         slp = &sc->sc_sclow;
378 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
379         slp->sl_dev = devi;
380         sc->sc_iot = rman_get_bustag(sc->port_res);
381         sc->sc_ioh = rman_get_bushandle(sc->port_res);
382 #else
383         bzero(sc, sizeof(struct nsp_softc));
384         strcpy(slp->sl_dev.dv_xname, dvname);
385         slp->sl_dev.dv_unit = unit;
386         sc->sc_iot = I386_BUS_SPACE_IO;
387         sc->sc_ioh = iobase;
388 #endif
389
390         if((flags & PIO_MODE) == 0) {
391 #if defined(__FreeBSD__) && __FreeBSD_version >= 400001
392                 sc->sc_memt = rman_get_bustag(sc->mem_res);
393                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
394 #else
395                 sc->sc_memt = I386_BUS_SPACE_MEM;
396                 sc->sc_memh = (bus_space_handle_t)DEVPORT_PDEVMADDR(devi);
397 #endif
398         } else {
399                 sc->sc_memh = 0;
400         }
401         /* slp->sl_irq = devi->pd_irq; */
402         sc->sc_iclkdiv = CLKDIVR_20M;
403         sc->sc_clkdiv = CLKDIVR_40M;
404
405         slp->sl_hostid = NSP_HOSTID;
406         slp->sl_cfgflags = flags;
407
408         nspattachsubr(sc);
409
410         sc->sc_ih = nspintr;
411
412         return(NSP_IOSIZE);
413 }
414 #endif /* NCARD>0 */