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