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