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