More minor IPI work.
[dragonfly/vkernel-mp.git] / sys / dev / disk / stg / tmc18c30_pccard.c
blob22482db504f19ba88a26823637416ceaf5e65027
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.13 2007/05/17 21:08:49 dillon Exp $ */
3 /* $NecBSD: tmc18c30_pisa.c,v 1.22 1998/11/26 01:59:21 honda Exp $ */
4 /* $NetBSD$ */
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.
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.
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.
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>
52 #include <vm/vm.h>
54 #include <sys/device_port.h>
56 #include <bus/pccard/pccarddevs.h>
57 #include <bus/pccard/pccardvar.h>
59 #include <bus/cam/scsi/scsi_low.h>
60 #include <bus/cam/scsi/scsi_low_pisa.h>
62 #include "tmc18c30reg.h"
63 #include "tmc18c30var.h"
65 #define STG_HOSTID 7
67 #include <sys/kernel.h>
68 #include <sys/module.h>
69 #include <bus/pccard/cardinfo.h>
70 #include <bus/pccard/slot.h>
72 static const struct pccard_product stg_products[] = {
73 PCMCIA_CARD(FUTUREDOMAIN, SCSI2GO, 0),
74 PCMCIA_CARD(IBM, SCSICARD, 0),
75 PCMCIA_CARD(RATOC, REX5536, 0),
76 PCMCIA_CARD(RATOC, REX5536AM, 0),
77 PCMCIA_CARD(RATOC, REX5536M, 0),
78 { NULL }
81 static int stgprobe(DEVPORT_PDEVICE devi);
82 static int stgattach(DEVPORT_PDEVICE devi);
84 static void stg_card_unload (DEVPORT_PDEVICE);
87 * Additional code for FreeBSD new-bus PCCard frontend
90 static int stg_pccard_match(device_t dev)
92 const struct pccard_product *pp;
94 if ((pp = pccard_product_lookup(dev, stg_products,
95 sizeof(stg_products[0]), NULL)) != NULL) {
96 if (pp->pp_name != NULL)
97 device_set_desc(dev, pp->pp_name);
98 return(0);
100 return(EIO);
103 static void
104 stg_pccard_intr(void * arg)
106 stgintr(arg);
109 static void
110 stg_release_resource(DEVPORT_PDEVICE dev)
112 struct stg_softc *sc = device_get_softc(dev);
114 if (sc->stg_intrhand) {
115 bus_teardown_intr(dev, sc->irq_res, sc->stg_intrhand);
118 if (sc->port_res) {
119 bus_release_resource(dev, SYS_RES_IOPORT,
120 sc->port_rid, sc->port_res);
123 if (sc->irq_res) {
124 bus_release_resource(dev, SYS_RES_IRQ,
125 sc->irq_rid, sc->irq_res);
128 if (sc->mem_res) {
129 bus_release_resource(dev, SYS_RES_MEMORY,
130 sc->mem_rid, sc->mem_res);
134 static int
135 stg_alloc_resource(DEVPORT_PDEVICE dev)
137 struct stg_softc *sc = device_get_softc(dev);
138 u_long ioaddr, iosize, maddr, msize;
139 int error;
141 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
142 if (error || iosize < STGIOSZ) {
143 return(ENOMEM);
146 sc->port_rid = 0;
147 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
148 0, ~0, STGIOSZ, RF_ACTIVE);
149 if (sc->port_res == NULL) {
150 stg_release_resource(dev);
151 return(ENOMEM);
154 sc->irq_rid = 0;
155 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
156 0, ~0, 1, RF_ACTIVE);
157 if (sc->irq_res == NULL) {
158 stg_release_resource(dev);
159 return(ENOMEM);
162 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
163 if (error) {
164 return(0); /* XXX */
167 /* no need to allocate memory if not configured */
168 if (maddr == 0 || msize == 0) {
169 return(0);
172 sc->mem_rid = 0;
173 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
174 0, ~0, 1, RF_ACTIVE);
175 if (sc->mem_res == NULL) {
176 stg_release_resource(dev);
177 return(ENOMEM);
180 return(0);
183 static int
184 stg_pccard_probe(DEVPORT_PDEVICE dev)
186 struct stg_softc *sc = device_get_softc(dev);
187 int error;
189 bzero(sc, sizeof(struct stg_softc));
191 error = stg_alloc_resource(dev);
192 if (error) {
193 return(error);
196 if (stgprobe(dev) == 0) {
197 stg_release_resource(dev);
198 return(ENXIO);
201 stg_release_resource(dev);
203 return(0);
206 static int
207 stg_pccard_attach(DEVPORT_PDEVICE dev)
209 struct stg_softc *sc = device_get_softc(dev);
210 int error;
212 error = stg_alloc_resource(dev);
213 if (error) {
214 return(error);
217 error = bus_setup_intr(dev, sc->irq_res, 0,
218 stg_pccard_intr, (void *)sc,
219 &sc->stg_intrhand, NULL);
220 if (error) {
221 stg_release_resource(dev);
222 return(error);
225 if (stgattach(dev) == 0) {
226 stg_release_resource(dev);
227 return(ENXIO);
230 return(0);
233 static void
234 stg_pccard_detach(DEVPORT_PDEVICE dev)
236 stg_card_unload(dev);
237 stg_release_resource(dev);
240 static device_method_t stg_pccard_methods[] = {
241 /* Device interface */
242 DEVMETHOD(device_probe, pccard_compat_probe),
243 DEVMETHOD(device_attach, pccard_compat_attach),
244 DEVMETHOD(device_detach, stg_pccard_detach),
246 /* Card interface */
247 DEVMETHOD(card_compat_match, stg_pccard_match),
248 DEVMETHOD(card_compat_probe, stg_pccard_probe),
249 DEVMETHOD(card_compat_attach, stg_pccard_attach),
251 { 0, 0 }
254 static driver_t stg_pccard_driver = {
255 "stg",
256 stg_pccard_methods,
257 sizeof(struct stg_softc),
260 static devclass_t stg_devclass;
262 MODULE_DEPEND(stg, scsi_low, 1, 1, 1);
263 DRIVER_MODULE(stg, pccard, stg_pccard_driver, stg_devclass, 0, 0);
265 static void
266 stg_card_unload(DEVPORT_PDEVICE devi)
268 struct stg_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
270 kprintf("%s: unload\n",sc->sc_sclow.sl_xname);
271 crit_enter();
272 scsi_low_deactivate((struct scsi_low_softc *)sc);
273 scsi_low_dettach(&sc->sc_sclow);
274 crit_exit();
277 static int
278 stgprobe(DEVPORT_PDEVICE devi)
280 int rv;
281 struct stg_softc *sc = device_get_softc(devi);
283 rv = stgprobesubr(rman_get_bustag(sc->port_res),
284 rman_get_bushandle(sc->port_res),
285 DEVPORT_PDEVFLAGS(devi));
287 return rv;
290 static int
291 stgattach(DEVPORT_PDEVICE devi)
293 struct stg_softc *sc;
294 struct scsi_low_softc *slp;
295 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
296 u_int iobase = DEVPORT_PDEVIOBASE(devi);
297 char dvname[16];
299 strcpy(dvname,"stg");
301 if (iobase == 0)
303 kprintf("%s: no ioaddr is given\n", dvname);
304 return (0);
307 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
308 if (sc == NULL) {
309 return(0);
312 slp = &sc->sc_sclow;
313 slp->sl_dev = devi;
314 sc->sc_iot = rman_get_bustag(sc->port_res);
315 sc->sc_ioh = rman_get_bushandle(sc->port_res);
317 slp->sl_hostid = STG_HOSTID;
318 slp->sl_cfgflags = flags;
320 crit_enter();
321 stgattachsubr(sc);
322 crit_exit();
324 return(STGIOSZ);