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