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