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