kernel: Generate miidevs.h, pccarddevs.h and pcidevs.h on the fly.
[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/pccardvar.h>
57
58 #include "pccarddevs.h"
59
60 #include <bus/cam/scsi/scsi_low.h>
61 #include <bus/cam/scsi/scsi_low_pisa.h>
62
63 #include "ncr53c500reg.h"
64 #include "ncr53c500hw.h"
65 #include "ncr53c500var.h"
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>
72
73 static int ncvprobe(DEVPORT_PDEVICE devi);
74 static int ncvattach(DEVPORT_PDEVICE devi);
75
76 static void     ncv_card_unload (DEVPORT_PDEVICE);
77
78 static const struct ncv_product {
79         struct pccard_product   prod;
80         int flags;
81 } ncv_products[] = {
82         { PCMCIA_CARD(EPSON, SC200, 0), 0}, 
83         { PCMCIA_CARD(PANASONIC, KXLC002, 0), 0xb4d00000 }, 
84         { PCMCIA_CARD(PANASONIC, KXLC004, 0), 0xb4d00100 }, 
85         { PCMCIA_CARD(MACNICA, MPS100, 0), 0xb6250000 }, 
86         { PCMCIA_CARD(MACNICA, MPS110, 0), 0 }, 
87         { PCMCIA_CARD(NEC, PC9801N_J03R, 0), 0 }, 
88         { PCMCIA_CARD(NEWMEDIA, BASICS_SCSI, 0), 0 }, 
89         { PCMCIA_CARD(QLOGIC, PC05, 0), 0x84d00000 }, 
90 #define FLAGS_REX5572 0x84d00000
91         { PCMCIA_CARD(RATOC, REX5572, 0), FLAGS_REX5572 }, 
92         { PCMCIA_CARD(RATOC, REX9530, 0), 0x84d00000 }, 
93         { { NULL }, 0 }
94 };
95
96 /*
97  * Additional code for FreeBSD new-bus PCCard frontend
98  */
99
100 static void
101 ncv_pccard_intr(void * arg)
102 {
103         ncvintr(arg);
104 }
105
106 static void
107 ncv_release_resource(DEVPORT_PDEVICE dev)
108 {
109         struct ncv_softc        *sc = device_get_softc(dev);
110
111         if (sc->ncv_intrhand) {
112                 bus_teardown_intr(dev, sc->irq_res, sc->ncv_intrhand);
113         }
114
115         if (sc->port_res) {
116                 bus_release_resource(dev, SYS_RES_IOPORT,
117                                      sc->port_rid, sc->port_res);
118         }
119
120         if (sc->port_res_dmy) {
121                 bus_release_resource(dev, SYS_RES_IOPORT,
122                                      sc->port_rid_dmy, sc->port_res_dmy);
123         }
124
125         if (sc->irq_res) {
126                 bus_release_resource(dev, SYS_RES_IRQ,
127                                      sc->irq_rid, sc->irq_res);
128         }
129
130         if (sc->mem_res) {
131                 bus_release_resource(dev, SYS_RES_MEMORY,
132                                      sc->mem_rid, sc->mem_res);
133         }
134 }
135
136 static int
137 ncv_alloc_resource(DEVPORT_PDEVICE dev)
138 {
139         struct ncv_softc        *sc = device_get_softc(dev);
140         u_int32_t               flags = DEVPORT_PDEVFLAGS(dev);
141         u_long                  ioaddr, iosize, maddr, msize;
142         int                     error;
143         bus_addr_t              offset = 0;
144
145         if(flags & KME_KXLC004_01)
146                 offset = OFFSET_KME_KXLC004_01;
147
148         error = bus_get_resource(dev, SYS_RES_IOPORT, 0, &ioaddr, &iosize);
149         if (error || (iosize < (offset + NCVIOSZ))) {
150                 return(ENOMEM);
151         }
152
153         sc->port_rid = 0;
154         sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->port_rid,
155                                           ioaddr+offset, ioaddr+iosize-offset,
156                                           iosize-offset, RF_ACTIVE);
157         if (sc->port_res == NULL) {
158                 ncv_release_resource(dev);
159                 return(ENOMEM);
160         }
161
162         if (offset != 0) {
163                 sc->port_rid_dmy = 0;
164                 sc->port_res_dmy = bus_alloc_resource(dev, SYS_RES_IOPORT, 
165                                                 &sc->port_rid_dmy,
166                                                 ioaddr, ioaddr+offset, offset, 
167                                                 RF_ACTIVE);
168                 if (sc->port_res_dmy == NULL) {
169                         kprintf("Warning: cannot allocate IOPORT partially.\n");
170                 }
171         } else {
172                 sc->port_rid_dmy = 0;
173                 sc->port_res_dmy = NULL;
174         }
175
176         sc->irq_rid = 0;
177         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
178                                          0, ~0, 1, RF_ACTIVE);
179         if (sc->irq_res == NULL) {
180                 ncv_release_resource(dev);
181                 return(ENOMEM);
182         }
183
184         error = bus_get_resource(dev, SYS_RES_MEMORY, 0, &maddr, &msize);
185         if (error) {
186                 return(0);      /* XXX */
187         }
188
189         /* no need to allocate memory if not configured */
190         if (maddr == 0 || msize == 0) {
191                 return(0);
192         }
193
194         sc->mem_rid = 0;
195         sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->mem_rid,
196                                          0, ~0, 1, RF_ACTIVE);
197         if (sc->mem_res == NULL) {
198                 ncv_release_resource(dev);
199                 return(ENOMEM);
200         }
201
202         return(0);
203 }
204
205 static int ncv_pccard_match(device_t dev)
206 {
207         const struct ncv_product *pp;
208         const char *vendorstr;
209         const char *prodstr;
210
211         if ((pp = (const struct ncv_product *) pccard_product_lookup(dev, 
212             (const struct pccard_product *) ncv_products,
213             sizeof(ncv_products[0]), NULL)) != NULL) {
214                 if (pp->prod.pp_name != NULL)
215                         device_set_desc(dev, pp->prod.pp_name);
216                 device_set_flags(dev, pp->flags);
217                 return(0);
218         }
219         vendorstr = pccard_get_vendor_str(dev);
220         prodstr = pccard_get_product_str(dev);
221         if (strcmp(vendorstr, "RATOC System Inc.") == 0 &&
222                 strncmp(prodstr, "SOUND/SCSI2 CARD", 16) == 0) {
223                 device_set_desc(dev, "RATOC REX-5572");
224                 device_set_flags(dev, FLAGS_REX5572);
225                 return (0);
226         }
227         return(EIO);
228 }
229
230 static int
231 ncv_pccard_probe(DEVPORT_PDEVICE dev)
232 {
233         struct ncv_softc        *sc = device_get_softc(dev);
234         int                     error;
235
236         bzero(sc, sizeof(struct ncv_softc));
237
238         error = ncv_alloc_resource(dev);
239         if (error) {
240                 return(error);
241         }
242
243         if (ncvprobe(dev) == 0) {
244                 ncv_release_resource(dev);
245                 return(ENXIO);
246         }
247
248         ncv_release_resource(dev);
249
250         return(0);
251 }
252
253 static int
254 ncv_pccard_attach(DEVPORT_PDEVICE dev)
255 {
256         struct ncv_softc        *sc = device_get_softc(dev);
257         int                     error;
258
259         error = ncv_alloc_resource(dev);
260         if (error) {
261                 return(error);
262         }
263
264         error = bus_setup_intr(dev, sc->irq_res, 0,
265                                ncv_pccard_intr, (void *)sc,
266                                &sc->ncv_intrhand, NULL);
267         if (error) {
268                 ncv_release_resource(dev);
269                 return(error);
270         }
271
272         if (ncvattach(dev) == 0) {
273                 ncv_release_resource(dev);
274                 return(ENXIO);
275         }
276
277         return(0);
278 }
279
280 static  void
281 ncv_pccard_detach(DEVPORT_PDEVICE dev)
282 {
283         ncv_card_unload(dev);
284         ncv_release_resource(dev);
285 }
286
287 static device_method_t ncv_pccard_methods[] = {
288         /* Device interface */
289         DEVMETHOD(device_probe,         pccard_compat_probe),
290         DEVMETHOD(device_attach,        pccard_compat_attach),
291         DEVMETHOD(device_detach,        ncv_pccard_detach),
292
293         /* Card interface */
294         DEVMETHOD(card_compat_match,    ncv_pccard_match),
295         DEVMETHOD(card_compat_probe,    ncv_pccard_probe),
296         DEVMETHOD(card_compat_attach,   ncv_pccard_attach),
297
298         DEVMETHOD_END
299 };
300
301 static driver_t ncv_pccard_driver = {
302         "ncv",
303         ncv_pccard_methods,
304         sizeof(struct ncv_softc),
305 };
306
307 static devclass_t ncv_devclass;
308
309 MODULE_DEPEND(ncv, scsi_low, 1, 1, 1);
310 DRIVER_MODULE(ncv, pccard, ncv_pccard_driver, ncv_devclass, NULL, NULL);
311
312 static void
313 ncv_card_unload(DEVPORT_PDEVICE devi)
314 {
315         struct ncv_softc *sc = DEVPORT_PDEVGET_SOFTC(devi);
316
317         kprintf("%s: unload\n", sc->sc_sclow.sl_xname);
318         crit_enter();
319         scsi_low_deactivate((struct scsi_low_softc *)sc);
320         scsi_low_dettach(&sc->sc_sclow);
321         crit_exit();
322 }
323
324 static int
325 ncvprobe(DEVPORT_PDEVICE devi)
326 {
327         int rv;
328         struct ncv_softc *sc = device_get_softc(devi);
329         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
330
331         rv = ncvprobesubr(rman_get_bustag(sc->port_res),
332                           rman_get_bushandle(sc->port_res),
333                           flags, NCV_HOSTID);
334
335         return rv;
336 }
337
338 static int
339 ncvattach(DEVPORT_PDEVICE devi)
340 {
341         struct ncv_softc *sc;
342         struct scsi_low_softc *slp;
343         u_int32_t flags = DEVPORT_PDEVFLAGS(devi);
344         char dvname[16]; /* SCSI_LOW_DVNAME_LEN */
345
346         strcpy(dvname, "ncv");
347
348         sc = DEVPORT_PDEVALLOC_SOFTC(devi);
349         if (sc == NULL) {
350                 return(0);
351         }
352
353         slp = &sc->sc_sclow;
354         slp->sl_dev = devi;
355         sc->sc_iot = rman_get_bustag(sc->port_res);
356         sc->sc_ioh = rman_get_bushandle(sc->port_res);
357
358         slp->sl_hostid = NCV_HOSTID;
359         slp->sl_cfgflags = flags;
360
361         crit_enter();
362         ncvattachsubr(sc);
363         crit_exit();
364
365         return(NCVIOSZ);
366 }