sys/dev __FreeBSD__ -> __DragonFly__ cleanups.
[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.5 2004/02/12 00:00:18 dillon 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 #include <sys/buf.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/errno.h>
50
51 #include <vm/vm.h>
52
53 #include <machine/bus.h>
54 #include <machine/bus_pio.h>
55 #include <machine/dvcfg.h>
56
57 #include <sys/device_port.h>
58
59 #include <bus/cam/scsi/scsi_low.h>
60 #include <bus/cam/scsi/scsi_low_pisa.h>
61
62 #include "tmc18c30reg.h"
63 #include "tmc18c30var.h"
64
65 #define STG_HOSTID      7
66
67 #include        <sys/kernel.h>
68 #include        <sys/module.h>
69 #include        <bus/pccard/cardinfo.h>
70 #include        <bus/pccard/slot.h>
71
72 static  int     stgprobe(DEVPORT_PDEVICE devi);
73 static  int     stgattach(DEVPORT_PDEVICE devi);
74
75 static  void    stg_card_unload (DEVPORT_PDEVICE);
76
77 /*
78  * Additional code for FreeBSD new-bus PCCard frontend
79  */
80
81 static void
82 stg_pccard_intr(void * arg)
83 {
84         stgintr(arg);
85 }
86
87 static void
88 stg_release_resource(DEVPORT_PDEVICE dev)
89 {
90         struct stg_softc        *sc = device_get_softc(dev);
91
92         if (sc->stg_intrhand) {
93                 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
94         }
95
96         if (sc->port_res) {
97                 bus_release_resource(dev, SYS_RES_IOPORT,
98                                      sc->port_rid, sc->port_res);
99         }
100
101         if (sc->irq_res) {
102                 bus_release_resource(dev, SYS_RES_IRQ,
103                                      sc->irq_rid, sc->irq_res);
104         }
105
106         if (sc->mem_res) {
107                 bus_release_resource(dev, SYS_RES_MEMORY,
108                                      sc->mem_rid, sc->mem_res);
109         }
110 }
111
112 static int
113 stg_alloc_resource(DEVPORT_PDEVICE dev)
114 {
115         struct stg_softc        *sc = device_get_softc(dev);
116         u_long                  ioaddr, iosize, maddr, msize;
117         int                     error;
118
119         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
120         if (error || iosize < STGIOSZ) {
121                 return(ENOMEM);
122         }
123
124         sc->port_rid = 0;
125         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
126                                           0, ~0, STGIOSZ, RF_ACTIVE);
127         if (sc->port_res == NULL) {
128                 stg_release_resource(dev);
129                 return(ENOMEM);
130         }
131
132         sc->irq_rid = 0;
133         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
134                                          0, ~0, 1, RF_ACTIVE);
135         if (sc->irq_res == NULL) {
136                 stg_release_resource(dev);
137                 return(ENOMEM);
138         }
139
140         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
141         if (error) {
142                 return(0);      /* XXX */
143         }
144
145         /* no need to allocate memory if not configured */
146         if (maddr == 0 || msize == 0) {
147                 return(0);
148         }
149
150         sc->mem_rid = 0;
151         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
152                                          0, ~0, 1, RF_ACTIVE);
153         if (sc->mem_res == NULL) {
154                 stg_release_resource(dev);
155                 return(ENOMEM);
156         }
157
158         return(0);
159 }
160
161 static int
162 stg_pccard_probe(DEVPORT_PDEVICE dev)
163 {
164         struct stg_softc        *sc = device_get_softc(dev);
165         int                     error;
166
167         bzero(sc, sizeof(struct stg_softc));
168
169         error = stg_alloc_resource(dev);
170         if (error) {
171                 return(error);
172         }
173
174         if (stgprobe(dev) == 0) {
175                 stg_release_resource(dev);
176                 return(ENXIO);
177         }
178
179         stg_release_resource(dev);
180
181         return(0);
182 }
183
184 static int
185 stg_pccard_attach(DEVPORT_PDEVICE dev)
186 {
187         struct stg_softc        *sc = device_get_softc(dev);
188         int                     error;
189
190         error = stg_alloc_resource(dev);
191         if (error) {
192                 return(error);
193         }
194
195         error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_CAM,
196                                stg_pccard_intr, (void *)sc, &sc->stg_intrhand);
197         if (error) {
198                 stg_release_resource(dev);
199                 return(error);
200         }
201
202         if (stgattach(dev) == 0) {
203                 stg_release_resource(dev);
204                 return(ENXIO);
205         }
206
207         return(0);
208 }
209
210 static  void
211 stg_pccard_detach(DEVPORT_PDEVICE dev)
212 {
213         stg_card_unload(dev);
214         stg_release_resource(dev);
215 }
216
217 static device_method_t stg_pccard_methods[] = {
218         /* Device interface */
219         DEVMETHOD(device_probe,         stg_pccard_probe),
220         DEVMETHOD(device_attach,        stg_pccard_attach),
221         DEVMETHOD(device_detach,        stg_pccard_detach),
222
223         { 0, 0 }
224 };
225
226 static driver_t stg_pccard_driver = {
227         "stg",
228         stg_pccard_methods,
229         sizeof(struct stg_softc),
230 };
231
232 static devclass_t stg_devclass;
233
234 MODULE_DEPEND(stg, scsi_low, 1, 1, 1);
235 DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
236
237 static  void
238 stg_card_unload(DEVPORT_PDEVICE devi)
239 {
240         struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
241         intrmask_t s;
242
243         printf("%s: unload\n",sc->sc_sclow.sl_xname);
244         s = splcam();
245         scsi_low_deactivate((struct scsi_low_softc *)sc);
246         scsi_low_dettach(&sc->sc_sclow);
247         splx(s);
248 }
249
250 static  int
251 stgprobe(DEVPORT_PDEVICE devi)
252 {
253         int rv;
254         struct stg_softc *sc = device_get_softc(devi);
255
256         rv = stgprobesubr(rman_get_bustag(sc->port_res),
257                           rman_get_bushandle(sc->port_res),
258                           DEVPORT_PDEVFLAGS(devi));
259
260         return rv;
261 }
262
263 static  int
264 stgattach(DEVPORT_PDEVICE devi)
265 {
266         struct stg_softc *sc;
267         struct scsi_low_softc *slp;
268         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
269         u_int iobase = DEVPORT_PDEVIOBASE(devi);
270         intrmask_t s;
271         char    dvname[16];
272
273         strcpy(dvname,"stg");
274
275         if (iobase == 0)
276         {
277                 printf("%s: no ioaddr is given\n", dvname);
278                 return (0);
279         }
280
281         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
282         if (sc == NULL) {
283                 return(0);
284         }
285
286         slp = &sc->sc_sclow;
287         slp->sl_dev = devi;
288         sc->sc_iot = rman_get_bustag(sc->port_res);
289         sc->sc_ioh = rman_get_bushandle(sc->port_res);
290
291         slp->sl_hostid = STG_HOSTID;
292         slp->sl_cfgflags = flags;
293
294         s = splcam();
295         stgattachsubr(sc);
296         splx(s);
297
298         return(STGIOSZ);
299 }