Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / stg / tmc18c30_pccard.c
1 /*      $FreeBSD: src/sys/dev/stg/tmc18c30_pccard.c,v 1.2.2.6 2001/12/17 13:30:19 non Exp $     */
2 /*      $NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda 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) 1996, 1997, 1998
12  *      NetBSD/pc98 porting staff. All rights reserved.
13  *  Copyright (c) 1996, 1997, 1998
14  *      Naofumi HONDA. All rights reserved.
15  *  Copyright (c) 1996, 1997, 1998
16  *      Kouichi Matsuda. All rights reserved.
17  * 
18  *  Redistribution and use in source and binary forms, with or without
19  *  modification, are permitted provided that the following conditions
20  *  are met:
21  *  1. Redistributions of source code must retain the above copyright
22  *     notice, this list of conditions and the following disclaimer.
23  *  2. Redistributions in binary form must reproduce the above copyright
24  *     notice, this list of conditions and the following disclaimer in the
25  *     documentation and/or other materials provided with the distribution.
26  *  3. The name of the author may not be used to endorse or promote products
27  *     derived from this software without specific prior written permission.
28  * 
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/disklabel.h>
45 #if defined(__FreeBSD__) && __FreeBSD_version >= 500001
46 #include <sys/bio.h>
47 #endif
48 #include <sys/buf.h>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
52
53 #include <vm/vm.h>
54
55 #include <machine/bus.h>
56 #include <machine/bus_pio.h>
57 #include <machine/dvcfg.h>
58
59 #include <sys/device_port.h>
60
61 #include <cam/scsi/scsi_low.h>
62 #include <cam/scsi/scsi_low_pisa.h>
63
64 #include <dev/stg/tmc18c30reg.h>
65 #include <dev/stg/tmc18c30var.h>
66
67 #define STG_HOSTID      7
68
69 #include        <sys/kernel.h>
70 #include        <sys/module.h>
71 #if !defined(__FreeBSD__) || __FreeBSD_version < 500014
72 #include        <sys/select.h>
73 #endif
74 #include        <pccard/cardinfo.h>
75 #include        <pccard/slot.h>
76
77 static  int     stgprobe(DEVPORT_PDEVICE devi);
78 static  int     stgattach(DEVPORT_PDEVICE devi);
79
80 static  void    stg_card_unload __P((DEVPORT_PDEVICE));
81
82 /*
83  * Additional code for FreeBSD new-bus PCCard frontend
84  */
85
86 static void
87 stg_pccard_intr(void * arg)
88 {
89         stgintr(arg);
90 }
91
92 static void
93 stg_release_resource(DEVPORT_PDEVICE dev)
94 {
95         struct stg_softc        *sc = device_get_softc(dev);
96
97         if (sc->stg_intrhand) {
98                 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
99         }
100
101         if (sc->port_res) {
102                 bus_release_resource(dev, SYS_RES_IOPORT,
103                                      sc->port_rid, sc->port_res);
104         }
105
106         if (sc->irq_res) {
107                 bus_release_resource(dev, SYS_RES_IRQ,
108                                      sc->irq_rid, sc->irq_res);
109         }
110
111         if (sc->mem_res) {
112                 bus_release_resource(dev, SYS_RES_MEMORY,
113                                      sc->mem_rid, sc->mem_res);
114         }
115 }
116
117 static int
118 stg_alloc_resource(DEVPORT_PDEVICE dev)
119 {
120         struct stg_softc        *sc = device_get_softc(dev);
121         u_long                  ioaddr, iosize, maddr, msize;
122         int                     error;
123
124         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
125         if (error || iosize < STGIOSZ) {
126                 return(ENOMEM);
127         }
128
129         sc->port_rid = 0;
130         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
131                                           0, ~0, STGIOSZ, RF_ACTIVE);
132         if (sc->port_res == NULL) {
133                 stg_release_resource(dev);
134                 return(ENOMEM);
135         }
136
137         sc->irq_rid = 0;
138         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
139                                          0, ~0, 1, RF_ACTIVE);
140         if (sc->irq_res == NULL) {
141                 stg_release_resource(dev);
142                 return(ENOMEM);
143         }
144
145         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
146         if (error) {
147                 return(0);      /* XXX */
148         }
149
150         /* no need to allocate memory if not configured */
151         if (maddr == 0 || msize == 0) {
152                 return(0);
153         }
154
155         sc->mem_rid = 0;
156         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
157                                          0, ~0, 1, RF_ACTIVE);
158         if (sc->mem_res == NULL) {
159                 stg_release_resource(dev);
160                 return(ENOMEM);
161         }
162
163         return(0);
164 }
165
166 static int
167 stg_pccard_probe(DEVPORT_PDEVICE dev)
168 {
169         struct stg_softc        *sc = device_get_softc(dev);
170         int                     error;
171
172         bzero(sc, sizeof(struct stg_softc));
173
174         error = stg_alloc_resource(dev);
175         if (error) {
176                 return(error);
177         }
178
179         if (stgprobe(dev) == 0) {
180                 stg_release_resource(dev);
181                 return(ENXIO);
182         }
183
184         stg_release_resource(dev);
185
186         return(0);
187 }
188
189 static int
190 stg_pccard_attach(DEVPORT_PDEVICE dev)
191 {
192         struct stg_softc        *sc = device_get_softc(dev);
193         int                     error;
194
195         error = stg_alloc_resource(dev);
196         if (error) {
197                 return(error);
198         }
199
200         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM,
201                                stg_pccard_intr, (void *)sc, &sc->stg_intrhand);
202         if (error) {
203                 stg_release_resource(dev);
204                 return(error);
205         }
206
207         if (stgattach(dev) == 0) {
208                 stg_release_resource(dev);
209                 return(ENXIO);
210         }
211
212         return(0);
213 }
214
215 static  void
216 stg_pccard_detach(DEVPORT_PDEVICE dev)
217 {
218         stg_card_unload(dev);
219         stg_release_resource(dev);
220 }
221
222 static device_method_t stg_pccard_methods[] = {
223         /* Device interface */
224         DEVMETHOD(device_probe,         stg_pccard_probe),
225         DEVMETHOD(device_attach,        stg_pccard_attach),
226         DEVMETHOD(device_detach,        stg_pccard_detach),
227
228         { 0, 0 }
229 };
230
231 static driver_t stg_pccard_driver = {
232         "stg",
233         stg_pccard_methods,
234         sizeof(struct stg_softc),
235 };
236
237 static devclass_t stg_devclass;
238
239 MODULE_DEPEND(stg, scsi_low, 1, 1, 1);
240 DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
241
242 static  void
243 stg_card_unload(DEVPORT_PDEVICE devi)
244 {
245         struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
246         intrmask_t s;
247
248         printf("%s: unload\n",sc->sc_sclow.sl_xname);
249         s = splcam();
250         scsi_low_deactivate((struct scsi_low_softc *)sc);
251         scsi_low_dettach(&sc->sc_sclow);
252         splx(s);
253 }
254
255 static  int
256 stgprobe(DEVPORT_PDEVICE devi)
257 {
258         int rv;
259         struct stg_softc *sc = device_get_softc(devi);
260
261         rv = stgprobesubr(rman_get_bustag(sc->port_res),
262                           rman_get_bushandle(sc->port_res),
263                           DEVPORT_PDEVFLAGS(devi));
264
265         return rv;
266 }
267
268 static  int
269 stgattach(DEVPORT_PDEVICE devi)
270 {
271         struct stg_softc *sc;
272         struct scsi_low_softc *slp;
273         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
274         u_int iobase = DEVPORT_PDEVIOBASE(devi);
275         intrmask_t s;
276         char    dvname[16];
277
278         strcpy(dvname,"stg");
279
280         if (iobase == 0)
281         {
282                 printf("%s: no ioaddr is given\n", dvname);
283                 return (0);
284         }
285
286         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
287         if (sc == NULL) {
288                 return(0);
289         }
290
291         slp = &sc->sc_sclow;
292         slp->sl_dev = devi;
293         sc->sc_iot = rman_get_bustag(sc->port_res);
294         sc->sc_ioh = rman_get_bushandle(sc->port_res);
295
296         slp->sl_hostid = STG_HOSTID;
297         slp->sl_cfgflags = flags;
298
299         s = splcam();
300         stgattachsubr(sc);
301         splx(s);
302
303         return(STGIOSZ);
304 }