]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nsp/nsp_pccard.c
Remove NetBSD compat shims for drivers originally shared with NetBSD/pc98.
[FreeBSD/FreeBSD.git] / sys / dev / nsp / nsp_pccard.c
1 /*      $NecBSD: nsp_pisa.c,v 1.4 1999/04/15 01:35:54 kmatsuda Exp $    */
2 /*      $NetBSD$        */
3
4 /*-
5  * [Ported for FreeBSD]
6  *  Copyright (c) 2000
7  *      Noriaki Mitsunaga, Mitsuru Iwasaki and Takanori Watanabe.
8  *      All rights reserved.
9  * [NetBSD for NEC PC-98 series]
10  *  Copyright (c) 1998
11  *      NetBSD/pc98 porting staff. All rights reserved.
12  * 
13  *  Redistribution and use in source and binary forms, with or without
14  *  modification, are permitted provided that the following conditions
15  *  are met:
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. The name of the author may not be used to endorse or promote products
22  *     derived from this software without specific prior written permission.
23  * 
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51 #include <compat/netbsd/dvcfg.h>
52
53 #include <sys/bus.h>
54
55 #include <dev/pccard/pccardvar.h>
56
57 #include <cam/scsi/scsi_low.h>
58 #include <cam/scsi/scsi_low_pisa.h>
59
60 #include <dev/nsp/nspreg.h>
61 #include <dev/nsp/nspvar.h>
62
63 #define NSP_HOSTID      7
64
65 #include "pccarddevs.h"
66
67 #define PIO_MODE 0x100          /* pd_flags */
68
69 static int nspprobe(device_t devi);
70 static int nspattach(device_t devi);
71
72 static  void    nsp_card_unload (device_t);
73
74 const struct pccard_product nsp_products[] = {
75         PCMCIA_CARD(IODATA3, CBSC16),
76         PCMCIA_CARD(PANASONIC, KME),
77         PCMCIA_CARD(WORKBIT2, NINJA_SCSI3),
78         PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16),
79         { NULL }
80 };
81
82 /*
83  * Additional code for FreeBSD new-bus PC Card frontend
84  */
85
86 static void
87 nsp_pccard_intr(void * arg)
88 {
89         nspintr(arg);
90 }
91
92 static void
93 nsp_release_resource(device_t dev)
94 {
95         struct nsp_softc        *sc = device_get_softc(dev);
96
97         if (sc->nsp_intrhand)
98                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
99         if (sc->port_res)
100                 bus_release_resource(dev, SYS_RES_IOPORT,
101                                      sc->port_rid, sc->port_res);
102         if (sc->irq_res)
103                 bus_release_resource(dev, SYS_RES_IRQ,
104                                      sc->irq_rid, sc->irq_res);
105         if (sc->mem_res)
106                 bus_release_resource(dev, SYS_RES_MEMORY,
107                                      sc->mem_rid, sc->mem_res);
108 }
109
110 static int
111 nsp_alloc_resource(device_t dev)
112 {
113         struct nsp_softc        *sc = device_get_softc(dev);
114         u_long                  ioaddr, iosize, maddr, msize;
115         int                     error;
116
117         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
118         if (error || iosize < NSP_IOSIZE)
119                 return(ENOMEM);
120
121         sc->port_rid = 0;
122         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
123                                           0, ~0, NSP_IOSIZE, RF_ACTIVE);
124         if (sc->port_res == NULL) {
125                 nsp_release_resource(dev);
126                 return(ENOMEM);
127         }
128
129         sc->irq_rid = 0;
130         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
131                                              RF_ACTIVE);
132         if (sc->irq_res == NULL) {
133                 nsp_release_resource(dev);
134                 return(ENOMEM);
135         }
136
137         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
138         if (error)
139                 return(0);      /* XXX */
140
141         /* No need to allocate memory if not configured and it's in PIO mode */
142         if (maddr == 0 || msize == 0) {
143                 if ((device_get_flags(dev) & PIO_MODE) == 0) {
144                         printf("Memory window was not configured. Configure or use in PIO mode.");
145                         nsp_release_resource(dev);
146                         return(ENOMEM);
147                 }
148                 /* no need to allocate memory if PIO mode */
149                 return(0);
150         }
151
152         sc->mem_rid = 0;
153         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
154                                              RF_ACTIVE);
155         if (sc->mem_res == NULL) {
156                 nsp_release_resource(dev);
157                 return(ENOMEM);
158         }
159
160         return(0);
161 }
162
163 static int
164 nsp_pccard_probe(device_t dev)
165 {
166         const struct pccard_product *pp;
167
168         if ((pp = pccard_product_lookup(dev, nsp_products,
169             sizeof(nsp_products[0]), NULL)) != NULL) {
170                 if (pp->pp_name)
171                         device_set_desc(dev, pp->pp_name);
172                 return(0);
173         }
174         return(EIO);
175 }
176
177 static int
178 nsp_pccard_attach(device_t dev)
179 {
180         struct nsp_softc        *sc = device_get_softc(dev);
181         int                     error;
182
183         error = nsp_alloc_resource(dev);
184         if (error)
185                 return(error);
186         if (nspprobe(dev) == 0) {
187                 nsp_release_resource(dev);
188                 return(ENXIO);
189         }
190         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
191                                NULL, nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
192         if (error) {
193                 nsp_release_resource(dev);
194                 return(error);
195         }
196         if (nspattach(dev) == 0) {
197                 nsp_release_resource(dev);
198                 return(ENXIO);
199         }
200
201         return(0);
202 }
203
204 static int
205 nsp_pccard_detach(device_t dev)
206 {
207         nsp_card_unload(dev);
208         nsp_release_resource(dev);
209
210         return (0);
211 }
212
213 static device_method_t nsp_pccard_methods[] = {
214         /* Device interface */
215         DEVMETHOD(device_probe,         nsp_pccard_probe),
216         DEVMETHOD(device_attach,        nsp_pccard_attach),
217         DEVMETHOD(device_detach,        nsp_pccard_detach),
218         { 0, 0 }
219 };
220
221 static driver_t nsp_pccard_driver = {
222         "nsp",
223         nsp_pccard_methods,
224         sizeof(struct nsp_softc),
225 };
226
227 static devclass_t nsp_devclass;
228
229 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
230 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
231
232 static void
233 nsp_card_unload(device_t devi)
234 {
235         struct nsp_softc *sc = device_get_softc(devi);
236         intrmask_t s;
237
238         s = splcam();
239         scsi_low_deactivate((struct scsi_low_softc *)sc);
240         scsi_low_dettach(&sc->sc_sclow);
241         splx(s);
242 }
243
244 static  int
245 nspprobe(device_t devi)
246 {
247         int rv;
248         struct nsp_softc *sc = device_get_softc(devi);
249
250         rv = nspprobesubr(rman_get_bustag(sc->port_res),
251                           rman_get_bushandle(sc->port_res),
252                           device_get_flags(devi));
253
254         return rv;
255 }
256
257 static  int
258 nspattach(device_t devi)
259 {
260         struct nsp_softc *sc;
261         struct scsi_low_softc *slp;
262         u_int32_t flags = device_get_flags(devi);
263         u_int   iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
264         intrmask_t s;
265         char    dvname[16];
266
267         strcpy(dvname,"nsp");
268
269         if (iobase == 0) {
270                 printf("%s: no ioaddr is given\n", dvname);
271                 return (0);
272         }
273
274         sc = device_get_softc(devi);
275         if (sc == NULL)
276                 return (0);
277
278         slp = &sc->sc_sclow;
279         slp->sl_dev = devi;
280         sc->sc_iot = rman_get_bustag(sc->port_res);
281         sc->sc_ioh = rman_get_bushandle(sc->port_res);
282
283         if (sc->mem_res == NULL) {
284                 printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode");
285                 flags |= PIO_MODE;
286         }
287
288         if ((flags & PIO_MODE) == 0) {
289                 sc->sc_memt = rman_get_bustag(sc->mem_res);
290                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
291         } else {
292                 sc->sc_memh = 0;
293         }
294         /* slp->sl_irq = devi->pd_irq; */
295         sc->sc_iclkdiv = CLKDIVR_20M;
296         sc->sc_clkdiv = CLKDIVR_40M;
297
298         slp->sl_hostid = NSP_HOSTID;
299         slp->sl_cfgflags = flags;
300
301         s = splcam();
302         nspattachsubr(sc);
303         splx(s);
304
305         return(NSP_IOSIZE);
306 }