Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / disk / ncv / ncr53c500_pccard.c
CommitLineData
984263bc 1/* $FreeBSD: src/sys/dev/ncv/ncr53c500_pccard.c,v 1.2.2.5 2001/12/17 13:30:18 non Exp $ */
1f7ab7c9 2/* $DragonFly: src/sys/dev/disk/ncv/ncr53c500_pccard.c,v 1.11 2006/10/25 20:55:53 dillon Exp $ */
984263bc
MD
3/* $NecBSD: ncr53c500_pisa.c,v 1.28 1998/11/26 01:59:11 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) 1995, 1996, 1997, 1998
13 * NetBSD/pc98 porting staff. All rights reserved.
14 * Copyright (c) 1995, 1996, 1997, 1998
15 * Naofumi HONDA. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/disklabel.h>
984263bc
MD
44#include <sys/buf.h>
45#include <sys/queue.h>
46#include <sys/malloc.h>
47#include <sys/errno.h>
1f7ab7c9 48#include <sys/bus.h>
2c9868e4 49#include <sys/thread2.h>
984263bc
MD
50
51#include <vm/vm.h>
52
984263bc
MD
53#include <machine/dvcfg.h>
54
55#include <sys/device_port.h>
56
57f7b5a3
JS
57#include <bus/pccard/pccarddevs.h>
58#include <bus/pccard/pccardvar.h>
59
1f2de5d4
MD
60#include <bus/cam/scsi/scsi_low.h>
61#include <bus/cam/scsi/scsi_low_pisa.h>
984263bc 62
1f2de5d4
MD
63#include "ncr53c500reg.h"
64#include "ncr53c500hw.h"
65#include "ncr53c500var.h"
984263bc
MD
66
67#define KME_KXLC004_01 0x100
68#define OFFSET_KME_KXLC004_01 0x10
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
MD
74
75static int ncvprobe(DEVPORT_PDEVICE devi);
76static int ncvattach(DEVPORT_PDEVICE devi);
77
38e94a25 78static void ncv_card_unload (DEVPORT_PDEVICE);
984263bc 79
57f7b5a3
JS
80static const struct ncv_product {
81 struct pccard_product prod;
82 int flags;
83} ncv_products[] = {
84 { PCMCIA_CARD(EPSON, SC200, 0), 0},
85 { PCMCIA_CARD(PANASONIC, KXLC002, 0), 0xb4d00000 },
86 { PCMCIA_CARD(PANASONIC, KXLC004, 0), 0xb4d00100 },
87 { PCMCIA_CARD(MACNICA, MPS100, 0), 0xb6250000 },
88 { PCMCIA_CARD(MACNICA, MPS110, 0), 0 },
89 { PCMCIA_CARD(NEC, PC9801N_J03R, 0), 0 },
90 { PCMCIA_CARD(NEWMEDIA, BASICS_SCSI, 0), 0 },
91 { PCMCIA_CARD(QLOGIC, PC05, 0), 0x84d00000 },
92#define FLAGS_REX5572 0x84d00000
93 { PCMCIA_CARD(RATOC, REX5572, 0), FLAGS_REX5572 },
94 { PCMCIA_CARD(RATOC, REX9530, 0), 0x84d00000 },
95 { { NULL }, 0 }
96};
97
984263bc
MD
98/*
99 * Additional code for FreeBSD new-bus PCCard frontend
100 */
101
102static void
103ncv_pccard_intr(void * arg)
104{
105 ncvintr(arg);
106}
107
108static void
109ncv_release_resource(DEVPORT_PDEVICE dev)
110{
111 struct ncv_softc *sc = device_get_softc(dev);
112
113 if (sc->ncv_intrhand) {
114 bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand);
115 }
116
117 if (sc->port_res) {
118 bus_release_resource(dev, SYS_RES_IOPORT,
119 sc->port_rid, sc->port_res);
120 }
121
122 if (sc->port_res_dmy) {
123 bus_release_resource(dev, SYS_RES_IOPORT,
124 sc->port_rid_dmy, sc->port_res_dmy);
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
138static int
139ncv_alloc_resource(DEVPORT_PDEVICE dev)
140{
141 struct ncv_softc *sc = device_get_softc(dev);
142 u_int32_t flags = DEVPORT_PDEVFLAGS(dev);
143 u_long ioaddr, iosize, maddr, msize;
144 int error;
145 bus_addr_t offset = 0;
146
147 if(flags & KME_KXLC004_01)
148 offset = OFFSET_KME_KXLC004_01;
149
150 error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
151 if (error || (iosize < (offset + NCVIOSZ))) {
152 return(ENOMEM);
153 }
154
155 sc->port_rid = 0;
156 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
157 ioaddr+offset, ioaddr+iosize-offset,
158 iosize-offset, RF_ACTIVE);
159 if (sc->port_res == NULL) {
160 ncv_release_resource(dev);
161 return(ENOMEM);
162 }
163
164 if (offset != 0) {
165 sc->port_rid_dmy = 0;
166 sc->port_res_dmy = bus_alloc_resource(dev, SYS_RES_IOPORT,
167 &sc->port_rid_dmy,
168 ioaddr, ioaddr+offset, offset,
169 RF_ACTIVE);
170 if (sc->port_res_dmy == NULL) {
171 printf("Warning: cannot allocate IOPORT partially.\n");
172 }
173 } else {
174 sc->port_rid_dmy = 0;
175 sc->port_res_dmy = NULL;
176 }
177
178 sc->irq_rid = 0;
179 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
180 0, ~0, 1, RF_ACTIVE);
181 if (sc->irq_res == NULL) {
182 ncv_release_resource(dev);
183 return(ENOMEM);
184 }
185
186 error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
187 if (error) {
188 return(0); /* XXX */
189 }
190
191 /* no need to allocate memory if not configured */
192 if (maddr == 0 || msize == 0) {
193 return(0);
194 }
195
196 sc->mem_rid = 0;
197 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
198 0, ~0, 1, RF_ACTIVE);
199 if (sc->mem_res == NULL) {
200 ncv_release_resource(dev);
201 return(ENOMEM);
202 }
203
204 return(0);
205}
206
57f7b5a3
JS
207static int ncv_pccard_match(device_t dev)
208{
209 const struct ncv_product *pp;
210 const char *vendorstr;
211 const char *prodstr;
212
213 if ((pp = (const struct ncv_product *) pccard_product_lookup(dev,
214 (const struct pccard_product *) ncv_products,
215 sizeof(ncv_products[0]), NULL)) != NULL) {
216 if (pp->prod.pp_name != NULL)
217 device_set_desc(dev, pp->prod.pp_name);
218 device_set_flags(dev, pp->flags);
219 return(0);
220 }
221 vendorstr = pccard_get_vendor_str(dev);
222 prodstr = pccard_get_product_str(dev);
223 if (strcmp(vendorstr, "RATOC System Inc.") == 0 &&
224 strncmp(prodstr, "SOUND/SCSI2 CARD", 16) == 0) {
225 device_set_desc(dev, "RATOC REX-5572");
226 device_set_flags(dev, FLAGS_REX5572);
227 return (0);
228 }
229 return(EIO);
230}
231
984263bc
MD
232static int
233ncv_pccard_probe(DEVPORT_PDEVICE dev)
234{
235 struct ncv_softc *sc = device_get_softc(dev);
236 int error;
237
238 bzero(sc, sizeof(struct ncv_softc));
239
240 error = ncv_alloc_resource(dev);
241 if (error) {
242 return(error);
243 }
244
245 if (ncvprobe(dev) == 0) {
246 ncv_release_resource(dev);
247 return(ENXIO);
248 }
249
250 ncv_release_resource(dev);
251
252 return(0);
253}
254
255static int
256ncv_pccard_attach(DEVPORT_PDEVICE dev)
257{
258 struct ncv_softc *sc = device_get_softc(dev);
259 int error;
260
261 error = ncv_alloc_resource(dev);
262 if (error) {
263 return(error);
264 }
265
ee61f228 266 error = bus_setup_intr(dev, sc->irq_res, 0,
e9cb6d99
MD
267 ncv_pccard_intr, (void *)sc,
268 &sc->ncv_intrhand, NULL);
984263bc
MD
269 if (error) {
270 ncv_release_resource(dev);
271 return(error);
272 }
273
274 if (ncvattach(dev) == 0) {
275 ncv_release_resource(dev);
276 return(ENXIO);
277 }
278
279 return(0);
280}
281
282static void
283ncv_pccard_detach(DEVPORT_PDEVICE dev)
284{
285 ncv_card_unload(dev);
286 ncv_release_resource(dev);
287}
288
289static device_method_t ncv_pccard_methods[] = {
290 /* Device interface */
57f7b5a3
JS
291 DEVMETHOD(device_probe, pccard_compat_probe),
292 DEVMETHOD(device_attach, pccard_compat_attach),
984263bc
MD
293 DEVMETHOD(device_detach, ncv_pccard_detach),
294
57f7b5a3
JS
295 /* Card interface */
296 DEVMETHOD(card_compat_match, ncv_pccard_match),
297 DEVMETHOD(card_compat_probe, ncv_pccard_probe),
298 DEVMETHOD(card_compat_attach, ncv_pccard_attach),
299
984263bc
MD
300 { 0, 0 }
301};
302
303static driver_t ncv_pccard_driver = {
304 "ncv",
305 ncv_pccard_methods,
306 sizeof(struct ncv_softc),
307};
308
309static devclass_t ncv_devclass;
310
311MODULE_DEPEND(ncv, scsi_low, 1, 1, 1);
312DRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, 0, 0);
313
314static void
315ncv_card_unload(DEVPORT_PDEVICE devi)
316{
317 struct ncv_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
984263bc
MD
318
319 printf("%s: unload\n", sc->sc_sclow.sl_xname);
2c9868e4 320 crit_enter();
984263bc
MD
321 scsi_low_deactivate((struct scsi_low_softc *)sc);
322 scsi_low_dettach(&sc->sc_sclow);
2c9868e4 323 crit_exit();
984263bc
MD
324}
325
326static int
327ncvprobe(DEVPORT_PDEVICE devi)
328{
329 int rv;
330 struct ncv_softc *sc = device_get_softc(devi);
331 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
332
333 rv = ncvprobesubr(rman_get_bustag(sc->port_res),
334 rman_get_bushandle(sc->port_res),
335 flags, NCV_HOSTID);
336
337 return rv;
338}
339
340static int
341ncvattach(DEVPORT_PDEVICE devi)
342{
343 struct ncv_softc *sc;
344 struct scsi_low_softc *slp;
345 u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
984263bc
MD
346 char dvname[16]; /* SCSI_LOW_DVNAME_LEN */
347
348 strcpy(dvname, "ncv");
349
350 sc = DEVPORT_PDEVALLOC_SOFTC(devi);
351 if (sc == NULL) {
352 return(0);
353 }
354
355 slp = &sc->sc_sclow;
356 slp->sl_dev = devi;
357 sc->sc_iot = rman_get_bustag(sc->port_res);
358 sc->sc_ioh = rman_get_bushandle(sc->port_res);
359
360 slp->sl_hostid = NCV_HOSTID;
361 slp->sl_cfgflags = flags;
362
2c9868e4 363 crit_enter();
984263bc 364 ncvattachsubr(sc);
2c9868e4 365 crit_exit();
984263bc
MD
366
367 return(NCVIOSZ);
368}