Merge from vendor branch FILE:
[dragonfly.git] / sys / net / i4b / capi / iavc / iavc_pci.c
1 /*
2  * Copyright (c) 2001 Cubical Solutions Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * capi/iavc/iavc_pci.c
26  *              The AVM ISDN controllers' PCI bus attachment handling.
27  *
28  * $FreeBSD: src/sys/i4b/capi/iavc/iavc_pci.c,v 1.1.2.1 2001/08/10 14:08:34 obrien Exp $
29  * $DragonFly: src/sys/net/i4b/capi/iavc/iavc_pci.c,v 1.5 2004/04/16 15:40:21 joerg Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38
39 #include <machine/clock.h>
40
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/bus.h>
44 #include <sys/rman.h>
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47
48 #include <bus/pci/pcireg.h>
49 #include <bus/pci/pcivar.h>
50
51 #include <net/i4b/include/machine/i4b_debug.h>
52 #include <net/i4b/include/machine/i4b_ioctl.h>
53 #include <net/i4b/include/machine/i4b_trace.h>
54
55 #include "../../include/i4b_global.h"
56 #include "../../include/i4b_l3l4.h"
57 #include "../../include/i4b_mbuf.h"
58 #include "../capi.h"
59
60 #include "iavc.h"
61
62 /* PCI device ids */
63
64 #define PCI_AVM_VID   0x1244
65 #define PCI_AVMT1_DID 0x1200
66 #define PCI_AVMB1_DID 0x0700
67
68 /* PCI driver linkage */
69
70 static void iavc_pci_intr(iavc_softc_t *sc);
71 static int iavc_pci_probe(device_t dev);
72 static int iavc_pci_attach(device_t dev);
73
74 static device_method_t iavc_pci_methods[] =
75 {
76     DEVMETHOD(device_probe,     iavc_pci_probe),
77     DEVMETHOD(device_attach,    iavc_pci_attach),
78     { 0, 0 }
79 };
80
81 static driver_t iavc_pci_driver =
82 {
83     "iavc",
84     iavc_pci_methods,
85     0
86 };
87
88 static devclass_t iavc_pci_devclass;
89
90 DRIVER_MODULE(iavc, pci, iavc_pci_driver, iavc_pci_devclass, 0, 0);
91
92 /* Driver soft contexts */
93
94 iavc_softc_t iavc_sc[IAVC_MAXUNIT];
95
96 /*---------------------------------------------------------------------------*
97  *
98  *---------------------------------------------------------------------------*/
99
100 static int
101 iavc_pci_probe(device_t dev)
102 {
103     u_int16_t did = pci_get_device(dev);
104     u_int16_t vid = pci_get_vendor(dev);
105
106     if ((vid == PCI_AVM_VID) && (did == PCI_AVMT1_DID)) {
107         device_set_desc(dev, "AVM T1 PCI");
108     } else if ((vid == PCI_AVM_VID) && (did == PCI_AVMB1_DID)) {
109         device_set_desc(dev, "AVM B1 PCI");
110     } else {
111         return(ENXIO);
112     }
113
114     return(0);
115 }
116
117 /*---------------------------------------------------------------------------*
118  *
119  *---------------------------------------------------------------------------*/
120
121 static int
122 iavc_pci_attach(device_t dev)
123 {
124     struct iavc_softc *sc;
125     void *ih = 0;
126     u_int16_t did = pci_get_device(dev);
127     int unit = device_get_unit(dev), ret;
128         
129     /* check max unit range */
130         
131     if (unit >= IAVC_MAXUNIT) {
132         printf("iavc%d: too many units\n", unit);
133         return(ENXIO);  
134     }   
135
136     sc = iavc_find_sc(unit);    /* get softc */ 
137         
138     sc->sc_unit = unit;
139
140     /* use the i/o mapped base address */
141         
142     sc->sc_resources.io_rid[0] = 0x14;
143         
144     if (!(sc->sc_resources.io_base[0] =
145          bus_alloc_resource(dev, SYS_RES_IOPORT,
146                             &sc->sc_resources.io_rid[0],
147                             0UL, ~0UL, 1, RF_ACTIVE))) {
148         printf("iavc%d: can't allocate io region\n", unit);
149         return(ENXIO);                                       
150     }
151
152     sc->sc_iobase = rman_get_start(sc->sc_resources.io_base[0]);
153     sc->sc_io_bt = rman_get_bustag(sc->sc_resources.io_base[0]);
154     sc->sc_io_bh = rman_get_bushandle(sc->sc_resources.io_base[0]);
155
156     /* use the memory mapped DMA controller */
157         
158     sc->sc_resources.mem_rid = 0x10;
159         
160     if (!(sc->sc_resources.mem =
161          bus_alloc_resource(dev, SYS_RES_MEMORY,
162                             &sc->sc_resources.mem_rid,
163                             0UL, ~0UL, 1, RF_ACTIVE))) {
164         printf("iavc%d: can't allocate memory region\n", unit);
165         return(ENXIO);                                       
166     }
167
168     sc->sc_membase = rman_get_start(sc->sc_resources.mem);
169     sc->sc_mem_bt = rman_get_bustag(sc->sc_resources.mem);
170     sc->sc_mem_bh = rman_get_bushandle(sc->sc_resources.mem);
171
172     /* do some detection */
173
174     sc->sc_t1 = FALSE;
175     sc->sc_dma = FALSE;
176     b1dma_reset(sc);
177
178     if (did == PCI_AVMT1_DID) {
179         sc->sc_capi.card_type = CARD_TYPEC_AVM_T1_PCI;
180         sc->sc_capi.sc_nbch = 30;
181         ret = t1_detect(sc);
182         if (ret) {
183             if (ret < 6) {
184                 printf("iavc%d: no card detected?\n", sc->sc_unit);
185             } else {
186                 printf("iavc%d: black box not on\n", sc->sc_unit);
187             }
188             return(ENXIO);
189         } else {
190             sc->sc_dma = TRUE;
191             sc->sc_t1 = TRUE;
192         }
193
194     } else if (did == PCI_AVMB1_DID) {
195         sc->sc_capi.card_type = CARD_TYPEC_AVM_B1_PCI;
196         sc->sc_capi.sc_nbch = 2;
197         ret = b1dma_detect(sc);
198         if (ret) {
199             ret = b1_detect(sc);
200             if (ret) {
201                 printf("iavc%d: no card detected?\n", sc->sc_unit);
202                 return(ENXIO);
203             }
204         } else {
205             sc->sc_dma = TRUE;
206         }
207     }
208
209     if (sc->sc_dma) b1dma_reset(sc);
210 #if 0
211     if (sc->sc_t1) t1_reset(sc);
212     else b1_reset(sc);
213 #endif
214
215     /* of course we need an interrupt */
216     
217     sc->sc_resources.irq_rid = 0x00;
218         
219     if(!(sc->sc_resources.irq =
220          bus_alloc_resource(dev, SYS_RES_IRQ,
221                             &sc->sc_resources.irq_rid,
222                             0UL, ~0UL, 1, RF_SHAREABLE|RF_ACTIVE))) {
223         printf("iavc%d: can't allocate irq\n",unit);
224         return(ENXIO);
225     }
226
227     /* finalize our own context */
228
229     memset(&sc->sc_txq, 0, sizeof(struct ifqueue));
230     sc->sc_txq.ifq_maxlen = sc->sc_capi.sc_nbch * 4;
231
232 #if defined (__FreeBSD__) && __FreeBSD__ > 4
233     mtx_init(&sc->sc_txq.ifq_mtx, "i4b_ivac_pci", MTX_DEF);
234 #endif
235     
236     sc->sc_intr = FALSE;
237     sc->sc_state = IAVC_DOWN;
238     sc->sc_blocked = FALSE;
239
240     /* setup capi link */
241         
242     sc->sc_capi.load = iavc_load;
243     sc->sc_capi.reg_appl = iavc_register;
244     sc->sc_capi.rel_appl = iavc_release;
245     sc->sc_capi.send = iavc_send;
246     sc->sc_capi.ctx = (void*) sc;
247
248     if (capi_ll_attach(&sc->sc_capi)) {
249         printf("iavc%d: capi attach failed\n", unit);
250         return(ENXIO);
251     }
252
253     /* setup the interrupt */
254
255     if(bus_setup_intr(dev, sc->sc_resources.irq, INTR_TYPE_NET,
256                       (void(*)(void*))iavc_pci_intr,
257                       sc, &ih)) {
258         printf("iavc%d: irq setup failed\n", unit);
259         return(ENXIO);
260     }
261
262     /* the board is now ready to be loaded */
263
264     return(0);
265 }
266
267 /*---------------------------------------------------------------------------*
268  *      IRQ handler
269  *---------------------------------------------------------------------------*/
270
271 static void
272 iavc_pci_intr(struct iavc_softc *sc)
273 {
274     iavc_handle_intr(sc);
275 }