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