]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/i4b/capi/iavc/iavc_card.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / i4b / capi / iavc / iavc_card.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
26 /* capi/iavc/iavc_card.c
27  *              The AVM ISDN controllers' card specific support routines.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39
40
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/bus.h>
44 #include <sys/rman.h>
45
46 #include <i4b/include/i4b_debug.h>
47 #include <i4b/include/i4b_ioctl.h>
48 #include <i4b/include/i4b_trace.h>
49
50 #include <i4b/include/i4b_global.h>
51 #include <i4b/include/i4b_l3l4.h>
52 #include <i4b/include/i4b_mbuf.h>
53
54 #include <i4b/capi/capi.h>
55
56 #include <i4b/capi/iavc/iavc.h>
57
58 /*
59 //  AVM B1 (active BRI, PIO mode)
60 */
61
62 int b1_detect(iavc_softc_t *sc)
63 {
64     if ((iavc_read_port(sc, B1_INSTAT) & 0xfc) ||
65         (iavc_read_port(sc, B1_OUTSTAT) & 0xfc))
66         return (1);
67
68     b1io_outp(sc, B1_INSTAT, 0x02);
69     b1io_outp(sc, B1_OUTSTAT, 0x02);
70     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) != 2 ||
71         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe) != 2)
72         return (2);
73
74     b1io_outp(sc, B1_INSTAT, 0x00);
75     b1io_outp(sc, B1_OUTSTAT, 0x00);
76     if ((iavc_read_port(sc, B1_INSTAT) & 0xfe) ||
77         (iavc_read_port(sc, B1_OUTSTAT) & 0xfe))
78         return (3);
79
80     return (0); /* found */
81 }
82
83 void b1_disable_irq(iavc_softc_t *sc)
84 {
85     b1io_outp(sc, B1_INSTAT, 0x00);
86 }
87
88 void b1_reset(iavc_softc_t *sc)
89 {
90     b1io_outp(sc, B1_RESET, 0);
91     DELAY(55*2*1000);
92
93     b1io_outp(sc, B1_RESET, 1);
94     DELAY(55*2*1000);
95
96     b1io_outp(sc, B1_RESET, 0);
97     DELAY(55*2*1000);
98 }
99
100 /*
101 //  Newer PCI-based B1's, and T1's, supports DMA
102 */
103
104 int b1dma_detect(iavc_softc_t *sc)
105 {
106     AMCC_WRITE(sc, AMCC_MCSR, 0);
107     DELAY(10*1000);
108     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
109     DELAY(10*1000);
110     AMCC_WRITE(sc, AMCC_MCSR, 0);
111     DELAY(42*1000);
112
113     AMCC_WRITE(sc, AMCC_RXLEN, 0);
114     AMCC_WRITE(sc, AMCC_TXLEN, 0);
115     sc->sc_csr = 0;
116     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
117
118     if (AMCC_READ(sc, AMCC_INTCSR) != 0)
119         return 1;
120
121     AMCC_WRITE(sc, AMCC_RXPTR, 0xffffffff);
122     AMCC_WRITE(sc, AMCC_TXPTR, 0xffffffff);
123     if ((AMCC_READ(sc, AMCC_RXPTR) != 0xfffffffc) ||
124         (AMCC_READ(sc, AMCC_TXPTR) != 0xfffffffc))
125         return 2;
126
127     AMCC_WRITE(sc, AMCC_RXPTR, 0);
128     AMCC_WRITE(sc, AMCC_TXPTR, 0);
129     if ((AMCC_READ(sc, AMCC_RXPTR) != 0) ||
130         (AMCC_READ(sc, AMCC_TXPTR) != 0))
131         return 3;
132
133     iavc_write_port(sc, 0x10, 0x00);
134     iavc_write_port(sc, 0x07, 0x00);
135
136     iavc_write_port(sc, 0x02, 0x02);
137     iavc_write_port(sc, 0x03, 0x02);
138
139     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x02) ||
140         (iavc_read_port(sc, 0x03) != 0x03))
141         return 4;
142
143     iavc_write_port(sc, 0x02, 0x00);
144     iavc_write_port(sc, 0x03, 0x00);
145
146     if (((iavc_read_port(sc, 0x02) & 0xfe) != 0x00) ||
147         (iavc_read_port(sc, 0x03) != 0x01))
148         return 5;
149
150     return (0); /* found */
151 }
152
153 void b1dma_reset(iavc_softc_t *sc)
154 {
155     int s = SPLI4B();
156
157     sc->sc_csr = 0;
158     AMCC_WRITE(sc, AMCC_INTCSR, sc->sc_csr);
159     AMCC_WRITE(sc, AMCC_MCSR, 0);
160     AMCC_WRITE(sc, AMCC_RXLEN, 0);
161     AMCC_WRITE(sc, AMCC_TXLEN, 0);
162
163     iavc_write_port(sc, 0x10, 0x00); /* XXX magic numbers from */
164     iavc_write_port(sc, 0x07, 0x00); /* XXX the linux driver */
165
166     splx(s);
167
168     AMCC_WRITE(sc, AMCC_MCSR, 0);
169     DELAY(10 * 1000);
170     AMCC_WRITE(sc, AMCC_MCSR, 0x0f000000);
171     DELAY(10 * 1000);
172     AMCC_WRITE(sc, AMCC_MCSR, 0);
173     DELAY(42 * 1000);
174 }
175
176 /*
177 //  AVM T1 (active PRI)
178 */
179
180 /* XXX how do these differ from b1io_{read,write}_reg()? XXX */
181
182 static int b1dma_tx_empty(int iobase)
183 { return inb(iobase + 3) & 1; }
184
185 static int b1dma_rx_full(int iobase)
186 { return inb(iobase + 2) & 1; }
187
188 static int b1dma_tolink(iavc_softc_t *sc, void *buf, int len)
189 {
190     volatile int spin;
191     char *s = (char*) buf;
192     while (len--) {
193         spin = 0;
194         while (!b1dma_tx_empty(sc->sc_iobase) && spin < 100000)
195             spin++;
196         if (!b1dma_tx_empty(sc->sc_iobase))
197             return -1;
198         t1io_outp(sc, 1, *s++);
199     }
200     return 0;
201 }
202
203 static int b1dma_fromlink(iavc_softc_t *sc, void *buf, int len)
204 {
205     volatile int spin;
206     char *s = (char*) buf;
207     while (len--) {
208         spin = 0;
209         while (!b1dma_rx_full(sc->sc_iobase) && spin < 100000)
210             spin++;
211         if (!b1dma_rx_full(sc->sc_iobase))
212             return -1;
213         *s++ = t1io_inp(sc, 0);
214     }
215     return 0;
216 }
217
218 static int WriteReg(iavc_softc_t *sc, u_int32_t reg, u_int8_t val)
219 {
220     u_int8_t cmd = 0;
221     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
222         b1dma_tolink(sc, &reg, 4) == 0) {
223         u_int32_t tmp = val;
224         return b1dma_tolink(sc, &tmp, 4);
225     }
226     return -1;
227 }
228
229 static u_int8_t ReadReg(iavc_softc_t *sc, u_int32_t reg)
230 {
231     u_int8_t cmd = 1;
232     if (b1dma_tolink(sc, &cmd, 1) == 0 &&
233         b1dma_tolink(sc, &reg, 4) == 0) {
234         u_int32_t tmp;
235         if (b1dma_fromlink(sc, &tmp, 4) == 0)
236             return (u_int8_t) tmp;
237     }
238     return 0xff;
239 }
240
241 int t1_detect(iavc_softc_t *sc)
242 {
243     int ret = b1dma_detect(sc);
244     if (ret) return ret;
245
246     if ((WriteReg(sc, 0x80001000, 0x11) != 0) ||
247         (WriteReg(sc, 0x80101000, 0x22) != 0) ||
248         (WriteReg(sc, 0x80201000, 0x33) != 0) ||
249         (WriteReg(sc, 0x80301000, 0x44) != 0))
250         return 6;
251
252     if ((ReadReg(sc, 0x80001000) != 0x11) ||
253         (ReadReg(sc, 0x80101000) != 0x22) ||
254         (ReadReg(sc, 0x80201000) != 0x33) ||
255         (ReadReg(sc, 0x80301000) != 0x44))
256         return 7;
257
258     if ((WriteReg(sc, 0x80001000, 0x55) != 0) ||
259         (WriteReg(sc, 0x80101000, 0x66) != 0) ||
260         (WriteReg(sc, 0x80201000, 0x77) != 0) ||
261         (WriteReg(sc, 0x80301000, 0x88) != 0))
262         return 8;
263
264     if ((ReadReg(sc, 0x80001000) != 0x55) ||
265         (ReadReg(sc, 0x80101000) != 0x66) ||
266         (ReadReg(sc, 0x80201000) != 0x77) ||
267         (ReadReg(sc, 0x80301000) != 0x88))
268         return 9;
269
270     return 0; /* found */
271 }
272
273 void t1_disable_irq(iavc_softc_t *sc)
274 {
275     iavc_write_port(sc, T1_IRQMASTER, 0x00);
276 }
277
278 void t1_reset(iavc_softc_t *sc)
279 {
280     b1_reset(sc);
281     iavc_write_port(sc, B1_INSTAT, 0x00);
282     iavc_write_port(sc, B1_OUTSTAT, 0x00);
283     iavc_write_port(sc, T1_IRQMASTER, 0x00);
284     iavc_write_port(sc, T1_RESETBOARD, 0x0f);
285 }