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