]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/nsp/nsp_pccard.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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
52 #include <sys/bus.h>
53
54 #include <dev/pccard/pccardvar.h>
55
56 #include <cam/scsi/scsi_low.h>
57
58 #include <dev/nsp/nspreg.h>
59 #include <dev/nsp/nspvar.h>
60
61 #define NSP_HOSTID      7
62
63 #include "pccarddevs.h"
64
65 #define PIO_MODE 0x100          /* pd_flags */
66
67 static int nspprobe(device_t devi);
68 static int nspattach(device_t devi);
69
70 static  void    nsp_card_unload (device_t);
71
72 const struct pccard_product nsp_products[] = {
73         PCMCIA_CARD(IODATA3, CBSC16),
74         PCMCIA_CARD(PANASONIC, KME),
75         PCMCIA_CARD(WORKBIT2, NINJA_SCSI3),
76         PCMCIA_CARD(WORKBIT, ULTRA_NINJA_16),
77         { NULL }
78 };
79
80 /*
81  * Additional code for FreeBSD new-bus PC Card frontend
82  */
83
84 static void
85 nsp_pccard_intr(void * arg)
86 {
87         nspintr(arg);
88 }
89
90 static void
91 nsp_release_resource(device_t dev)
92 {
93         struct nsp_softc        *sc = device_get_softc(dev);
94
95         if (sc->nsp_intrhand)
96                 bus_teardown_intr(dev, sc->irq_res, sc->nsp_intrhand);
97         if (sc->port_res)
98                 bus_release_resource(dev, SYS_RES_IOPORT,
99                                      sc->port_rid, sc->port_res);
100         if (sc->irq_res)
101                 bus_release_resource(dev, SYS_RES_IRQ,
102                                      sc->irq_rid, sc->irq_res);
103         if (sc->mem_res)
104                 bus_release_resource(dev, SYS_RES_MEMORY,
105                                      sc->mem_rid, sc->mem_res);
106 }
107
108 static int
109 nsp_alloc_resource(device_t 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_any(dev, SYS_RES_IRQ, &sc->irq_rid,
129                                              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         /* No need to allocate memory if not configured and it's in PIO mode */
140         if (maddr == 0 || msize == 0) {
141                 if ((device_get_flags(dev) & PIO_MODE) == 0) {
142                         printf("Memory window was not configured. Configure or use in PIO mode.");
143                         nsp_release_resource(dev);
144                         return(ENOMEM);
145                 }
146                 /* no need to allocate memory if PIO mode */
147                 return(0);
148         }
149
150         sc->mem_rid = 0;
151         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
152                                              RF_ACTIVE);
153         if (sc->mem_res == NULL) {
154                 nsp_release_resource(dev);
155                 return(ENOMEM);
156         }
157
158         return(0);
159 }
160
161 static int
162 nsp_pccard_probe(device_t dev)
163 {
164         const struct pccard_product *pp;
165
166         if ((pp = pccard_product_lookup(dev, nsp_products,
167             sizeof(nsp_products[0]), NULL)) != NULL) {
168                 if (pp->pp_name)
169                         device_set_desc(dev, pp->pp_name);
170                 return(0);
171         }
172         return(EIO);
173 }
174
175 static int
176 nsp_pccard_attach(device_t dev)
177 {
178         struct nsp_softc        *sc = device_get_softc(dev);
179         int                     error;
180
181         error = nsp_alloc_resource(dev);
182         if (error)
183                 return(error);
184         if (nspprobe(dev) == 0) {
185                 nsp_release_resource(dev);
186                 return(ENXIO);
187         }
188         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM | INTR_ENTROPY,
189                                NULL, nsp_pccard_intr, (void *)sc, &sc->nsp_intrhand);
190         if (error) {
191                 nsp_release_resource(dev);
192                 return(error);
193         }
194         if (nspattach(dev) == 0) {
195                 nsp_release_resource(dev);
196                 return(ENXIO);
197         }
198
199         return(0);
200 }
201
202 static int
203 nsp_pccard_detach(device_t dev)
204 {
205         nsp_card_unload(dev);
206         nsp_release_resource(dev);
207
208         return (0);
209 }
210
211 static device_method_t nsp_pccard_methods[] = {
212         /* Device interface */
213         DEVMETHOD(device_probe,         nsp_pccard_probe),
214         DEVMETHOD(device_attach,        nsp_pccard_attach),
215         DEVMETHOD(device_detach,        nsp_pccard_detach),
216         { 0, 0 }
217 };
218
219 static driver_t nsp_pccard_driver = {
220         "nsp",
221         nsp_pccard_methods,
222         sizeof(struct nsp_softc),
223 };
224
225 static devclass_t nsp_devclass;
226
227 MODULE_DEPEND(nsp, scsi_low, 1, 1, 1);
228 DRIVER_MODULE(nsp, pccard, nsp_pccard_driver, nsp_devclass, 0, 0);
229
230 static void
231 nsp_card_unload(device_t devi)
232 {
233         struct nsp_softc *sc = device_get_softc(devi);
234         intrmask_t s;
235
236         s = splcam();
237         scsi_low_deactivate((struct scsi_low_softc *)sc);
238         scsi_low_dettach(&sc->sc_sclow);
239         splx(s);
240 }
241
242 static  int
243 nspprobe(device_t devi)
244 {
245         int rv;
246         struct nsp_softc *sc = device_get_softc(devi);
247
248         rv = nspprobesubr(rman_get_bustag(sc->port_res),
249                           rman_get_bushandle(sc->port_res),
250                           device_get_flags(devi));
251
252         return rv;
253 }
254
255 static  int
256 nspattach(device_t devi)
257 {
258         struct nsp_softc *sc;
259         struct scsi_low_softc *slp;
260         u_int32_t flags = device_get_flags(devi);
261         u_int   iobase = bus_get_resource_start(devi, SYS_RES_IOPORT, 0);
262         intrmask_t s;
263         char    dvname[16];
264
265         strcpy(dvname,"nsp");
266
267         if (iobase == 0) {
268                 printf("%s: no ioaddr is given\n", dvname);
269                 return (0);
270         }
271
272         sc = device_get_softc(devi);
273         if (sc == NULL)
274                 return (0);
275
276         slp = &sc->sc_sclow;
277         slp->sl_dev = devi;
278         sc->sc_iot = rman_get_bustag(sc->port_res);
279         sc->sc_ioh = rman_get_bushandle(sc->port_res);
280
281         if (sc->mem_res == NULL) {
282                 printf("WARNING: CANNOT GET Memory RESOURCE going PIO mode");
283                 flags |= PIO_MODE;
284         }
285
286         if ((flags & PIO_MODE) == 0) {
287                 sc->sc_memt = rman_get_bustag(sc->mem_res);
288                 sc->sc_memh = rman_get_bushandle(sc->mem_res);
289         } else {
290                 sc->sc_memh = 0;
291         }
292         /* slp->sl_irq = devi->pd_irq; */
293         sc->sc_iclkdiv = CLKDIVR_20M;
294         sc->sc_clkdiv = CLKDIVR_40M;
295
296         slp->sl_hostid = NSP_HOSTID;
297         slp->sl_cfgflags = flags;
298
299         s = splcam();
300         nspattachsubr(sc);
301         splx(s);
302
303         return(NSP_IOSIZE);
304 }